Skip to content

Commit 1d3c9f5

Browse files
committed
Added a way to add reason rejection in the filtering plugin configuration
1 parent 589bc0a commit 1d3c9f5

File tree

4 files changed

+56
-66
lines changed

4 files changed

+56
-66
lines changed

src/Plugin/Filtering/Builder/Reject.php

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
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;
10+
use Kiboko\Component\Satellite\Plugin\Filtering\DTO\Exclusion;
711
use Kiboko\Contract\Configurator\StepBuilderInterface;
812
use PhpParser\Builder;
913
use 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'),

src/Plugin/Filtering/Configuration/Reject.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public function getConfigTreeBuilder(): TreeBuilder
2929
->end()
3030
->end()
3131
->scalarNode('reason')
32-
->isRequired()
33-
->cannotBeEmpty()
3432
->validate()
3533
->ifTrue(isExpression())
3634
->then(asExpression())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Satellite\Plugin\Filtering\DTO;
6+
7+
use PhpParser\Node\Expr;
8+
9+
class Exclusion
10+
{
11+
public function __construct(
12+
public Expr $when,
13+
public ?Expr $reason = null
14+
){}
15+
}

src/Plugin/Filtering/Factory/Reject.php

Lines changed: 5 additions & 1 deletion
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 Reject implements Configurator\FactoryInterface
1920
{
@@ -69,7 +70,10 @@ public function compile(array $config): Filtering\Factory\Repository\Reject
6970

7071
foreach ($config as $condition) {
7172
$builder->withExclusions(
72-
compileExpression($interpreter, $condition['when'])
73+
new Filtering\DTO\Exclusion(
74+
compileExpression($interpreter, $condition['when']),
75+
compileValueWhenExpression($interpreter, $condition['reason']) ?: null,
76+
),
7377
);
7478
}
7579

0 commit comments

Comments
 (0)