Skip to content

Commit

Permalink
catch RejectedItemExceptions and log them instead of stopping the pip…
Browse files Browse the repository at this point in the history
…eline
  • Loading branch information
clemzarch committed Aug 29, 2023
1 parent 475bc14 commit 0a9d785
Showing 1 changed file with 110 additions and 31 deletions.
141 changes: 110 additions & 31 deletions src/Builder/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
use PhpParser\Builder;
use PhpParser\Node;

final readonly class Transformer implements StepBuilderInterface
final class Transformer implements StepBuilderInterface
{
public function __construct(private Builder|Node $mapper)
private ?Node\Expr $logger = null;

public function __construct(private readonly Builder|Node $mapper)
{
}

public function withLogger(Node\Expr $logger): self
{
$this->logger = $logger;

return $this;
}

Expand Down Expand Up @@ -54,6 +58,15 @@ class: new Node\Stmt\Class_(
),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE
),
new Node\Param(
var: new Node\Expr\Variable(
name: 'logger'
),
type: new Node\Name\FullyQualified(
name: \Psr\Log\LoggerInterface::class
),
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
),
],
],
),
Expand All @@ -64,45 +77,108 @@ class: new Node\Stmt\Class_(
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
var: new Node\Expr\Variable('input'),
expr: new Node\Expr\Yield_(null)
),
),
new Node\Stmt\Do_(
cond: new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\Yield_(
new Node\Expr\New_(
class: new Node\Name\FullyQualified(
\Kiboko\Component\Bucket\AcceptanceResultBucket::class
),
args: [
new Node\Arg(
new Node\Expr\Variable('line'),
),
],
)
new Node\Stmt\While_(
cond: new Node\Expr\BinaryOp\NotIdentical(
left: new Node\Expr\Variable('input'),
right: new Node\Expr\ConstFetch(
new Node\Name('null')
)
),
stmts: [
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\FuncCall(
name: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('mapper'),
new Node\Stmt\TryCatch(
stmts: [
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\FuncCall(
name: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('mapper'),
),
args: [
new Node\Arg(
value: new Node\Expr\Variable('input')
),
new Node\Arg(
value: new Node\Expr\Variable('input')
),
]
),
),
args: [
new Node\Arg(
value: new Node\Expr\Variable('line')
),
],
catches: [
new Node\Stmt\Catch_(
types: [
new Node\Name\FullyQualified('Kiboko\\Contract\\Pipeline\\RejectedItemException'),
],
var: new Node\Expr\Variable('exception'),
stmts: [
new Node\Stmt\Expression(
new Node\Expr\MethodCall(
var: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('logger'),
),
name: new Node\Name('error'),
args: [
new Node\Arg(
new Node\Expr\MethodCall(
var: new Node\Expr\Variable('exception'),
name: new Node\Identifier('getMessage')
)
),
new Node\Expr\Array_([
new Node\Expr\ArrayItem(
value: new Node\Expr\Variable('input'),
key: new Node\Scalar\String_('input')
),
]),
]
)
),
new Node\Arg(
value: new Node\Expr\Variable('line')
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('input'),
expr: new Node\Expr\Yield_(
new Node\Expr\New_(
class: new Node\Name\FullyQualified(
\Kiboko\Component\Bucket\RejectionResultBucket::class
),
args: [
new Node\Arg(
new Node\Expr\Variable('input'),
),
],
)
)
),
),
new Node\Stmt\Continue_(),
]
),
),
]
),
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('input'),
expr: new Node\Expr\Yield_(
new Node\Expr\New_(
class: new Node\Name\FullyQualified(
\Kiboko\Component\Bucket\AcceptanceResultBucket::class
),
args: [
new Node\Arg(
new Node\Expr\Variable('line'),
),
],
)
)
)
),
],
),
Expand All @@ -114,7 +190,7 @@ class: new Node\Name\FullyQualified(
),
args: [
new Node\Arg(
new Node\Expr\Variable('line'),
new Node\Expr\Variable('input'),
),
],
),
Expand All @@ -131,6 +207,9 @@ class: new Node\Name\FullyQualified(
new Node\Arg(
$this->mapper instanceof Builder ? $this->mapper->getNode() : $this->mapper,
),
new Node\Arg(
$this->logger ?? new Node\Expr\New_(new Node\Name\FullyQualified(\Psr\Log\NullLogger::class))
),
],
);
}
Expand Down

0 comments on commit 0a9d785

Please sign in to comment.