Skip to content

Commit b06361e

Browse files
committed
Created a new builder to manage exclusions
1 parent d58d507 commit b06361e

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 Kiboko\Contract\Pipeline\TransformerInterface;
1312
use PhpParser\Builder;
@@ -18,8 +17,7 @@ final class Reject implements StepBuilderInterface
1817
private ?Node\Expr $logger = null;
1918
private ?Node\Expr $rejection = null;
2019
private ?Node\Expr $state = null;
21-
/** @var list<?Exclusion> */
22-
private array $exclusions = [];
20+
private ?ExclusionsBuilder $exclusions = null;
2321

2422
public function withLogger(Node\Expr $logger): self
2523
{
@@ -42,48 +40,13 @@ public function withState(Node\Expr $state): self
4240
return $this;
4341
}
4442

45-
public function withExclusions(Exclusion ...$exclusions): self
43+
public function withExclusions(ExclusionsBuilder $builder): self
4644
{
47-
array_push($this->exclusions, ...$exclusions);
45+
$this->exclusions = $builder;
4846

4947
return $this;
5048
}
5149

52-
private function buildExclusions(Exclusion ...$exclusions): array
53-
{
54-
$statements = [];
55-
foreach ($exclusions as $exclusion) {
56-
$statements[] = new Node\Stmt\If_(
57-
$exclusion->when,
58-
[
59-
'stmts' => [
60-
new Node\Stmt\Expression(
61-
new Node\Expr\Assign(
62-
new Node\Expr\Variable('input'),
63-
new Node\Expr\Yield_(
64-
new Node\Expr\New_(
65-
$exclusion->reason ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class),
66-
[
67-
new Node\Arg(new Node\Expr\Variable('input')),
68-
$exclusion->reason ? new Node\Arg($exclusion->reason) : new Node\Arg(
69-
new Node\Expr\ConstFetch(
70-
new Node\Name(null)
71-
),
72-
),
73-
]
74-
),
75-
),
76-
),
77-
),
78-
new Node\Stmt\Continue_(),
79-
],
80-
]
81-
);
82-
}
83-
84-
return $statements;
85-
}
86-
8750
public function getNode(): Node
8851
{
8952
return new Node\Expr\New_(
@@ -107,7 +70,7 @@ class: new Node\Stmt\Class_(null, [
10770
new Node\Name('true'),
10871
),
10972
[
110-
...$this->buildExclusions(...$this->exclusions),
73+
...$this->exclusions->getNode(),
11174
new Node\Stmt\Expression(
11275
new Node\Expr\Assign(
11376
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): Repository\Reject
6868

6969
$repository = new 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)