44
55namespace Kiboko \Component \Satellite \Plugin \Filtering \Builder ;
66
7+ use Kiboko \Component \Bucket \AcceptanceResultBucket ;
8+ use Kiboko \Component \Bucket \RejectionResultBucket ;
9+ use Kiboko \Component \Bucket \RejectionWithReasonResultBucket ;
10+ use Kiboko \Component \Satellite \Plugin \Filtering \DTO \Exclusion ;
711use Kiboko \Contract \Configurator \StepBuilderInterface ;
812use PhpParser \Builder ;
913use PhpParser \Node ;
@@ -13,13 +17,9 @@ final class Reject implements StepBuilderInterface
1317 private ?Node \Expr $ logger = null ;
1418 private ?Node \Expr $ rejection = null ;
1519 private ?Node \Expr $ state = null ;
16- /** @var list<?Node\Expr > */
20+ /** @var list<?Exclusion > */
1721 private array $ exclusions = [];
1822
19- public function __construct ()
20- {
21- }
22-
2323 public function withLogger (Node \Expr $ logger ): self
2424 {
2525 $ this ->logger = $ logger ;
@@ -41,53 +41,46 @@ public function withState(Node\Expr $state): self
4141 return $ this ;
4242 }
4343
44- public function withExclusions (Node \ Expr ...$ exclusions ): self
44+ public function withExclusions (Exclusion ...$ exclusions ): self
4545 {
4646 array_push ($ this ->exclusions , ...$ exclusions );
4747
4848 return $ this ;
4949 }
5050
51- private function buildExclusions (Node \ Expr ...$ exclusions ): Node \ Expr
51+ private function buildExclusions (Exclusion ...$ exclusions ): array
5252 {
53- if (\count ($ exclusions ) > 3 ) {
54- $ length = \count ($ exclusions );
55- $ middle = (int ) floor ($ length / 2 );
56- $ left = \array_slice ($ exclusions , 0 , $ middle );
57- $ right = \array_slice ($ exclusions , $ middle , $ length );
58-
59- return new Node \Expr \BinaryOp \BooleanAnd (
60- $ this ->buildExclusions (...$ left ),
61- $ this ->buildExclusions (...$ right ),
62- );
63- }
64-
65- if (\count ($ exclusions ) > 2 ) {
66- $ right = array_shift ($ exclusions );
67-
68- return new Node \Expr \BinaryOp \BooleanAnd (
69- $ this ->buildExclusions (...$ exclusions ),
70- $ right ,
71- );
72- }
73-
74- if (\count ($ exclusions ) > 1 ) {
75- $ left = array_pop ($ exclusions );
76- $ right = array_pop ($ exclusions );
77-
78- return new Node \Expr \BinaryOp \BooleanAnd (
79- $ left ,
80- $ right ,
53+ $ statements = [];
54+ foreach ($ exclusions as $ exclusion ) {
55+ $ statements [] = new Node \Stmt \If_ (
56+ $ exclusion ->when ,
57+ [
58+ 'stmts ' => [
59+ new Node \Stmt \Expression (
60+ new Node \Expr \Assign (
61+ new Node \Expr \Variable ('input ' ),
62+ new Node \Expr \Yield_ (
63+ new Node \Expr \New_ (
64+ $ exclusion ->reason ? new Node \Name \FullyQualified (RejectionWithReasonResultBucket::class) : new Node \Name \FullyQualified (RejectionResultBucket::class),
65+ [
66+ new Node \Arg (new Node \Expr \Variable ('input ' )),
67+ $ exclusion ->reason ? new Node \Arg ($ exclusion ->reason ) : new Node \Arg (
68+ new Node \Expr \ConstFetch (
69+ new Node \Name (null )
70+ ),
71+ ),
72+ ]
73+ ),
74+ ),
75+ ),
76+ ),
77+ new Node \Stmt \Continue_ (),
78+ ],
79+ ]
8180 );
8281 }
8382
84- if (\count ($ exclusions ) > 0 ) {
85- return array_pop ($ exclusions );
86- }
87-
88- return new Node \Expr \ConstFetch (
89- new Node \Name ('false ' ),
90- );
83+ return $ statements ;
9184 }
9285
9386 public function getNode (): Node
@@ -113,27 +106,7 @@ class: new Node\Stmt\Class_(null, [
113106 new Node \Name ('true ' ),
114107 ),
115108 [
116- new Node \Stmt \If_ (
117- $ this ->buildExclusions (...$ this ->exclusions ),
118- [
119- 'stmts ' => [
120- new Node \Stmt \Expression (
121- new Node \Expr \Assign (
122- new Node \Expr \Variable ('input ' ),
123- new Node \Expr \Yield_ (
124- new Node \Expr \New_ (
125- new Node \Name \FullyQualified ('Kiboko \\Component \\Bucket \\RejectionResultBucket ' ),
126- [
127- new Node \Arg (new Node \Expr \Variable ('input ' )),
128- ]
129- ),
130- ),
131- ),
132- ),
133- new Node \Stmt \Continue_ (),
134- ],
135- ]
136- ),
109+ ...$ this ->buildExclusions (...$ this ->exclusions ),
137110 new Node \Stmt \Expression (
138111 new Node \Expr \Assign (
139112 new Node \Expr \Variable ('input ' ),
0 commit comments