22
33declare (strict_types=1 );
44
5- namespace PhpList \Core \Domain \Messaging \Service \ Processor ;
5+ namespace PhpList \Core \Domain \Messaging \MessageHandler ;
66
77use Doctrine \ORM \EntityManagerInterface ;
8+ use PhpList \Core \Domain \Messaging \Message \CampaignProcessorMessage ;
89use PhpList \Core \Domain \Messaging \Model \Message ;
9- use PhpList \Core \Domain \Messaging \Model \UserMessage ;
10- use PhpList \Core \Domain \Messaging \Model \Message \UserMessageStatus ;
1110use PhpList \Core \Domain \Messaging \Model \Message \MessageStatus ;
11+ use PhpList \Core \Domain \Messaging \Model \Message \UserMessageStatus ;
12+ use PhpList \Core \Domain \Messaging \Model \UserMessage ;
13+ use PhpList \Core \Domain \Messaging \Repository \MessageRepository ;
1214use PhpList \Core \Domain \Messaging \Repository \UserMessageRepository ;
1315use PhpList \Core \Domain \Messaging \Service \Handler \RequeueHandler ;
14- use PhpList \Core \Domain \Messaging \Service \RateLimitedCampaignMailer ;
1516use PhpList \Core \Domain \Messaging \Service \MaxProcessTimeLimiter ;
1617use PhpList \Core \Domain \Messaging \Service \MessageProcessingPreparator ;
18+ use PhpList \Core \Domain \Messaging \Service \RateLimitedCampaignMailer ;
19+ use PhpList \Core \Domain \Subscription \Model \Subscriber ;
1720use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberHistoryManager ;
1821use PhpList \Core \Domain \Subscription \Service \Provider \SubscriberProvider ;
19- use PhpList \Core \Domain \Subscription \Model \Subscriber ;
2022use Psr \Log \LoggerInterface ;
21- use Symfony \Component \Console \Output \OutputInterface ;
2223use Symfony \Contracts \Translation \TranslatorInterface ;
2324use Throwable ;
2425
2728 * @SuppressWarnings(PHPMD.StaticAccess)
2829 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
2930 */
30- class CampaignProcessor
31+ class CampaignProcessorMessageHandler
3132{
3233 private RateLimitedCampaignMailer $ mailer ;
3334 private EntityManagerInterface $ entityManager ;
@@ -39,6 +40,7 @@ class CampaignProcessor
3940 private RequeueHandler $ requeueHandler ;
4041 private TranslatorInterface $ translator ;
4142 private SubscriberHistoryManager $ subscriberHistoryManager ;
43+ private MessageRepository $ messageRepository ;
4244
4345 public function __construct (
4446 RateLimitedCampaignMailer $ mailer ,
@@ -51,6 +53,7 @@ public function __construct(
5153 RequeueHandler $ requeueHandler ,
5254 TranslatorInterface $ translator ,
5355 SubscriberHistoryManager $ subscriberHistoryManager ,
56+ MessageRepository $ messageRepository ,
5457 ) {
5558 $ this ->mailer = $ mailer ;
5659 $ this ->entityManager = $ entityManager ;
@@ -62,10 +65,21 @@ public function __construct(
6265 $ this ->requeueHandler = $ requeueHandler ;
6366 $ this ->translator = $ translator ;
6467 $ this ->subscriberHistoryManager = $ subscriberHistoryManager ;
68+ $ this ->messageRepository = $ messageRepository ;
6569 }
6670
67- public function process ( Message $ campaign , ? OutputInterface $ output = null ): void
71+ public function __invoke ( CampaignProcessorMessage $ message ): void
6872 {
73+ $ campaign = $ this ->messageRepository ->findByIdAndStatus ($ message ->getMessageId (), MessageStatus::Submitted);
74+ if (!$ campaign ) {
75+ $ this ->logger ->warning (
76+ $ this ->translator ->trans ('Campaign not found or not in submitted status ' ),
77+ ['campaign_id ' => $ message ->getMessageId ()]
78+ );
79+
80+ return ;
81+ }
82+
6983 $ this ->updateMessageStatus ($ campaign , MessageStatus::Prepared);
7084 $ subscribers = $ this ->subscriberProvider ->getSubscribersForMessage ($ campaign );
7185
@@ -75,7 +89,7 @@ public function process(Message $campaign, ?OutputInterface $output = null): voi
7589 $ stoppedEarly = false ;
7690
7791 foreach ($ subscribers as $ subscriber ) {
78- if ($ this ->timeLimiter ->shouldStop ($ output )) {
92+ if ($ this ->timeLimiter ->shouldStop ()) {
7993 $ stoppedEarly = true ;
8094 break ;
8195 }
@@ -92,7 +106,7 @@ public function process(Message $campaign, ?OutputInterface $output = null): voi
92106 if (!filter_var ($ subscriber ->getEmail (), FILTER_VALIDATE_EMAIL )) {
93107 $ this ->updateUserMessageStatus ($ userMessage , UserMessageStatus::InvalidEmailAddress);
94108 $ this ->unconfirmSubscriber ($ subscriber );
95- $ output ?->writeln ($ this ->translator ->trans ('Invalid email, marking unconfirmed: %email% ' , [
109+ $ this -> logger -> warning ($ this ->translator ->trans ('Invalid email, marking unconfirmed: %email% ' , [
96110 '%email% ' => $ subscriber ->getEmail (),
97111 ]));
98112 $ this ->subscriberHistoryManager ->addHistory (
@@ -119,13 +133,13 @@ public function process(Message $campaign, ?OutputInterface $output = null): voi
119133 'subscriber_id ' => $ subscriber ->getId (),
120134 'campaign_id ' => $ campaign ->getId (),
121135 ]);
122- $ output ?->writeln ($ this ->translator ->trans ('Failed to send to: %email% ' , [
136+ $ this -> logger -> warning ($ this ->translator ->trans ('Failed to send to: %email% ' , [
123137 '%email% ' => $ subscriber ->getEmail (),
124138 ]));
125139 }
126140 }
127141
128- if ($ stoppedEarly && $ this ->requeueHandler ->handle ($ campaign, $ output )) {
142+ if ($ stoppedEarly && $ this ->requeueHandler ->handle ($ campaign )) {
129143 $ this ->entityManager ->flush ();
130144 return ;
131145 }
0 commit comments