Skip to content

Commit cdd5e47

Browse files
committed
Created a new builder to manage exclusions
1 parent 1d3c9f5 commit cdd5e47

File tree

4 files changed

+70
-60
lines changed

4 files changed

+70
-60
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Satellite\Plugin\Filtering\Builder;
6+
7+
use Kiboko\Component\Bucket\RejectionResultBucket;
8+
use Kiboko\Component\Bucket\RejectionWithReasonResultBucket;
9+
use PhpParser\Builder;
10+
use PhpParser\Node;
11+
12+
final class ExclusionsBuilder extends Builder
13+
{
14+
/** @var list<list<Node\Expr>> */
15+
private array $exclusions = [];
16+
17+
public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):self
18+
{
19+
$this->exclusions[] = [
20+
'condition' => $condition,
21+
'reason' => $reason,
22+
];
23+
24+
return $this;
25+
}
26+
27+
public function getNode(): array
28+
{
29+
$statements = [];
30+
foreach ($this->exclusions as $exclusion) {
31+
$statements[] = new Node\Stmt\If_(
32+
$exclusion['condition'],
33+
[
34+
'stmts' => [
35+
new Node\Stmt\Expression(
36+
new Node\Expr\Assign(
37+
new Node\Expr\Variable('input'),
38+
new Node\Expr\Yield_(
39+
new Node\Expr\New_(
40+
\array_key_exists('reason', $exclusion) ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class),
41+
[
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+
),
47+
),
48+
]
49+
),
50+
),
51+
),
52+
),
53+
new Node\Stmt\Continue_(),
54+
],
55+
]
56+
);
57+
}
58+
59+
return $statements;
60+
}
61+
}

src/Plugin/Filtering/Builder/Reject.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Kiboko\Component\Bucket\AcceptanceResultBucket;
88
use Kiboko\Component\Bucket\RejectionResultBucket;
99
use Kiboko\Component\Bucket\RejectionWithReasonResultBucket;
10-
use Kiboko\Component\Satellite\Plugin\Filtering\DTO\Exclusion;
1110
use Kiboko\Contract\Configurator\StepBuilderInterface;
1211
use PhpParser\Builder;
1312
use PhpParser\Node;
@@ -17,8 +16,7 @@ final class Reject implements StepBuilderInterface
1716
private ?Node\Expr $logger = null;
1817
private ?Node\Expr $rejection = null;
1918
private ?Node\Expr $state = null;
20-
/** @var list<?Exclusion> */
21-
private array $exclusions = [];
19+
private ?ExclusionsBuilder $exclusions = null;
2220

2321
public function withLogger(Node\Expr $logger): self
2422
{
@@ -41,48 +39,13 @@ public function withState(Node\Expr $state): self
4139
return $this;
4240
}
4341

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

4846
return $this;
4947
}
5048

51-
private function buildExclusions(Exclusion ...$exclusions): array
52-
{
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-
]
80-
);
81-
}
82-
83-
return $statements;
84-
}
85-
8649
public function getNode(): Node
8750
{
8851
return new Node\Expr\New_(
@@ -106,7 +69,7 @@ class: new Node\Stmt\Class_(null, [
10669
new Node\Name('true'),
10770
),
10871
[
109-
...$this->buildExclusions(...$this->exclusions),
72+
...$this->exclusions->getNode(),
11073
new Node\Stmt\Expression(
11174
new Node\Expr\Assign(
11275
new Node\Expr\Variable('input'),

src/Plugin/Filtering/DTO/Exclusion.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Plugin/Filtering/Factory/Reject.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ public function compile(array $config): Filtering\Factory\Repository\Reject
6868

6969
$repository = new Filtering\Factory\Repository\Reject($builder);
7070

71+
$exclusionBuilder = new Filtering\Builder\ExclusionsBuilder();
7172
foreach ($config as $condition) {
72-
$builder->withExclusions(
73-
new Filtering\DTO\Exclusion(
73+
$exclusionBuilder
74+
->withCondition(
7475
compileExpression($interpreter, $condition['when']),
7576
compileValueWhenExpression($interpreter, $condition['reason']) ?: null,
76-
),
77-
);
77+
);
7878
}
79+
$builder->withExclusions($exclusionBuilder);
7980

8081
return $repository;
8182
}

0 commit comments

Comments
 (0)