diff --git a/src/Plugin/Filtering/Builder/ExclusionsBuilder.php b/src/Plugin/Filtering/Builder/ExclusionsBuilder.php new file mode 100644 index 00000000..694de9d9 --- /dev/null +++ b/src/Plugin/Filtering/Builder/ExclusionsBuilder.php @@ -0,0 +1,61 @@ +> */ + private array $exclusions = []; + + public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):self + { + $this->exclusions[] = [ + 'condition' => $condition, + 'reason' => $reason, + ]; + + return $this; + } + + public function getNode(): array + { + $statements = []; + foreach ($this->exclusions as $exclusion) { + $statements[] = new Node\Stmt\If_( + $exclusion['condition'], + [ + 'stmts' => [ + new Node\Stmt\Expression( + new Node\Expr\Assign( + new Node\Expr\Variable('input'), + new Node\Expr\Yield_( + new Node\Expr\New_( + \array_key_exists('reason', $exclusion) ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class), + [ + new Node\Arg(new Node\Expr\Variable('input')), + \array_key_exists('reason', $exclusion) ? new Node\Arg($exclusion['reason']) : new Node\Arg( + new Node\Expr\ConstFetch( + new Node\Name(null) + ), + ), + ] + ), + ), + ), + ), + new Node\Stmt\Continue_(), + ], + ] + ); + } + + return $statements; + } +} diff --git a/src/Plugin/Filtering/Builder/Reject.php b/src/Plugin/Filtering/Builder/Reject.php index 5fe9bdd0..47b5f03f 100644 --- a/src/Plugin/Filtering/Builder/Reject.php +++ b/src/Plugin/Filtering/Builder/Reject.php @@ -7,7 +7,6 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\RejectionResultBucket; use Kiboko\Component\Bucket\RejectionWithReasonResultBucket; -use Kiboko\Component\Satellite\Plugin\Filtering\DTO\Exclusion; use Kiboko\Contract\Configurator\StepBuilderInterface; use PhpParser\Builder; use PhpParser\Node; @@ -17,8 +16,7 @@ final class Reject implements StepBuilderInterface private ?Node\Expr $logger = null; private ?Node\Expr $rejection = null; private ?Node\Expr $state = null; - /** @var list */ - private array $exclusions = []; + private ?ExclusionsBuilder $exclusions = null; public function withLogger(Node\Expr $logger): self { @@ -41,48 +39,13 @@ public function withState(Node\Expr $state): self return $this; } - public function withExclusions(Exclusion ...$exclusions): self + public function withExclusions(ExclusionsBuilder $builder): self { - array_push($this->exclusions, ...$exclusions); + $this->exclusions = $builder; return $this; } - private function buildExclusions(Exclusion ...$exclusions): array - { - $statements = []; - foreach ($exclusions as $exclusion) { - $statements[] = new Node\Stmt\If_( - $exclusion->when, - [ - 'stmts' => [ - new Node\Stmt\Expression( - new Node\Expr\Assign( - new Node\Expr\Variable('input'), - new Node\Expr\Yield_( - new Node\Expr\New_( - $exclusion->reason ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class), - [ - new Node\Arg(new Node\Expr\Variable('input')), - $exclusion->reason ? new Node\Arg($exclusion->reason) : new Node\Arg( - new Node\Expr\ConstFetch( - new Node\Name(null) - ), - ), - ] - ), - ), - ), - ), - new Node\Stmt\Continue_(), - ], - ] - ); - } - - return $statements; - } - public function getNode(): Node { return new Node\Expr\New_( @@ -106,7 +69,7 @@ class: new Node\Stmt\Class_(null, [ new Node\Name('true'), ), [ - ...$this->buildExclusions(...$this->exclusions), + ...$this->exclusions->getNode(), new Node\Stmt\Expression( new Node\Expr\Assign( new Node\Expr\Variable('input'), diff --git a/src/Plugin/Filtering/DTO/Exclusion.php b/src/Plugin/Filtering/DTO/Exclusion.php deleted file mode 100644 index 7dfc1af5..00000000 --- a/src/Plugin/Filtering/DTO/Exclusion.php +++ /dev/null @@ -1,15 +0,0 @@ -withExclusions( - new Filtering\DTO\Exclusion( + $exclusionBuilder + ->withCondition( compileExpression($interpreter, $condition['when']), compileValueWhenExpression($interpreter, $condition['reason']) ?: null, - ), - ); + ); } + $builder->withExclusions($exclusionBuilder); return $repository; }