|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Domain\Messaging\Command; |
| 6 | + |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Doctrine\ORM\EntityManagerInterface; |
| 11 | +use PhpList\Core\Domain\Messaging\Model\Message; |
| 12 | +use PhpList\Core\Domain\Messaging\Repository\MessageRepository; |
| 13 | +use PhpList\Core\Domain\Messaging\Service\MessageProcessingPreparator; |
| 14 | +use PhpList\Core\Domain\Subscription\Service\Provider\SubscriberProvider; |
| 15 | +use Symfony\Component\Lock\LockFactory; |
| 16 | +use Symfony\Component\Mailer\MailerInterface; |
| 17 | +use Symfony\Component\Mime\Email; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +class ProcessQueueCommand extends Command |
| 21 | +{ |
| 22 | + protected static $defaultName = 'phplist:process-queue'; |
| 23 | + |
| 24 | + private MessageRepository $messageRepository; |
| 25 | + private MailerInterface $mailer; |
| 26 | + private LockFactory $lockFactory; |
| 27 | + private EntityManagerInterface $entityManager; |
| 28 | + private SubscriberProvider $subscriberProvider; |
| 29 | + private MessageProcessingPreparator $messageProcessingPreparator; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + MessageRepository $messageRepository, |
| 33 | + MailerInterface $mailer, |
| 34 | + LockFactory $lockFactory, |
| 35 | + EntityManagerInterface $entityManager, |
| 36 | + SubscriberProvider $subscriberProvider, |
| 37 | + MessageProcessingPreparator $messageProcessingPreparator |
| 38 | + ) { |
| 39 | + parent::__construct(); |
| 40 | + $this->messageRepository = $messageRepository; |
| 41 | + $this->mailer = $mailer; |
| 42 | + $this->lockFactory = $lockFactory; |
| 43 | + $this->entityManager = $entityManager; |
| 44 | + $this->subscriberProvider = $subscriberProvider; |
| 45 | + $this->messageProcessingPreparator = $messageProcessingPreparator; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
| 50 | + */ |
| 51 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 52 | + { |
| 53 | + $lock = $this->lockFactory->createLock('queue_processor'); |
| 54 | + if (!$lock->acquire()) { |
| 55 | + $output->writeln('Queue is already being processed by another instance.'); |
| 56 | + |
| 57 | + return Command::FAILURE; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + $this->messageProcessingPreparator->ensureSubscribersHaveUuid($output); |
| 62 | + $this->messageProcessingPreparator->ensureCampaignsHaveUuid($output); |
| 63 | + |
| 64 | + $campaigns = $this->messageRepository->findBy(['status' => 'submitted']); |
| 65 | + |
| 66 | + foreach ($campaigns as $campaign) { |
| 67 | + $this->processCampaign($campaign, $output); |
| 68 | + } |
| 69 | + } finally { |
| 70 | + $lock->release(); |
| 71 | + } |
| 72 | + |
| 73 | + return Command::SUCCESS; |
| 74 | + } |
| 75 | + |
| 76 | + private function processCampaign(Message $campaign, OutputInterface $output): void |
| 77 | + { |
| 78 | + $subscribers = $this->subscriberProvider->getSubscribersForMessage($campaign); |
| 79 | + // todo: check $ISPrestrictions logic |
| 80 | + foreach ($subscribers as $subscriber) { |
| 81 | + if (!filter_var($subscriber->getEmail(), FILTER_VALIDATE_EMAIL)) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + $email = (new Email()) |
| 86 | + |
| 87 | + ->to($subscriber->getEmail()) |
| 88 | + ->subject($campaign->getContent()->getSubject()) |
| 89 | + ->text($campaign->getContent()->getTextMessage()) |
| 90 | + ->html($campaign->getContent()->getText()); |
| 91 | + |
| 92 | + try { |
| 93 | + $this->mailer->send($email); |
| 94 | + |
| 95 | + // todo: log somewhere that this subscriber got email |
| 96 | + } catch (Throwable $e) { |
| 97 | + $output->writeln('Failed to send to: ' . $subscriber->getEmail()); |
| 98 | + } |
| 99 | + |
| 100 | + usleep(100000); |
| 101 | + } |
| 102 | + |
| 103 | + $campaign->getMetadata()->setStatus('sent'); |
| 104 | + $this->entityManager->flush(); |
| 105 | + } |
| 106 | +} |
0 commit comments