From b2bab02b1af1b68aa606b1330224ee79cd8eb04f Mon Sep 17 00:00:00 2001 From: sebprt Date: Tue, 25 Jun 2024 12:16:26 +0200 Subject: [PATCH] Added a way to add a reason to filtering plugin rejects or drops --- src/Plugin/Filtering/Builder/Drop.php | 25 +++-------------- .../Filtering/Builder/ExclusionsBuilder.php | 28 ++++++++----------- src/Plugin/Filtering/Builder/Reject.php | 5 +--- src/Plugin/Filtering/Configuration/Drop.php | 7 +++++ src/Plugin/Filtering/Configuration/Reject.php | 1 + src/Plugin/Filtering/Factory/Drop.php | 17 +++++++---- src/Plugin/Filtering/Factory/Reject.php | 9 +++--- src/Plugin/Filtering/Service.php | 7 ++--- 8 files changed, 45 insertions(+), 54 deletions(-) diff --git a/src/Plugin/Filtering/Builder/Drop.php b/src/Plugin/Filtering/Builder/Drop.php index 32ae3f7e..e461ea87 100644 --- a/src/Plugin/Filtering/Builder/Drop.php +++ b/src/Plugin/Filtering/Builder/Drop.php @@ -14,7 +14,7 @@ final class Drop implements StepBuilderInterface private ?Node\Expr $rejection = null; private ?Node\Expr $state = null; /** @var list */ - private array $exclusions = []; + private ?ExclusionsBuilder $exclusions = null; public function __construct() { @@ -41,9 +41,9 @@ public function withState(Node\Expr $state): self return $this; } - public function withExclusions(Node\Expr ...$exclusions): self + public function withExclusions(ExclusionsBuilder $builder): self { - array_push($this->exclusions, ...$exclusions); + $this->exclusions = $builder; return $this; } @@ -113,24 +113,7 @@ class: new Node\Stmt\Class_(null, [ new Node\Name('true'), ), [ - new Node\Stmt\If_( - $this->buildExclusions(...$this->exclusions), - [ - 'stmts' => [ - new Node\Stmt\Expression( - new Node\Expr\Assign( - new Node\Expr\Variable('input'), - new Node\Expr\Yield_( - new Node\Expr\New_( - new Node\Name\FullyQualified('Kiboko\\Component\\Bucket\\RejectionResultBucket'), - ), - ), - ), - ), - new Node\Stmt\Continue_(), - ], - ] - ), + ...$this->exclusions->build(), new Node\Stmt\Expression( new Node\Expr\Assign( new Node\Expr\Variable('input'), diff --git a/src/Plugin/Filtering/Builder/ExclusionsBuilder.php b/src/Plugin/Filtering/Builder/ExclusionsBuilder.php index e7cf1c3b..b5415472 100644 --- a/src/Plugin/Filtering/Builder/ExclusionsBuilder.php +++ b/src/Plugin/Filtering/Builder/ExclusionsBuilder.php @@ -4,17 +4,14 @@ namespace Kiboko\Component\Satellite\Plugin\Filtering\Builder; -use Kiboko\Component\Bucket\RejectionResultBucket; -use Kiboko\Component\Bucket\RejectionWithReasonResultBucket; -use PhpParser\Builder; use PhpParser\Node; -final class ExclusionsBuilder implements Builder +final class ExclusionsBuilder { /** @var list> */ private array $exclusions = []; - public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):self + public function withCondition(Node\Expr $condition, ?Node\Expr $reason): self { $this->exclusions[] = [ 'condition' => $condition, @@ -24,11 +21,10 @@ public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):s return $this; } - public function getNode(): Node + public function build(): \Generator { - $statements = []; foreach ($this->exclusions as $exclusion) { - $statements[] = new Node\Stmt\If_( + yield new Node\Stmt\If_( $exclusion['condition'], [ 'stmts' => [ @@ -37,13 +33,15 @@ public function getNode(): Node 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\Name\FullyQualified('Kiboko\\Component\\Bucket\\RejectionResultBucket'), [ - 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\Arg( + value: $exclusion['reason'], + name: new Node\Identifier('reason') + ), + new Node\Arg( + value: new Node\Expr\Variable('input'), + name: new Node\Identifier('values') ), ] ), @@ -55,7 +53,5 @@ public function getNode(): Node ] ); } - - return new Node; } } diff --git a/src/Plugin/Filtering/Builder/Reject.php b/src/Plugin/Filtering/Builder/Reject.php index 47b5f03f..7f066cd6 100644 --- a/src/Plugin/Filtering/Builder/Reject.php +++ b/src/Plugin/Filtering/Builder/Reject.php @@ -4,9 +4,6 @@ namespace Kiboko\Component\Satellite\Plugin\Filtering\Builder; -use Kiboko\Component\Bucket\AcceptanceResultBucket; -use Kiboko\Component\Bucket\RejectionResultBucket; -use Kiboko\Component\Bucket\RejectionWithReasonResultBucket; use Kiboko\Contract\Configurator\StepBuilderInterface; use PhpParser\Builder; use PhpParser\Node; @@ -69,7 +66,7 @@ class: new Node\Stmt\Class_(null, [ new Node\Name('true'), ), [ - ...$this->exclusions->getNode(), + ...$this->exclusions->build(), new Node\Stmt\Expression( new Node\Expr\Assign( new Node\Expr\Variable('input'), diff --git a/src/Plugin/Filtering/Configuration/Drop.php b/src/Plugin/Filtering/Configuration/Drop.php index 113efd78..23756e3d 100644 --- a/src/Plugin/Filtering/Configuration/Drop.php +++ b/src/Plugin/Filtering/Configuration/Drop.php @@ -28,6 +28,13 @@ public function getConfigTreeBuilder(): TreeBuilder ->then(asExpression()) ->end() ->end() + ->scalarNode('reason') + ->cannotBeEmpty() + ->validate() + ->ifTrue(isExpression()) + ->then(asExpression()) + ->end() + ->end() ->end() ->end() ; diff --git a/src/Plugin/Filtering/Configuration/Reject.php b/src/Plugin/Filtering/Configuration/Reject.php index c52061a8..91cf57b7 100644 --- a/src/Plugin/Filtering/Configuration/Reject.php +++ b/src/Plugin/Filtering/Configuration/Reject.php @@ -29,6 +29,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->scalarNode('reason') + ->cannotBeEmpty() ->validate() ->ifTrue(isExpression()) ->then(asExpression()) diff --git a/src/Plugin/Filtering/Factory/Drop.php b/src/Plugin/Filtering/Factory/Drop.php index 4648a092..53c40219 100644 --- a/src/Plugin/Filtering/Factory/Drop.php +++ b/src/Plugin/Filtering/Factory/Drop.php @@ -14,6 +14,7 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use function Kiboko\Component\SatelliteToolbox\Configuration\compileExpression; +use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression; class Drop implements Configurator\FactoryInterface { @@ -59,20 +60,26 @@ public function validate(array $config): bool /** * @throws Configurator\ConfigurationExceptionInterface */ - public function compile(array $config): Filtering\Factory\Repository\Drop + public function compile(array $config): Repository\Drop { $interpreter = clone $this->interpreter; $builder = new Filtering\Builder\Drop(); - $repository = new Filtering\Factory\Repository\Drop($builder); + $repository = new Repository\Drop($builder); + $exclusionBuilder = new Filtering\Builder\ExclusionsBuilder(); foreach ($config as $condition) { - $builder->withExclusions( - compileExpression($interpreter, $condition['when']) - ); + $exclusionBuilder + ->withCondition( + compileExpression($interpreter, $condition['when']), + compileValueWhenExpression($interpreter, $condition['reason']), + ) + ; } + $builder->withExclusions($exclusionBuilder); + return $repository; } } diff --git a/src/Plugin/Filtering/Factory/Reject.php b/src/Plugin/Filtering/Factory/Reject.php index 91391d6c..631e00ce 100644 --- a/src/Plugin/Filtering/Factory/Reject.php +++ b/src/Plugin/Filtering/Factory/Reject.php @@ -60,21 +60,22 @@ public function validate(array $config): bool /** * @throws Configurator\ConfigurationExceptionInterface */ - public function compile(array $config): Filtering\Factory\Repository\Reject + public function compile(array $config): Repository\Reject { $interpreter = clone $this->interpreter; $builder = new Filtering\Builder\Reject(); - $repository = new Filtering\Factory\Repository\Reject($builder); + $repository = new Repository\Reject($builder); $exclusionBuilder = new Filtering\Builder\ExclusionsBuilder(); foreach ($config as $condition) { $exclusionBuilder ->withCondition( compileExpression($interpreter, $condition['when']), - \array_key_exists('reason', $condition) ? compileValueWhenExpression($interpreter, $condition['reason']) : null, - ); + compileValueWhenExpression($interpreter, $condition['reason']), + ) + ; } $builder->withExclusions($exclusionBuilder); diff --git a/src/Plugin/Filtering/Service.php b/src/Plugin/Filtering/Service.php index 364773a1..525ef830 100644 --- a/src/Plugin/Filtering/Service.php +++ b/src/Plugin/Filtering/Service.php @@ -5,7 +5,6 @@ namespace Kiboko\Component\Satellite\Plugin\Filtering; use Kiboko\Component\Satellite\ExpressionLanguage as Satellite; -use Kiboko\Component\Satellite\Plugin\Filtering; use Kiboko\Contract\Configurator; use Symfony\Component\Config\Definition\Exception as Symfony; use Symfony\Component\Config\Definition\Processor; @@ -26,7 +25,7 @@ public function __construct( private ExpressionLanguage $interpreter = new Satellite\ExpressionLanguage() ) { $this->processor = new Processor(); - $this->configuration = new Filtering\Configuration(); + $this->configuration = new Configuration(); } public function interpreter(): ExpressionLanguage @@ -79,11 +78,11 @@ public function compile(array $config): Configurator\RepositoryInterface } if (\array_key_exists('reject', $config)) { - return (new Filtering\Factory\Reject($interpreter, $config['expression_language'] ?? []))->compile($config['reject']); + return (new Factory\Reject($interpreter, $config['expression_language'] ?? []))->compile($config['reject']); } if (\array_key_exists('drop', $config)) { - return (new Filtering\Factory\Drop($interpreter, $config['expression_language'] ?? []))->compile($config['drop']); + return (new Factory\Drop($interpreter, $config['expression_language'] ?? []))->compile($config['drop']); } throw new \RuntimeException('No possible pipeline step, expecting "extractor", "transformer" or "loader".');