Skip to content

Commit

Permalink
Created a new builder to manage exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Jun 25, 2024
1 parent 1d3c9f5 commit cdd5e47
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 60 deletions.
61 changes: 61 additions & 0 deletions src/Plugin/Filtering/Builder/ExclusionsBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

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

use Kiboko\Component\Bucket\RejectionResultBucket;
use Kiboko\Component\Bucket\RejectionWithReasonResultBucket;
use PhpParser\Builder;
use PhpParser\Node;

final class ExclusionsBuilder extends Builder
{
/** @var list<list<Node\Expr>> */
private array $exclusions = [];

public function withCondition(Node\Expr $condition, ?Node\Expr $reason = null):self
{
$this->exclusions[] = [
'condition' => $condition,
'reason' => $reason,
];

return $this;
}

public function getNode(): array
{
$statements = [];
foreach ($this->exclusions as $exclusion) {
$statements[] = new Node\Stmt\If_(
$exclusion['condition'],
[
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('input'),
new Node\Expr\Yield_(
new Node\Expr\New_(
\array_key_exists('reason', $exclusion) ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class),
[
new Node\Arg(new Node\Expr\Variable('input')),
\array_key_exists('reason', $exclusion) ? new Node\Arg($exclusion['reason']) : new Node\Arg(
new Node\Expr\ConstFetch(
new Node\Name(null)
),
),
]
),
),
),
),
new Node\Stmt\Continue_(),
],
]
);
}

return $statements;
}
}
45 changes: 4 additions & 41 deletions src/Plugin/Filtering/Builder/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Kiboko\Component\Bucket\AcceptanceResultBucket;
use Kiboko\Component\Bucket\RejectionResultBucket;
use Kiboko\Component\Bucket\RejectionWithReasonResultBucket;
use Kiboko\Component\Satellite\Plugin\Filtering\DTO\Exclusion;
use Kiboko\Contract\Configurator\StepBuilderInterface;
use PhpParser\Builder;
use PhpParser\Node;
Expand All @@ -17,8 +16,7 @@ final class Reject implements StepBuilderInterface
private ?Node\Expr $logger = null;
private ?Node\Expr $rejection = null;
private ?Node\Expr $state = null;
/** @var list<?Exclusion> */
private array $exclusions = [];
private ?ExclusionsBuilder $exclusions = null;

public function withLogger(Node\Expr $logger): self
{
Expand All @@ -41,48 +39,13 @@ public function withState(Node\Expr $state): self
return $this;
}

public function withExclusions(Exclusion ...$exclusions): self
public function withExclusions(ExclusionsBuilder $builder): self
{
array_push($this->exclusions, ...$exclusions);
$this->exclusions = $builder;

return $this;
}

private function buildExclusions(Exclusion ...$exclusions): array
{
$statements = [];
foreach ($exclusions as $exclusion) {
$statements[] = new Node\Stmt\If_(
$exclusion->when,
[
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('input'),
new Node\Expr\Yield_(
new Node\Expr\New_(
$exclusion->reason ? new Node\Name\FullyQualified(RejectionWithReasonResultBucket::class) : new Node\Name\FullyQualified(RejectionResultBucket::class),
[
new Node\Arg(new Node\Expr\Variable('input')),
$exclusion->reason ? new Node\Arg($exclusion->reason) : new Node\Arg(
new Node\Expr\ConstFetch(
new Node\Name(null)
),
),
]
),
),
),
),
new Node\Stmt\Continue_(),
],
]
);
}

return $statements;
}

public function getNode(): Node
{
return new Node\Expr\New_(
Expand All @@ -106,7 +69,7 @@ class: new Node\Stmt\Class_(null, [
new Node\Name('true'),
),
[
...$this->buildExclusions(...$this->exclusions),
...$this->exclusions->getNode(),
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('input'),
Expand Down
15 changes: 0 additions & 15 deletions src/Plugin/Filtering/DTO/Exclusion.php

This file was deleted.

9 changes: 5 additions & 4 deletions src/Plugin/Filtering/Factory/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ public function compile(array $config): Filtering\Factory\Repository\Reject

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

$exclusionBuilder = new Filtering\Builder\ExclusionsBuilder();
foreach ($config as $condition) {
$builder->withExclusions(
new Filtering\DTO\Exclusion(
$exclusionBuilder
->withCondition(
compileExpression($interpreter, $condition['when']),
compileValueWhenExpression($interpreter, $condition['reason']) ?: null,
),
);
);
}
$builder->withExclusions($exclusionBuilder);

return $repository;
}
Expand Down

0 comments on commit cdd5e47

Please sign in to comment.