55namespace PhpList \Core \Domain \Messaging \Service \Manager ;
66
77use Doctrine \ORM \EntityManagerInterface ;
8+ use PhpList \Core \Domain \Identity \Model \Administrator ;
89use PhpList \Core \Domain \Messaging \Model \Bounce ;
910use PhpList \Core \Domain \Messaging \Model \BounceRegex ;
1011use PhpList \Core \Domain \Messaging \Model \BounceRegexBounce ;
1112use PhpList \Core \Domain \Messaging \Repository \BounceRegexRepository ;
13+ use Symfony \Component \Validator \Exception \ValidatorException ;
1214
1315class 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 {
0 commit comments