Skip to content

Commit

Permalink
Added a way to add reason rejection in the filtering plugin configura…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
sebprt committed Oct 20, 2023
1 parent 59de5a4 commit d58d507
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 64 deletions.
95 changes: 34 additions & 61 deletions src/Plugin/Filtering/Builder/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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 Kiboko\Contract\Pipeline\TransformerInterface;
use PhpParser\Builder;
Expand All @@ -16,11 +18,9 @@ final class Reject implements StepBuilderInterface
private ?Node\Expr $logger = null;
private ?Node\Expr $rejection = null;
private ?Node\Expr $state = null;
/** @var list<?Node\Expr> */
/** @var list<?Exclusion> */
private array $exclusions = [];

public function __construct() {}

public function withLogger(Node\Expr $logger): self
{
$this->logger = $logger;
Expand All @@ -42,53 +42,46 @@ public function withState(Node\Expr $state): self
return $this;
}

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

return $this;
}

private function buildExclusions(Node\Expr ...$exclusions): Node\Expr
private function buildExclusions(Exclusion ...$exclusions): array
{
if (\count($exclusions) > 3) {
$length = \count($exclusions);
$middle = (int) floor($length / 2);
$left = \array_slice($exclusions, 0, $middle);
$right = \array_slice($exclusions, $middle, $length);

return new Node\Expr\BinaryOp\BooleanAnd(
$this->buildExclusions(...$left),
$this->buildExclusions(...$right),
);
}

if (\count($exclusions) > 2) {
$right = array_shift($exclusions);

return new Node\Expr\BinaryOp\BooleanAnd(
$this->buildExclusions(...$exclusions),
$right,
);
}

if (\count($exclusions) > 1) {
$left = array_pop($exclusions);
$right = array_pop($exclusions);

return new Node\Expr\BinaryOp\BooleanAnd(
$left,
$right,
$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),

Check failure on line 65 in src/Plugin/Filtering/Builder/Reject.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Kiboko\Component\Bucket\RejectionWithReasonResultBucket not found.
[
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_(),
],
]
);
}

if (\count($exclusions) > 0) {
return array_pop($exclusions);
}

return new Node\Expr\ConstFetch(
new Node\Name('false'),
);
return $statements;
}

public function getNode(): Node
Expand All @@ -114,27 +107,7 @@ class: new Node\Stmt\Class_(null, [
new Node\Name('true'),
),
[
new Node\Stmt\If_(
$this->buildExclusions(...$this->exclusions),
[
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('input'),
new Node\Expr\Yield_(
new Node\Expr\New_(
new Node\Name\FullyQualified(RejectionResultBucket::class),
[
new Node\Arg(new Node\Expr\Variable('input')),
]
),
),
),
),
new Node\Stmt\Continue_(),
],
]
),
...$this->buildExclusions(...$this->exclusions),
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('input'),
Expand Down
2 changes: 0 additions & 2 deletions src/Plugin/Filtering/Configuration/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->scalarNode('reason')
->isRequired()
->cannotBeEmpty()
->validate()
->ifTrue(isExpression())
->then(asExpression())
Expand Down
15 changes: 15 additions & 0 deletions src/Plugin/Filtering/DTO/Exclusion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

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

use PhpParser\Node\Expr;

class Exclusion
{
public function __construct(
public Expr $when,
public ?Expr $reason = null
){}
}
6 changes: 5 additions & 1 deletion src/Plugin/Filtering/Factory/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

use function Kiboko\Component\SatelliteToolbox\Configuration\compileExpression;
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;

class Reject implements Configurator\FactoryInterface
{
Expand Down Expand Up @@ -69,7 +70,10 @@ public function compile(array $config): Repository\Reject

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

Expand Down

0 comments on commit d58d507

Please sign in to comment.