Skip to content

Commit 85c1709

Browse files
committed
PhpMd CyclomaticComplexity
1 parent df15676 commit 85c1709

14 files changed

+202
-150
lines changed

src/Domain/Messaging/Service/BounceProcessingServiceInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace PhpList\Core\Domain\Messaging\Service;
66

7-
use Symfony\Component\Console\Style\SymfonyStyle;
8-
97
interface BounceProcessingServiceInterface
108
{
11-
public function processMailbox(SymfonyStyle $inputOutput, string $mailbox, int $max, bool $testMode): string;
9+
public function processMailbox(string $mailbox, int $max, bool $testMode): string;
1210
}

src/Domain/Messaging/Service/ConsecutiveBounceHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private function hasRealId(?Bounce $bounce): bool
120120
private function applyThresholdActions($user, int $consecutive, bool $alreadyUnsubscribed): bool
121121
{
122122
if ($consecutive >= $this->unsubscribeThreshold && !$alreadyUnsubscribed) {
123-
$this->subscriberManager->markUnconfirmed($user->getId());
123+
$this->subscriberRepository->markUnconfirmed($user->getId());
124124
$this->subscriberHistoryManager->addHistory(
125125
subscriber: $user,
126126
message: 'Auto Unconfirmed',

src/Domain/Messaging/Service/Handler/DecreaseCountConfirmUserAndDeleteBounceHandler.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@
55
namespace PhpList\Core\Domain\Messaging\Service\Handler;
66

77
use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager;
8+
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
89
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberHistoryManager;
910
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
1011

1112
class DecreaseCountConfirmUserAndDeleteBounceHandler implements BounceActionHandlerInterface
1213
{
14+
private SubscriberHistoryManager $subscriberHistoryManager;
15+
private SubscriberManager $subscriberManager;
16+
private BounceManager $bounceManager;
17+
private SubscriberRepository $subscriberRepository;
18+
1319
public function __construct(
14-
private readonly SubscriberHistoryManager $subscriberHistoryManager,
15-
private readonly SubscriberManager $subscriberManager,
16-
private readonly BounceManager $bounceManager,
17-
) {}
20+
SubscriberHistoryManager $subscriberHistoryManager,
21+
SubscriberManager $subscriberManager,
22+
BounceManager $bounceManager,
23+
SubscriberRepository $subscriberRepository,
24+
) {
25+
$this->subscriberHistoryManager = $subscriberHistoryManager;
26+
$this->subscriberManager = $subscriberManager;
27+
$this->bounceManager = $bounceManager;
28+
$this->subscriberRepository = $subscriberRepository;
29+
}
1830

1931
public function supports(string $action): bool
2032
{
@@ -26,11 +38,11 @@ public function handle(array $closureData): void
2638
if (!empty($closureData['subscriber'])) {
2739
$this->subscriberManager->decrementBounceCount($closureData['subscriber']);
2840
if (!$closureData['confirmed']) {
29-
$this->subscriberManager->markConfirmed($closureData['userId']);
41+
$this->subscriberRepository->markConfirmed($closureData['userId']);
3042
$this->subscriberHistoryManager->addHistory(
31-
$closureData['subscriber'],
32-
'Auto confirmed',
33-
'Subscriber auto confirmed for bounce rule '.$closureData['ruleId']
43+
subscriber: $closureData['subscriber'],
44+
message: 'Auto confirmed',
45+
details: 'Subscriber auto confirmed for bounce rule '.$closureData['ruleId']
3446
);
3547
}
3648
}

src/Domain/Messaging/Service/Handler/UnconfirmUserAndDeleteBounceHandler.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
namespace PhpList\Core\Domain\Messaging\Service\Handler;
66

77
use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager;
8+
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
89
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberHistoryManager;
9-
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
1010

1111
class UnconfirmUserAndDeleteBounceHandler implements BounceActionHandlerInterface
1212
{
13+
private SubscriberHistoryManager $subscriberHistoryManager;
14+
private SubscriberRepository $subscriberRepository;
15+
private BounceManager $bounceManager;
16+
1317
public function __construct(
14-
private readonly SubscriberHistoryManager $subscriberHistoryManager,
15-
private readonly SubscriberManager $subscriberManager,
16-
private readonly BounceManager $bounceManager,
17-
) {}
18+
SubscriberHistoryManager $subscriberHistoryManager,
19+
SubscriberRepository $subscriberRepository,
20+
BounceManager $bounceManager,
21+
) {
22+
$this->subscriberHistoryManager = $subscriberHistoryManager;
23+
$this->subscriberRepository = $subscriberRepository;
24+
$this->bounceManager = $bounceManager;
25+
}
1826

1927
public function supports(string $action): bool
2028
{
@@ -24,11 +32,11 @@ public function supports(string $action): bool
2432
public function handle(array $closureData): void
2533
{
2634
if (!empty($closureData['subscriber']) && $closureData['confirmed']) {
27-
$this->subscriberManager->markUnconfirmed($closureData['userId']);
35+
$this->subscriberRepository->markUnconfirmed($closureData['userId']);
2836
$this->subscriberHistoryManager->addHistory(
29-
$closureData['subscriber'],
30-
'Auto unconfirmed',
31-
'Subscriber auto unconfirmed for bounce rule '.$closureData['ruleId']
37+
subscriber: $closureData['subscriber'],
38+
message: 'Auto unconfirmed',
39+
details: 'Subscriber auto unconfirmed for bounce rule '.$closureData['ruleId']
3240
);
3341
}
3442
$this->bounceManager->delete($closureData['bounce']);

src/Domain/Messaging/Service/Handler/UnconfirmUserHandler.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44

55
namespace PhpList\Core\Domain\Messaging\Service\Handler;
66

7+
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
78
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberHistoryManager;
8-
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
99

1010
class UnconfirmUserHandler implements BounceActionHandlerInterface
1111
{
12+
private SubscriberRepository $subscriberRepository;
13+
private SubscriberHistoryManager $subscriberHistoryManager;
14+
1215
public function __construct(
13-
private readonly SubscriberManager $subscriberManager,
14-
private readonly SubscriberHistoryManager $subscriberHistoryManager,
15-
) {}
16+
SubscriberRepository $subscriberRepository,
17+
SubscriberHistoryManager $subscriberHistoryManager,
18+
) {
19+
$this->subscriberRepository = $subscriberRepository;
20+
$this->subscriberHistoryManager = $subscriberHistoryManager;
21+
}
1622

1723
public function supports(string $action): bool
1824
{
@@ -22,7 +28,7 @@ public function supports(string $action): bool
2228
public function handle(array $closureData): void
2329
{
2430
if (!empty($closureData['subscriber']) && $closureData['confirmed']) {
25-
$this->subscriberManager->markUnconfirmed($closureData['userId']);
31+
$this->subscriberRepository->markUnconfirmed($closureData['userId']);
2632
$this->subscriberHistoryManager->addHistory(
2733
$closureData['subscriber'],
2834
'Auto Unconfirmed',

src/Domain/Messaging/Service/MessageParser.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
namespace PhpList\Core\Domain\Messaging\Service;
66

7-
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
7+
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
88

99
class MessageParser
1010
{
11-
public function __construct(
12-
private readonly SubscriberManager $subscriberManager,
13-
) {
11+
private SubscriberRepository $subscriberRepository;
12+
13+
public function __construct(SubscriberRepository $subscriberRepository)
14+
{
15+
$this->subscriberRepository = $subscriberRepository;
1416
}
17+
1518
public function decodeBody(string $header, string $body): string
1619
{
1720
$transferEncoding = '';
@@ -41,17 +44,17 @@ public function findUserId(string $text): ?int
4144
if (preg_match('/(?:X-ListMember|X-User): (.*)\r\n/iU', $text, $match)) {
4245
$user = trim($match[1]);
4346
if (str_contains($user, '@')) {
44-
return $this->subscriberManager->getSubscriberByEmail($user)?->getId();
47+
return $this->subscriberRepository->findOneByEmail($user)?->getId();
4548
} elseif (preg_match('/^\d+$/', $user)) {
4649
return (int)$user;
4750
} elseif ($user !== '') {
48-
return $this->subscriberManager->getSubscriberByEmail($user)?->getId();
51+
return $this->subscriberRepository->findOneByEmail($user)?->getId();
4952
}
5053
}
5154
// Fallback: parse any email in the body and see if it is a subscriber
5255
if (preg_match_all('/[._a-zA-Z0-9-]+@[.a-zA-Z0-9-]+/', $text, $regs)) {
5356
foreach ($regs[0] as $email) {
54-
$id = $this->subscriberManager->getSubscriberByEmail($email)?->getId();
57+
$id = $this->subscriberRepository->findOneByEmail($email)?->getId();
5558
if ($id) {
5659
return $id;
5760
}

src/Domain/Messaging/Service/NativeBounceProcessingService.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use PhpList\Core\Domain\Common\Mail\NativeImapMailReader;
99
use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager;
1010
use PhpList\Core\Domain\Messaging\Service\Processor\BounceDataProcessor;
11+
use Psr\Log\LoggerInterface;
1112
use RuntimeException;
12-
use Symfony\Component\Console\Style\SymfonyStyle;
1313
use Throwable;
1414

1515
class NativeBounceProcessingService implements BounceProcessingServiceInterface
@@ -18,6 +18,7 @@ class NativeBounceProcessingService implements BounceProcessingServiceInterface
1818
private NativeImapMailReader $mailReader;
1919
private MessageParser $messageParser;
2020
private BounceDataProcessor $bounceDataProcessor;
21+
private LoggerInterface $logger;
2122
private bool $purgeProcessed;
2223
private bool $purgeUnprocessed;
2324

@@ -26,73 +27,74 @@ public function __construct(
2627
NativeImapMailReader $mailReader,
2728
MessageParser $messageParser,
2829
BounceDataProcessor $bounceDataProcessor,
30+
LoggerInterface $logger,
2931
bool $purgeProcessed,
3032
bool $purgeUnprocessed
3133
) {
3234
$this->bounceManager = $bounceManager;
3335
$this->mailReader = $mailReader;
3436
$this->messageParser = $messageParser;
3537
$this->bounceDataProcessor = $bounceDataProcessor;
38+
$this->logger = $logger;
3639
$this->purgeProcessed = $purgeProcessed;
3740
$this->purgeUnprocessed = $purgeUnprocessed;
3841
}
3942

4043
public function processMailbox(
41-
SymfonyStyle $inputOutput,
4244
string $mailbox,
4345
int $max,
4446
bool $testMode
4547
): string {
46-
$link = $this->openOrFail($inputOutput, $mailbox, $testMode);
48+
$link = $this->openOrFail($mailbox, $testMode);
4749

48-
$num = $this->prepareAndCapCount($inputOutput, $link, $max);
50+
$num = $this->prepareAndCapCount( $link, $max);
4951
if ($num === 0) {
5052
$this->mailReader->close($link, false);
5153

5254
return '';
5355
}
5456

55-
$this->announceDeletionMode($inputOutput, $testMode);
57+
$this->announceDeletionMode($testMode);
5658

5759
for ($messageNumber = 1; $messageNumber <= $num; $messageNumber++) {
5860
$this->handleMessage($link, $messageNumber, $testMode);
5961
}
6062

61-
$this->finalize($inputOutput, $link, $testMode);
63+
$this->finalize($link, $testMode);
6264

6365
return '';
6466
}
6567

66-
private function openOrFail(SymfonyStyle $io, string $mailbox, bool $testMode): Connection
68+
private function openOrFail(string $mailbox, bool $testMode): Connection
6769
{
6870
try {
6971
return $this->mailReader->open($mailbox, $testMode ? 0 : CL_EXPUNGE);
7072
} catch (Throwable $e) {
71-
$io->error('Cannot open mailbox file: '.$e->getMessage());
73+
$this->logger->error('Cannot open mailbox file: '.$e->getMessage());
7274
throw new RuntimeException('Cannot open mbox file');
7375
}
7476
}
7577

76-
private function prepareAndCapCount(SymfonyStyle $inputOutput, Connection $link, int $max): int
78+
private function prepareAndCapCount(Connection $link, int $max): int
7779
{
7880
$num = $this->mailReader->numMessages($link);
79-
$inputOutput->writeln(sprintf('%d bounces to fetch from the mailbox', $num));
81+
$this->logger->info(sprintf('%d bounces to fetch from the mailbox', $num));
8082
if ($num === 0) {
8183
return 0;
8284
}
8385

84-
$inputOutput->writeln('Please do not interrupt this process');
86+
$this->logger->info('Please do not interrupt this process');
8587
if ($num > $max) {
86-
$inputOutput->writeln(sprintf('Processing first %d bounces', $max));
88+
$this->logger->info(sprintf('Processing first %d bounces', $max));
8789
$num = $max;
8890
}
8991

9092
return $num;
9193
}
9294

93-
private function announceDeletionMode(SymfonyStyle $io, bool $testMode): void
95+
private function announceDeletionMode(bool $testMode): void
9496
{
95-
$io->writeln(
97+
$this->logger->info(
9698
$testMode
9799
? 'Running in test mode, not deleting messages from mailbox'
98100
: 'Processed messages will be deleted from the mailbox'
@@ -118,9 +120,9 @@ private function handleMessage(Connection $link, int $messageNumber, bool $testM
118120
}
119121
}
120122

121-
private function finalize(SymfonyStyle $io, Connection $link, bool $testMode): void
123+
private function finalize(Connection $link, bool $testMode): void
122124
{
123-
$io->writeln('Closing mailbox, and purging messages');
125+
$this->logger->info('Closing mailbox, and purging messages');
124126
$this->mailReader->close($link, !$testMode);
125127
}
126128

src/Domain/Messaging/Service/Processor/BounceDataProcessor.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,27 @@
1515

1616
class BounceDataProcessor
1717
{
18+
private readonly BounceManager $bounceManager;
19+
private readonly SubscriberRepository $subscriberRepository;
20+
private readonly MessageRepository $messageRepository;
21+
private readonly LoggerInterface $logger;
22+
private readonly SubscriberManager $subscriberManager;
23+
private readonly SubscriberHistoryManager $subscriberHistoryManager;
24+
1825
public function __construct(
19-
private readonly BounceManager $bounceManager,
20-
private readonly SubscriberRepository $users,
21-
private readonly MessageRepository $messages,
22-
private readonly LoggerInterface $logger,
23-
private readonly SubscriberManager $subscriberManager,
24-
private readonly SubscriberHistoryManager $subscriberHistoryManager,
26+
BounceManager $bounceManager,
27+
SubscriberRepository $subscriberRepository,
28+
MessageRepository $messageRepository,
29+
LoggerInterface $logger,
30+
SubscriberManager $subscriberManager,
31+
SubscriberHistoryManager $subscriberHistoryManager,
2532
) {
33+
$this->bounceManager = $bounceManager;
34+
$this->subscriberRepository = $subscriberRepository;
35+
$this->messageRepository = $messageRepository;
36+
$this->logger = $logger;
37+
$this->subscriberManager = $subscriberManager;
38+
$this->subscriberHistoryManager = $subscriberHistoryManager;
2639
}
2740

2841
public function process(Bounce $bounce, ?string $msgId, ?int $userId, DateTimeImmutable $bounceDate): bool
@@ -64,7 +77,7 @@ private function handleSystemMessageWithUser(
6477
comment: sprintf('%d marked unconfirmed', $userId)
6578
);
6679
$this->bounceManager->linkUserMessageBounce($bounce, $date, $userId);
67-
$this->subscriberManager->markUnconfirmed($userId);
80+
$this->subscriberRepository->markUnconfirmed($userId);
6881
$this->logger->info('system message bounced, user marked unconfirmed', ['userId' => $userId]);
6982

7083
if ($userOrNull) {
@@ -99,8 +112,8 @@ private function handleKnownMessageAndUser(
99112
status: sprintf('bounced list message %d', $msgId),
100113
comment: sprintf('%d bouncecount increased', $userId)
101114
);
102-
$this->messages->incrementBounceCount($msgId);
103-
$this->users->incrementBounceCount($userId);
115+
$this->messageRepository->incrementBounceCount($msgId);
116+
$this->subscriberRepository->incrementBounceCount($userId);
104117
} else {
105118
$this->bounceManager->linkUserMessageBounce($bounce, $date, $userId, $msgId);
106119
$this->bounceManager->update(
@@ -120,19 +133,19 @@ private function handleUserOnly(Bounce $bounce, int $userId): bool
120133
status: 'bounced unidentified message',
121134
comment: sprintf('%d bouncecount increased', $userId)
122135
);
123-
$this->users->incrementBounceCount($userId);
136+
$this->subscriberRepository->incrementBounceCount($userId);
124137

125138
return true;
126139
}
127140

128141
private function handleMessageOnly(Bounce $bounce, int $msgId): bool
129142
{
130143
$this->bounceManager->update(
131-
$bounce,
132-
sprintf('bounced list message %d', $msgId),
133-
'unknown user'
144+
bounce: $bounce,
145+
status: sprintf('bounced list message %d', $msgId),
146+
comment: 'unknown user'
134147
);
135-
$this->messages->incrementBounceCount($msgId);
148+
$this->messageRepository->incrementBounceCount($msgId);
136149

137150
return true;
138151
}

0 commit comments

Comments
 (0)