You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5KB

  1. <?php
  2. namespace domain\User\User;
  3. use domain\User\UserProducer\UserProducerBuilder;
  4. use domain\User\UserProducer\UserProducerRepository;
  5. use domain\_\AbstractService;
  6. use domain\_\ManagerInterface;
  7. class NewsletterManager extends AbstractService implements ManagerInterface
  8. {
  9. protected UserProducerRepository $userProducerRepository;
  10. protected UserProducerBuilder $userProducerBuilder;
  11. public function loadDependencies(): void
  12. {
  13. $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
  14. $this->userProducerBuilder = $this->loadService(UserProducerBuilder::class);
  15. }
  16. public function isUserSubscribedNewsletter(User $user = null): bool
  17. {
  18. if($user) {
  19. $userProducer = $this->userProducerRepository->findOneUserProducer($user);
  20. if($userProducer && $userProducer->newsletter) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. public function subscribeUserNewsletter(User $user): bool
  27. {
  28. $userProducer = $this->userProducerBuilder->createUserProducerIfNotExist($user, $this->getProducerContext());
  29. return $this->userProducerBuilder->updateUserProducerNewsletter($userProducer, true);
  30. }
  31. public function unsubscribeUserNewsletter(User $user): bool
  32. {
  33. $userProducer = $this->userProducerRepository->findOneUserProducer($user);
  34. if($userProducer) {
  35. return $this->userProducerBuilder->updateUserProducerNewsletter($userProducer, false);
  36. }
  37. return false;
  38. }
  39. }