|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\SyliusMailchimpPlugin\Message\Handler; |
| 6 | + |
| 7 | +use Doctrine\Persistence\ManagerRegistry; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | +use function Safe\sprintf; |
| 10 | +use Setono\SyliusMailchimpPlugin\Client\ClientInterface; |
| 11 | +use Setono\SyliusMailchimpPlugin\Exception\ClientException; |
| 12 | +use Setono\SyliusMailchimpPlugin\Message\Command\PushCustomer; |
| 13 | +use Setono\SyliusMailchimpPlugin\Model\CustomerInterface; |
| 14 | +use Setono\SyliusMailchimpPlugin\Provider\AudienceProviderInterface; |
| 15 | +use Setono\SyliusMailchimpPlugin\Repository\CustomerRepositoryInterface; |
| 16 | +use Setono\SyliusMailchimpPlugin\Workflow\MailchimpWorkflow; |
| 17 | +use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; |
| 18 | +use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
| 19 | +use Symfony\Component\Workflow\Registry; |
| 20 | +use Webmozart\Assert\Assert; |
| 21 | + |
| 22 | +final class PushCustomerHandler implements MessageHandlerInterface |
| 23 | +{ |
| 24 | + /** @var CustomerRepositoryInterface */ |
| 25 | + private $customerRepository; |
| 26 | + |
| 27 | + /** @var AudienceProviderInterface */ |
| 28 | + private $audienceProvider; |
| 29 | + |
| 30 | + /** @var ClientInterface */ |
| 31 | + private $client; |
| 32 | + |
| 33 | + /** @var Registry */ |
| 34 | + private $workflowRegistry; |
| 35 | + |
| 36 | + /** @var LoggerInterface */ |
| 37 | + private $logger; |
| 38 | + |
| 39 | + /** @var ManagerRegistry */ |
| 40 | + private $managerRegistry; |
| 41 | + |
| 42 | + public function __construct( |
| 43 | + CustomerRepositoryInterface $customerRepository, |
| 44 | + AudienceProviderInterface $audienceProvider, |
| 45 | + ClientInterface $client, |
| 46 | + Registry $workflowRegistry, |
| 47 | + LoggerInterface $logger, |
| 48 | + ManagerRegistry $managerRegistry |
| 49 | + ) { |
| 50 | + $this->customerRepository = $customerRepository; |
| 51 | + $this->audienceProvider = $audienceProvider; |
| 52 | + $this->client = $client; |
| 53 | + $this->workflowRegistry = $workflowRegistry; |
| 54 | + $this->logger = $logger; |
| 55 | + $this->managerRegistry = $managerRegistry; |
| 56 | + } |
| 57 | + |
| 58 | + public function __invoke(PushCustomer $message): void |
| 59 | + { |
| 60 | + /** @var CustomerInterface|null $customer */ |
| 61 | + $customer = $this->customerRepository->find($message->getCustomerId()); |
| 62 | + Assert::isInstanceOf($customer, CustomerInterface::class); |
| 63 | + |
| 64 | + $workflow = $this->workflowRegistry->get($customer, MailchimpWorkflow::NAME); |
| 65 | + if (!$workflow->can($customer, MailchimpWorkflow::TRANSITION_PROCESS)) { |
| 66 | + // this means that the state was changed another place |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $workflow->apply($customer, MailchimpWorkflow::TRANSITION_PROCESS); |
| 71 | + |
| 72 | + $manager = $this->managerRegistry->getManagerForClass(get_class($customer)); |
| 73 | + if (null === $manager) { |
| 74 | + throw new UnrecoverableMessageHandlingException(sprintf( |
| 75 | + 'No object manager available for class %s', get_class($customer) |
| 76 | + )); |
| 77 | + } |
| 78 | + $manager->flush(); |
| 79 | + |
| 80 | + try { |
| 81 | + $audience = $this->audienceProvider->getAudienceFromCustomerOrders($customer); |
| 82 | + if (null === $audience) { |
| 83 | + $audience = $this->audienceProvider->getAudienceFromContext(); |
| 84 | + } |
| 85 | + if (null === $audience) { |
| 86 | + // todo maybe this should fire a warning somewhere |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + $this->client->updateMember($audience, $customer); |
| 91 | + |
| 92 | + if (!$workflow->can($customer, MailchimpWorkflow::TRANSITION_PUSH)) { |
| 93 | + throw new UnrecoverableMessageHandlingException(sprintf( |
| 94 | + 'Could not apply transition "push" on customer with id "%s". Mailchimp state: "%s"', |
| 95 | + $customer->getId(), $customer->getMailchimpState() |
| 96 | + )); |
| 97 | + } |
| 98 | + |
| 99 | + $workflow->apply($customer, MailchimpWorkflow::TRANSITION_PUSH); |
| 100 | + } catch (\Throwable $e) { |
| 101 | + $this->logger->error(self::buildErrorMessage($e)); |
| 102 | + $customer->setMailchimpError(self::buildErrorMessage($e)); |
| 103 | + $workflow->apply($customer, MailchimpWorkflow::TRANSITION_FAIL); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static function buildErrorMessage(\Throwable $e): string |
| 108 | + { |
| 109 | + $error = $e->getMessage() . "\n\n"; |
| 110 | + if ($e instanceof ClientException) { |
| 111 | + $error .= 'Uri: ' . $e->getUri() . "\n\n"; |
| 112 | + $error .= 'Status code: ' . $e->getStatusCode() . "\n\n"; |
| 113 | + $error .= "Options:\n" . print_r($e->getOptions(), true) . "\n\n"; |
| 114 | + } |
| 115 | + |
| 116 | + $error .= $e->getTraceAsString(); |
| 117 | + |
| 118 | + return $error; |
| 119 | + } |
| 120 | +} |
0 commit comments