Skip to content

Commit 3e2db6b

Browse files
committed
Bounce regex update
1 parent 56af5dd commit 3e2db6b

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,9 @@ command to extract translation strings
222222
```bash
223223
php bin/console translation:extract --force en --format=xlf
224224
```
225+
226+
```bash
227+
vendor/bin/phpstan analyse -c phpstan.neon;
228+
vendor/bin/phpmd src/ text config/PHPMD/rules.xml;
229+
vendor/bin/phpcs --standard=config/PhpCodeSniffer/ --ignore=*/Migrations/* bin/ src/ tests/ public/;
230+
```

src/Domain/Messaging/Service/Manager/BounceRegexManager.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace PhpList\Core\Domain\Messaging\Service\Manager;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Identity\Model\Administrator;
89
use PhpList\Core\Domain\Messaging\Model\Bounce;
910
use PhpList\Core\Domain\Messaging\Model\BounceRegex;
1011
use PhpList\Core\Domain\Messaging\Model\BounceRegexBounce;
1112
use PhpList\Core\Domain\Messaging\Repository\BounceRegexRepository;
13+
use Symfony\Component\Validator\Exception\ValidatorException;
1214

1315
class BounceRegexManager
1416
{
@@ -25,36 +27,28 @@ public function __construct(
2527

2628
/**
2729
* Creates or updates (if exists) a BounceRegex from a raw regex pattern.
30+
* @throws ValidatorException
2831
*/
29-
public function createOrUpdateFromPattern(
32+
public function create(
3033
string $regex,
34+
Administrator $admin,
3135
?string $action = null,
3236
?int $listOrder = 0,
33-
?int $adminId = null,
3437
?string $comment = null,
3538
?string $status = null
3639
): BounceRegex {
3740
$regexHash = md5($regex);
38-
3941
$existing = $this->bounceRegexRepository->findOneByRegexHash($regexHash);
40-
4142
if ($existing !== null) {
42-
$existing->setRegex($regex)
43-
->setAction($action ?? $existing->getAction())
44-
->setListOrder($listOrder ?? $existing->getListOrder())
45-
->setAdminId($adminId ?? $existing->getAdminId())
46-
->setComment($comment ?? $existing->getComment())
47-
->setStatus($status ?? $existing->getStatus());
48-
49-
return $existing;
43+
throw new ValidatorException('Bounce Regex already exists.');
5044
}
5145

5246
$bounceRegex = new BounceRegex(
5347
regex: $regex,
5448
regexHash: $regexHash,
5549
action: $action,
5650
listOrder: $listOrder,
57-
adminId: $adminId,
51+
adminId: $admin->getId(),
5852
comment: $comment,
5953
status: $status,
6054
count: 0
@@ -65,6 +59,30 @@ public function createOrUpdateFromPattern(
6559
return $bounceRegex;
6660
}
6761

62+
public function update(
63+
BounceRegex $bounceRegex,
64+
string $regex,
65+
?string $action = null,
66+
?int $listOrder = 0,
67+
?string $comment = null,
68+
?string $status = null
69+
): BounceRegex {
70+
$regexHash = md5($regex);
71+
$existing = $this->bounceRegexRepository->findOneByRegexHash($regexHash);
72+
if ($existing !== null && $existing->getId() !== $bounceRegex->getId()) {
73+
throw new ValidatorException('Bounce Regex already exists.');
74+
}
75+
76+
$bounceRegex->setRegex($regex)
77+
->setAction($action ?? $existing->getAction())
78+
->setListOrder($listOrder ?? $existing->getListOrder())
79+
->setRegexHash($regexHash)
80+
->setComment($comment ?? $existing->getComment())
81+
->setStatus($status ?? $existing->getStatus());
82+
83+
return $bounceRegex;
84+
}
85+
6886
/** @return BounceRegex[] */
6987
public function getAll(): array
7088
{

tests/Unit/Domain/Messaging/Service/Manager/BounceRegexManagerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service\Manager;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Identity\Model\Administrator;
89
use PhpList\Core\Domain\Messaging\Model\Bounce;
910
use PhpList\Core\Domain\Messaging\Model\BounceRegex;
1011
use PhpList\Core\Domain\Messaging\Model\BounceRegexBounce;
@@ -35,6 +36,8 @@ public function testCreateNewRegex(): void
3536
{
3637
$pattern = 'user unknown';
3738
$expectedHash = md5($pattern);
39+
$admin = $this->createMock(Administrator::class);
40+
$admin->method('getId')->willReturn(5);
3841

3942
$this->regexRepository->expects($this->once())
4043
->method('findOneByRegexHash')
@@ -45,11 +48,11 @@ public function testCreateNewRegex(): void
4548
->method('persist')
4649
->with($this->isInstanceOf(BounceRegex::class));
4750

48-
$regex = $this->manager->createOrUpdateFromPattern(
51+
$regex = $this->manager->create(
4952
regex: $pattern,
53+
admin: $admin,
5054
action: 'delete',
5155
listOrder: 5,
52-
adminId: 1,
5356
comment: 'test',
5457
status: 'active'
5558
);
@@ -59,7 +62,6 @@ public function testCreateNewRegex(): void
5962
$this->assertSame($expectedHash, $regex->getRegexHash());
6063
$this->assertSame('delete', $regex->getAction());
6164
$this->assertSame(5, $regex->getListOrder());
62-
$this->assertSame(1, $regex->getAdminId());
6365
$this->assertSame('test', $regex->getComment());
6466
$this->assertSame('active', $regex->getStatus());
6567
}
@@ -74,7 +76,7 @@ public function testUpdateExistingRegex(): void
7476
regexHash: $hash,
7577
action: 'keep',
7678
listOrder: 0,
77-
adminId: null,
79+
adminId: 2,
7880
comment: null,
7981
status: 'inactive',
8082
count: 3
@@ -85,11 +87,11 @@ public function testUpdateExistingRegex(): void
8587
->with($hash)
8688
->willReturn($existing);
8789

88-
$updated = $this->manager->createOrUpdateFromPattern(
90+
$updated = $this->manager->update(
91+
bounceRegex: $existing,
8992
regex: $pattern,
9093
action: 'delete',
9194
listOrder: 10,
92-
adminId: 2,
9395
comment: 'upd',
9496
status: 'active'
9597
);

0 commit comments

Comments
 (0)