vendor/bitbag/mailchimp-plugin/src/EventListener/CustomerNewsletterListener.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3. * This file was created by developers working at BitBag
  4. * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5. * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusMailChimpPlugin\EventListener;
  9. use BitBag\SyliusMailChimpPlugin\Handler\NewsletterSubscriptionHandler;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Sylius\Component\Core\Model\CustomerInterface;
  12. use Sylius\Component\Resource\Exception\UnexpectedTypeException;
  13. use Symfony\Component\EventDispatcher\GenericEvent;
  14. final class CustomerNewsletterListener
  15. {
  16. /** @var NewsletterSubscriptionHandler */
  17. private $newsletterSubscriptionHandler;
  18. /** @var EntityManagerInterface */
  19. private $entityManager;
  20. public function __construct(NewsletterSubscriptionHandler $newsletterSubscriptionHandler, EntityManagerInterface $entityManager)
  21. {
  22. $this->newsletterSubscriptionHandler = $newsletterSubscriptionHandler;
  23. $this->entityManager = $entityManager;
  24. }
  25. public function customerCreateEvent(GenericEvent $event): void
  26. {
  27. $customer = $event->getSubject();
  28. if (!$customer instanceof CustomerInterface) {
  29. throw new UnexpectedTypeException(
  30. $customer,
  31. CustomerInterface::class
  32. );
  33. }
  34. $customer->isSubscribedToNewsletter() === false ? $this->unsubscribe($customer) : $this->subscribe($customer);
  35. }
  36. public function customerPostUpdateEvent(GenericEvent $event): void
  37. {
  38. $this->customerCreateEvent($event);
  39. }
  40. public function customerPreUpdateEvent(GenericEvent $event): void
  41. {
  42. $customer = $event->getSubject();
  43. if (!$customer instanceof CustomerInterface) {
  44. throw new UnexpectedTypeException(
  45. $customer,
  46. CustomerInterface::class
  47. );
  48. }
  49. $unitOfWork = $this->entityManager->getUnitOfWork();
  50. $unitOfWork->computeChangeSets();
  51. $changelist = $unitOfWork->getEntityChangeSet($customer);
  52. $oldEmail = $this->getOldEmailFromChangeList($changelist);
  53. if (null !== $oldEmail) {
  54. $this->newsletterSubscriptionHandler->unsubscribeEmail($oldEmail);
  55. }
  56. }
  57. private function subscribe(CustomerInterface $customer): void
  58. {
  59. if (null !== $customer->getEmail()) {
  60. $this->newsletterSubscriptionHandler->subscribe($customer->getEmail());
  61. }
  62. }
  63. private function unsubscribe(CustomerInterface $customer): void
  64. {
  65. $this->newsletterSubscriptionHandler->unsubscribe($customer);
  66. }
  67. private function getOldEmailFromChangeList(array $changelist): ?string
  68. {
  69. if (!array_key_exists('email', $changelist)) {
  70. return null;
  71. }
  72. $emailChanges = $changelist['email'];
  73. if (!is_array($emailChanges)) {
  74. return null;
  75. }
  76. $oldEmail = reset($emailChanges);
  77. if (false === $oldEmail) {
  78. return null;
  79. }
  80. return $oldEmail;
  81. }
  82. }