Skip to content

Commit

Permalink
Merge branch 'feature/reject-with-reason' into feature/add-data-to-fo…
Browse files Browse the repository at this point in the history
…rmat-key-management

# Conflicts:
#	src/Plugin/Filtering/Builder/Reject.php
#	src/Plugin/Filtering/Configuration/Reject.php
  • Loading branch information
JoMessina committed Oct 30, 2023
2 parents 299ee87 + d58d507 commit c2e0bce
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 62 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 @@ -17,11 +19,9 @@ final class Reject implements StepBuilderInterface
private ?Node\Expr $rejection = null;
private ?Node\Expr $state = null;
private ?Node\Expr $rejection_serializer = 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 Down Expand Up @@ -50,53 +50,46 @@ public function withRejectionSerializer(Node\Expr $rejection_serializer): 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),
[
null !== $this->rejection_serializer ? new Node\Arg($this->rejection_serializer) : 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 @@ -122,27 +115,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),
[
null !== $this->rejection_serializer ? new Node\Arg($this->rejection_serializer) : 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
6 changes: 6 additions & 0 deletions src/Plugin/Filtering/Configuration/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->then(asExpression())
->end()
->end()
->scalarNode('reason')
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->scalarNode('rejection_serializer')
->cannotBeEmpty()
->validate()
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,
),
);
if (\array_key_exists('rejection_serializer', $condition)) {
$builder->withRejectionSerializer(compileExpression($interpreter, $condition['rejection_serializer']));
Expand Down

0 comments on commit c2e0bce

Please sign in to comment.