Skip to content

Commit ff51a50

Browse files
committed
Added a way to add a reason to filtering plugin rejects or drops
1 parent 354388c commit ff51a50

File tree

8 files changed

+49
-112
lines changed

8 files changed

+49
-112
lines changed

src/Plugin/Filtering/Builder/Drop.php

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ final class Drop implements StepBuilderInterface
1616
/** @var list<?Node\Expr> */
1717
private array $exclusions = [];
1818

19-
public function __construct()
20-
{
21-
}
22-
2319
public function withLogger(Node\Expr $logger): self
2420
{
2521
$this->logger = $logger;
@@ -41,55 +37,13 @@ public function withState(Node\Expr $state): self
4137
return $this;
4238
}
4339

44-
public function withExclusions(Node\Expr ...$exclusions): self
40+
public function withExclusions(Node ...$exclusions): self
4541
{
4642
array_push($this->exclusions, ...$exclusions);
4743

4844
return $this;
4945
}
5046

51-
private function buildExclusions(Node\Expr ...$exclusions): Node\Expr
52-
{
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,
81-
);
82-
}
83-
84-
if (\count($exclusions) > 0) {
85-
return array_pop($exclusions);
86-
}
87-
88-
return new Node\Expr\ConstFetch(
89-
new Node\Name('false'),
90-
);
91-
}
92-
9347
public function getNode(): Node
9448
{
9549
return new Node\Expr\New_(
@@ -113,24 +67,7 @@ class: new Node\Stmt\Class_(null, [
11367
new Node\Name('true'),
11468
),
11569
[
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-
),
128-
),
129-
),
130-
new Node\Stmt\Continue_(),
131-
],
132-
]
133-
),
70+
...$this->exclusions,
13471
new Node\Stmt\Expression(
13572
new Node\Expr\Assign(
13673
new Node\Expr\Variable('input'),
@@ -146,11 +83,6 @@ class: new Node\Stmt\Class_(null, [
14683
),
14784
],
14885
),
149-
new Node\Stmt\Expression(
150-
new Node\Expr\Yield_(
151-
new Node\Expr\Variable('input')
152-
),
153-
),
15486
])
15587
->getNode(),
15688
],

src/Plugin/Filtering/Builder/ExclusionsBuilder.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
namespace Kiboko\Component\Satellite\Plugin\Filtering\Builder;
66

7-
use Kiboko\Component\Bucket\RejectionResultBucket;
8-
use Kiboko\Component\Bucket\RejectionWithReasonResultBucket;
9-
use PhpParser\Builder;
107
use PhpParser\Node;
118

12-
final class ExclusionsBuilder implements Builder
9+
final class ExclusionsBuilder
1310
{
1411
/** @var list<list<Node\Expr>> */
1512
private array $exclusions = [];
1613

17-
public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):self
14+
public function withCondition(Node\Expr $condition, ?Node\Expr $reason): self
1815
{
1916
$this->exclusions[] = [
2017
'condition' => $condition,
@@ -24,11 +21,10 @@ public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):s
2421
return $this;
2522
}
2623

