Skip to content

Commit 9e1ad9c

Browse files
committed
Refactor
1 parent 0f455f5 commit 9e1ad9c

File tree

2 files changed

+89
-30
lines changed

2 files changed

+89
-30
lines changed

src/Domain/Common/IspRestrictionsProvider.php

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,114 @@ public function __construct(
1616

1717
public function load(): IspRestrictions
1818
{
19-
if (!is_file($this->confPath) || !is_readable($this->confPath)) {
19+
$contents = $this->readConfigFile();
20+
if ($contents === null) {
2021
return new IspRestrictions(null, null, null);
2122
}
2223

24+
[$raw, $maxBatch, $minBatchPeriod, $lockFile] = $this->parseContents($contents);
25+
26+
$this->logIfDetected($maxBatch, $minBatchPeriod, $lockFile);
27+
28+
return new IspRestrictions($maxBatch, $minBatchPeriod, $lockFile, $raw);
29+
}
30+
31+
private function readConfigFile(): ?string
32+
{
33+
if (!is_file($this->confPath) || !is_readable($this->confPath)) {
34+
return null;
35+
}
2336
$contents = file_get_contents($this->confPath);
2437
if ($contents === false) {
2538
$this->logger->warning('Cannot read ISP restrictions file', ['path' => $this->confPath]);
26-
return new IspRestrictions(null, null, null);
39+
return null;
2740
}
41+
return $contents;
42+
}
2843

44+
/**
45+
* @return array{0: array<string,string>, 1: ?int, 2: ?int, 3: ?string}
46+
*/
47+
private function parseContents(string $contents): array
48+
{
2949
$maxBatch = null;
3050
$minBatchPeriod = null;
3151
$lockFile = null;
32-
3352
$raw = [];
53+
3454
foreach (preg_split('/\R/', $contents) as $line) {
35-
$line = trim($line);
36-
if ($line === '' || str_starts_with($line, '#') || str_starts_with($line, ';')) {
37-
continue;
38-
}
39-
$parts = explode('=', $line, 2);
40-
if (\count($parts) !== 2) {
55+
[$key, $val] = $this->parseLine($line);
56+
if ($key === null) {
4157
continue;
4258
}
43-
[$key, $val] = array_map('trim', $parts);
4459
$raw[$key] = $val;
60+
[$maxBatch, $minBatchPeriod, $lockFile] = $this->applyKeyValue(
61+
$key,
62+
$val,
63+
$maxBatch,
64+
$minBatchPeriod,
65+
$lockFile
66+
);
67+
}
4568

46-
switch ($key) {
47-
case 'maxbatch':
48-
if ($val !== '' && ctype_digit($val)) {
49-
$maxBatch = (int) $val;
50-
}
51-
break;
52-
case 'minbatchperiod':
53-
if ($val !== '' && ctype_digit($val)) {
54-
$minBatchPeriod = (int) $val;
55-
}
56-
break;
57-
case 'lockfile':
58-
if ($val !== '') {
59-
$lockFile = $val;
60-
}
61-
break;
69+
return [$raw, $maxBatch, $minBatchPeriod, $lockFile];
70+
}
71+
72+
/**
73+
* @return array{0: ?string, 1: string}
74+
*/
75+
private function parseLine(string $line): array
76+
{
77+
$line = trim($line);
78+
if ($line === '' || str_starts_with($line, '#') || str_starts_with($line, ';')) {
79+
return [null, ''];
80+
}
81+
$parts = explode('=', $line, 2);
82+
if (\count($parts) !== 2) {
83+
return [null, ''];
84+
}
85+
86+
return array_map('trim', $parts);
87+
}
88+
89+
/**
90+
* @param string $key
91+
* @param string $val
92+
* @param ?int $maxBatch
93+
* @param ?int $minBatchPeriod
94+
* @param ?string $lockFile
95+
* @return array{0: ?int, 1: ?int, 2: ?string}
96+
*/
97+
private function applyKeyValue(
98+
string $key,
99+
string $val,
100+
?int $maxBatch,
101+
?int $minBatchPeriod,
102+
?string $lockFile
103+
): array {
104+
if ($key === 'maxbatch') {
105+
if ($val !== '' && ctype_digit($val)) {
106+
$maxBatch = (int) $val;
62107
}
108+
return [$maxBatch, $minBatchPeriod, $lockFile];
63109
}
110+
if ($key === 'minbatchperiod') {
111+
if ($val !== '' && ctype_digit($val)) {
112+
$minBatchPeriod = (int) $val;
113+
}
114+
return [$maxBatch, $minBatchPeriod, $lockFile];
115+
}
116+
if ($key === 'lockfile') {
117+
if ($val !== '') {
118+
$lockFile = $val;
119+
}
120+
return [$maxBatch, $minBatchPeriod, $lockFile];
121+
}
122+
return [$maxBatch, $minBatchPeriod, $lockFile];
123+
}
64124

125+
private function logIfDetected(?int $maxBatch, ?int $minBatchPeriod, ?string $lockFile): void
126+
{
65127
if ($maxBatch !== null || $minBatchPeriod !== null || $lockFile !== null) {
66128
$this->logger->info('ISP restrictions detected', [
67129
'path' => $this->confPath,
@@ -70,7 +132,5 @@ public function load(): IspRestrictions
70132
'lockfile' => $lockFile,
71133
]);
72134
}
73-
74-
return new IspRestrictions($maxBatch, $minBatchPeriod, $lockFile, $raw);
75135
}
76136
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public function process(Message $campaign, ?OutputInterface $output = null): voi
6161

6262
$userMessage = $existing ?? new UserMessage($subscriber, $campaign);
6363
$userMessage->setStatus(UserMessageStatus::Active);
64-
$this->entityManager->persist($userMessage);
65-
$this->entityManager->flush();
64+
$this->userMessageRepository->save($userMessage);
6665

6766
$this->rateLimiter->awaitTurn($output);
6867

0 commit comments

Comments
 (0)