Skip to content

Commit c2e0bce

Browse files
committed
Merge branch 'feature/reject-with-reason' into feature/add-data-to-format-key-management
# Conflicts: # src/Plugin/Filtering/Builder/Reject.php # src/Plugin/Filtering/Configuration/Reject.php
2 parents 299ee87 + d58d507 commit c2e0bce

File tree

4 files changed

+60
-62
lines changed

4 files changed

+60
-62
lines changed

src/Plugin/Filtering/Builder/Reject.php

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Kiboko\Component\Bucket\AcceptanceResultBucket;
88
use Kiboko\Component\Bucket\RejectionResultBucket;
9+
use Kiboko\Component\Bucket\RejectionWithReasonResultBucket;
10+
use Kiboko\Component\Satellite\Plugin\Filtering\DTO\Exclusion;
911
use Kiboko\Contract\Configurator\StepBuilderInterface;
1012
use Kiboko\Contract\Pipeline\TransformerInterface;
1113
use PhpParser\Builder;
@@ -17,11 +19,9 @@ final class Reject implements StepBuilderInterface
1719
private ?Node\Expr $rejection = null;
1820
private ?Node\Expr $state = null;
1921
private ?Node\Expr $rejection_serializer = null;
20-
/** @var list<?Node\Expr> */
22+
/** @var list<?Exclusion> */
2123
private array $exclusions = [];
2224

23-
public function __construct() {}
24-
2525
public function withLogger(Node\Expr $logger): self
2626
{
2727
$this->logger = $logger;
@@ -50,53 +50,46 @@ public function withRejectionSerializer(Node\Expr $rejection_serializer): self
5050
return $this;
5151
}
5252

53-
public function withExclusions(Node\Expr ...$exclusions): self
53+
public function withExclusions(Exclusion ...$exclusions): self
5454
{
5555
array_push($this->exclusions, ...$exclusions);
5656

5757
return $this;
5858
}
5959

60-
private function buildExclusions(Node\Expr ...$exclusions): Node\Expr
60+
private function buildExclusions(Exclusion ...$exclusions): array
6161
{
62-
if (\count($exclusions) > 3) {
63-
$length = \count($exclusions);
64-
$middle = (int) floor($length / 2);
65-
$left = \array_slice($exclusions, 0, $middle);
66-
$right = \array_slice($exclusions, $middle, $length);
67-
68-
return new Node\Expr\BinaryOp\BooleanAnd(
69-
$this->buildExclusions(...$left),
70-
$this->buildExclusions(...$right),
71-
);
72-
}
73-
74-
if (\count($exclusions) > 2) {
75-
$right = array_shift($exclusions);
76-
77-
return new Node\Expr\BinaryOp\BooleanAnd(
78-
$this->buildExclusions(...$exclusions),
79-
$right,
80-
);
81-
}
82-
83-
if (\count($exclusions) > 1) {
84-
$left = array_pop($exclusions);
85-
$right = array_pop($exclusions);
86-
87-
return new Node\Expr\BinaryOp\BooleanAnd(
88-
$left,
89-
$right,
62+
$statements = [];
63+
foreach ($exclusions as $exclusion) {
64+
$statements[] = new Node\Stmt\If_(
65+
$exclusion->when,
66+
[
67+
'stmts' => [
68+
new Node\Stmt\Expression(
69+
new Node\Expr\Assign(
70+
new Node\Expr\Variable('input'),
71+
new Node\Expr\Yield_(
72+
new Node\Expr\New_(
73+
$exclusion->reason ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class),
74+
[
75+
null !== $this->rejection_serializer ? new Node\Arg($this->rejection_serializer) : new Node\Arg(new Node\Expr\Variable('input')),
76+
$exclusion->reason ? new Node\Arg($exclusion->reason) : new Node\Arg(
77+
new Node\Expr\ConstFetch(
78+
new Node\Name(null)
79+
),
80+
),
81+
]
82+
),
83+
),
84+
),
85+
),
86+
new Node\Stmt\Continue_(),
87+
],
88+
]
9089
);
9190
}
9291

93-
if (\count($exclusions) > 0) {
94-
return array_pop($exclusions);
95-
}
96-
97-
return new Node\Expr\ConstFetch(
98-
new Node\Name('false'),
99-
);
92+
return $statements;
10093
}
10194

10295
public function getNode(): Node
@@ -122,27 +115,7 @@ class: new Node\Stmt\Class_(null, [
122115
new Node\Name('true'),
123116
),
124117
[
125-
new Node\Stmt\If_(
126-
$this->buildExclusions(...$this->exclusions),
127-
[
128-
'stmts' => [
129-
new Node\Stmt\Expression(
130-
new Node\Expr\Assign(
131-
new Node\Expr\Variable('input'),
132-
new Node\Expr\Yield_(
133-
new Node\Expr\New_(
134-
new Node\Name\FullyQualified(RejectionResultBucket::class),
135-
[
136-
null !== $this->rejection_serializer ? new Node\Arg($this->rejection_serializer) : new Node\Arg(new Node\Expr\Variable('input')),
137-
]
138-
),
139-
),
140-
),
141-
),
142-
new Node\Stmt\Continue_(),
143-
],
144-
]
145-
),
118+
...$this->buildExclusions(...$this->exclusions),
146119
new Node\Stmt\Expression(
147120
new Node\Expr\Assign(
148121
new Node\Expr\Variable('input'),

src/Plugin/Filtering/Configuration/Reject.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public function getConfigTreeBuilder(): TreeBuilder
2828
->then(asExpression())
2929
->end()
3030
->end()
31+
->scalarNode('reason')
32+
->validate()
33+
->ifTrue(isExpression())
34+
->then(asExpression())
35+
->end()
36+
->end()
3137
->scalarNode('rejection_serializer')
3238
->cannotBeEmpty()
3339
->validate()
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): 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
if (\array_key_exists('rejection_serializer', $condition)) {
7579
$builder->withRejectionSerializer(compileExpression($interpreter, $condition['rejection_serializer']));

0 commit comments

Comments
 (0)