Skip to content

Commit

Permalink
Added a way to add a reason to filtering plugin rejects or drops
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Jun 25, 2024
1 parent 354388c commit b2bab02
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 54 deletions.
25 changes: 4 additions & 21 deletions src/Plugin/Filtering/Builder/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class Drop implements StepBuilderInterface
private ?Node\Expr $rejection = null;
private ?Node\Expr $state = null;
/** @var list<?Node\Expr> */
private array $exclusions = [];
private ?ExclusionsBuilder $exclusions = null;

Check failure on line 17 in src/Plugin/Filtering/Builder/Drop.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @var for property Kiboko\Component\Satellite\Plugin\Filtering\Builder\Drop::$exclusions with type array<int, PhpParser\Node\Expr|null> is incompatible with native type Kiboko\Component\Satellite\Plugin\Filtering\Builder\ExclusionsBuilder|null.

public function __construct()
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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'),
Expand Down
28 changes: 12 additions & 16 deletions src/Plugin/Filtering/Builder/ExclusionsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<list<Node\Expr>> */
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,
Expand All @@ -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'],

Check failure on line 28 in src/Plugin/Filtering/Builder/ExclusionsBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Offset 'condition' does not exist on array<int, PhpParser\Node\Expr>.
[
'stmts' => [
Expand All @@ -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'],

Check failure on line 39 in src/Plugin/Filtering/Builder/ExclusionsBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Offset 'reason' does not exist on array<int, PhpParser\Node\Expr>.
name: new Node\Identifier('reason')
),
new Node\Arg(
value: new Node\Expr\Variable('input'),
name: new Node\Identifier('values')
),
]
),
Expand All @@ -55,7 +53,5 @@ public function getNode(): Node
]
);
}

return new Node;
}
}
5 changes: 1 addition & 4 deletions src/Plugin/Filtering/Builder/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
Expand Down
7 changes: 7 additions & 0 deletions src/Plugin/Filtering/Configuration/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->then(asExpression())
->end()
->end()
->scalarNode('reason')
->cannotBeEmpty()
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->end()
->end()
;
Expand Down
1 change: 1 addition & 0 deletions src/Plugin/Filtering/Configuration/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->scalarNode('reason')
->cannotBeEmpty()
->validate()
->ifTrue(isExpression())
->then(asExpression())
Expand Down
17 changes: 12 additions & 5 deletions src/Plugin/Filtering/Factory/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
9 changes: 5 additions & 4 deletions src/Plugin/Filtering/Factory/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 3 additions & 4 deletions src/Plugin/Filtering/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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".');
Expand Down

0 comments on commit b2bab02

Please sign in to comment.