27-
public function getNode(): Node
24+
public function build(): \Generator
2825
{
29-
$statements = [];
3026
foreach ($this->exclusions as $exclusion) {
31-
$statements[] = new Node\Stmt\If_(
27+
yield new Node\Stmt\If_(
3228
$exclusion['condition'],
3329
[
3430
'stmts' => [
@@ -37,13 +33,15 @@ public function getNode(): Node
3733
new Node\Expr\Variable('input'),
3834
new Node\Expr\Yield_(
3935
new Node\Expr\New_(
40-
\array_key_exists('reason', $exclusion) ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class),
36+
new Node\Name\FullyQualified('Kiboko\\Component\\Bucket\\RejectionResultBucket'),
4137
[
42-
new Node\Arg(new Node\Expr\Variable('input')),
43-
\array_key_exists('reason', $exclusion) ? new Node\Arg($exclusion['reason']) : new Node\Arg(
44-
new Node\Expr\ConstFetch(
45-
new Node\Name(null)
46-
),
38+
new Node\Arg(
39+
value: $exclusion['reason'],
40+
name: new Node\Identifier('reason')
41+
),
42+
new Node\Arg(
43+
value: new Node\Expr\Variable('input'),
44+
name: new Node\Identifier('values')
4745
),
4846
]
4947
),
@@ -55,7 +53,5 @@ public function getNode(): Node
5553
]
5654
);
5755
}
58-
59-
return new Node;
6056
}
6157
}

src/Plugin/Filtering/Builder/Reject.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace 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;
107
use Kiboko\Contract\Configurator\StepBuilderInterface;
118
use PhpParser\Builder;
129
use PhpParser\Node;
@@ -16,7 +13,8 @@ final class Reject implements StepBuilderInterface
1613
private ?Node\Expr $logger = null;
1714
private ?Node\Expr $rejection = null;
1815
private ?Node\Expr $state = null;
19-
private ?ExclusionsBuilder $exclusions = null;
16+
/** @var list<?Node\Expr> */
17+
private array $exclusions = [];
2018

2119
public function withLogger(Node\Expr $logger): self
2220
{
@@ -39,9 +37,9 @@ public function withState(Node\Expr $state): self
3937
return $this;
4038
}
4139

42-
public function withExclusions(ExclusionsBuilder $builder): self
40+
public function withExclusions(Node ...$exclusions): self
4341
{
44-
$this->exclusions = $builder;
42+
array_push($this->exclusions, ...$exclusions);
4543

4644
return $this;
4745
}
@@ -69,7 +67,7 @@ class: new Node\Stmt\Class_(null, [
6967
new Node\Name('true'),
7068
),
7169
[
72-
...$this->exclusions->getNode(),
70+
...$this->exclusions,
7371
new Node\Stmt\Expression(
7472
new Node\Expr\Assign(
7573
new Node\Expr\Variable('input'),
@@ -85,11 +83,6 @@ class: new Node\Stmt\Class_(null, [
8583
),
8684
],
8785
),
88-
new Node\Stmt\Expression(
89-
new Node\Expr\Yield_(
90-
new Node\Expr\Variable('input')
91-
),
92-
),
9386
])
9487
->getNode(),
9588
],

src/Plugin/Filtering/Configuration/Drop.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function getConfigTreeBuilder(): TreeBuilder
2828
->then(asExpression())
2929
->end()
3030
->end()
31+
->scalarNode('reason')
32+
->cannotBeEmpty()
33+
->validate()
34+
->ifTrue(isExpression())
35+
->then(asExpression())
36+
->end()
37+
->end()
3138
->end()
3239
->end()
3340
;

src/Plugin/Filtering/Configuration/Reject.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2929
->end()
3030
->end()
3131
->scalarNode('reason')
32+
->cannotBeEmpty()
3233
->validate()
3334
->ifTrue(isExpression())
3435
->then(asExpression())

src/Plugin/Filtering/Factory/Drop.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1515

1616
use function Kiboko\Component\SatelliteToolbox\Configuration\compileExpression;
17+
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;
1718

1819
class Drop implements Configurator\FactoryInterface
1920
{
@@ -59,20 +60,26 @@ public function validate(array $config): bool
5960
/**
6061
* @throws Configurator\ConfigurationExceptionInterface
6162
*/
62-
public function compile(array $config): Filtering\Factory\Repository\Drop
63+
public function compile(array $config): Repository\Drop
6364
{
6465
$interpreter = clone $this->interpreter;
6566

6667
$builder = new Filtering\Builder\Drop();
6768

68-
$repository = new Filtering\Factory\Repository\Drop($builder);
69+
$repository = new Repository\Drop($builder);
6970

71+
$exclusionBuilder = new Filtering\Builder\ExclusionsBuilder();
7072
foreach ($config as $condition) {
71-
$builder->withExclusions(
72-
compileExpression($interpreter, $condition['when'])
73-
);
73+
$exclusionBuilder
74+
->withCondition(
75+
compileExpression($interpreter, $condition['when']),
76+
compileValueWhenExpression($interpreter, $condition['reason']),
77+
)
78+
;
7479
}
7580

81+
$builder->withExclusions(...$exclusionBuilder->build());
82+
7683
return $repository;
7784
}
7885
}

src/Plugin/Filtering/Factory/Reject.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,25 @@ public function validate(array $config): bool
6060
/**
6161
* @throws Configurator\ConfigurationExceptionInterface
6262
*/
63-
public function compile(array $config): Filtering\Factory\Repository\Reject
63+
public function compile(array $config): Repository\Reject
6464
{
6565
$interpreter = clone $this->interpreter;
6666

6767
$builder = new Filtering\Builder\Reject();
6868

69-
$repository = new Filtering\Factory\Repository\Reject($builder);
69+
$repository = new Repository\Reject($builder);
7070

7171
$exclusionBuilder = new Filtering\Builder\ExclusionsBuilder();
7272
foreach ($config as $condition) {
7373
$exclusionBuilder
7474
->withCondition(
7575
compileExpression($interpreter, $condition['when']),
76-
\array_key_exists('reason', $condition) ? compileValueWhenExpression($interpreter, $condition['reason']) : null,
77-
);
76+
compileValueWhenExpression($interpreter, $condition['reason']),
77+
)
78+
;
7879
}
79-
$builder->withExclusions($exclusionBuilder);
80+
81+
$builder->withExclusions(...$exclusionBuilder->build());
8082

8183
return $repository;
8284
}

src/Plugin/Filtering/Service.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Kiboko\Component\Satellite\Plugin\Filtering;
66

77
use Kiboko\Component\Satellite\ExpressionLanguage as Satellite;
8-
use Kiboko\Component\Satellite\Plugin\Filtering;
98
use Kiboko\Contract\Configurator;
109
use Symfony\Component\Config\Definition\Exception as Symfony;
1110
use Symfony\Component\Config\Definition\Processor;
@@ -26,7 +25,7 @@ public function __construct(
2625
private ExpressionLanguage $interpreter = new Satellite\ExpressionLanguage()
2726
) {
2827
$this->processor = new Processor();
29-
$this->configuration = new Filtering\Configuration();
28+
$this->configuration = new Configuration();
3029
}
3130

3231
public function interpreter(): ExpressionLanguage
@@ -79,11 +78,11 @@ public function compile(array $config): Configurator\RepositoryInterface
7978
}
8079

8180
if (\array_key_exists('reject', $config)) {
82-
return (new Filtering\Factory\Reject($interpreter, $config['expression_language'] ?? []))->compile($config['reject']);
81+
return (new Factory\Reject($interpreter, $config['expression_language'] ?? []))->compile($config['reject']);
8382
}
8483

8584
if (\array_key_exists('drop', $config)) {
86-
return (new Filtering\Factory\Drop($interpreter, $config['expression_language'] ?? []))->compile($config['drop']);
85+
return (new Factory\Drop($interpreter, $config['expression_language'] ?? []))->compile($config['drop']);
8786
}
8887

8988
throw new \RuntimeException('No possible pipeline step, expecting "extractor", "transformer" or "loader".');

0 commit comments

Comments
 (0)