Skip to content

Commit

Permalink
Merge pull request #59 from php-etl/feature/add-capacity-create-to-lo…
Browse files Browse the repository at this point in the history
…ader

Add create capacity for productMedialFile endpoint on Akeneo Loader
  • Loading branch information
gplanchat authored May 28, 2024
2 parents 17f77cd + 4686622 commit 1c5ef48
Show file tree
Hide file tree
Showing 34 changed files with 394 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/Builder/AlternativeLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ final class AlternativeLookup implements Builder

public function __construct(
private readonly Builder $capacity,
) {}
) {
}

public function withMerge(Builder $merge): self
{
Expand Down
10 changes: 5 additions & 5 deletions src/Builder/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

final class All implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $search = null;
private null|Node\Expr $code = null;
private null|Node\Expr $referenceEntity = null;
private null|Node\Expr $referenceEntityAttributeCode = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $search = null;
private ?Node\Expr $code = null;
private ?Node\Expr $referenceEntity = null;
private ?Node\Expr $referenceEntityAttributeCode = null;

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
8 changes: 5 additions & 3 deletions src/Builder/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

final class Get implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $identifier = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $identifier = null;

public function __construct() {}
public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
10 changes: 6 additions & 4 deletions src/Builder/Capacity/Extractor/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

final class ListPerPage implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $search = null;
private null|Node\Expr $code = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $search = null;
private ?Node\Expr $code = null;

public function __construct() {}
public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
243 changes: 243 additions & 0 deletions src/Builder/Capacity/Loader/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\Builder\Capacity\Loader;

use Kiboko\Plugin\Akeneo\MissingEndpointException;
use Kiboko\Plugin\Akeneo\MissingParameterException;
use PhpParser\Builder;
use PhpParser\Node;

final class Create implements Builder
{
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $code = null;
private ?Node\Expr $data = null;
private ?Node\Expr $referenceEntity = null;
private ?Node\Expr $referenceEntityAttribute = null;

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;

return $this;
}

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

return $this;
}

public function withData(Node\Expr $line): self
{
$this->data = $line;

return $this;
}

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

return $this;
}

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

return $this;
}

public function getNode(): Node
{
if (null === $this->endpoint) {
throw new MissingEndpointException(message: 'Please check your capacity builder, you should have selected an endpoint.');
}
if (null === $this->code) {
throw new MissingParameterException(message: 'Please check your capacity builder, you should have provided a code.');
}
if (null === $this->data) {
throw new MissingParameterException(message: 'Please check your capacity builder, you should have provided some data.');
}

return new Node\Stmt\While_(
cond: new Node\Expr\ConstFetch(
name: new Node\Name('true')
),
stmts: [
new Node\Stmt\TryCatch(
stmts: [
new Node\Stmt\Expression(
expr: new Node\Expr\MethodCall(
new Node\Expr\MethodCall(
var: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('client'),
),
name: $this->endpoint,
),
new Node\Identifier('create'),
array_filter([
$this->referenceEntity ? new Node\Arg(value: $this->referenceEntity) : null,
$this->referenceEntityAttribute ? new Node\Arg(value: $this->referenceEntityAttribute) : null,
new Node\Arg(value: $this->code),
new Node\Arg(value: $this->data),
]),
),
),
new Node\Stmt\Expression(
expr: new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\Yield_(
value: new Node\Expr\New_(
class: new Node\Name\FullyQualified(name: \Kiboko\Component\Bucket\AcceptanceResultBucket::class),
args: [
new Node\Arg(
value: new Node\Expr\Variable('line'),
),
],
),
),
)
),
],
catches: [
new Node\Stmt\Catch_(
types: [
new Node\Name\FullyQualified(
name: \Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException::class
),
],
var: new Node\Expr\Variable('exception'),
stmts: [
new Node\Stmt\Expression(
expr: new Node\Expr\MethodCall(
var: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: 'logger',
),
name: new Node\Identifier('error'),
args: [
new Node\Arg(
value: new Node\Expr\MethodCall(
var: new Node\Expr\Variable('exception'),
name: new Node\Identifier('getMessage'),
),
),
new Node\Arg(
value: new Node\Expr\Array_(
items: [
new Node\Expr\ArrayItem(
value: new Node\Expr\Variable('exception'),
key: new Node\Scalar\String_('exception'),
),
new Node\Expr\ArrayItem(
value: new Node\Expr\MethodCall(
var: new Node\Expr\Variable('exception'),
name: new Node\Identifier('getResponseErrors'),
),
key: new Node\Scalar\String_('errors'),
),
new Node\Expr\ArrayItem(
value: new Node\Expr\Variable('line'),
key: new Node\Scalar\String_('item'),
),
],
attributes: [
'kind' => Node\Expr\Array_::KIND_SHORT,
],
),
),
],
),
),
new Node\Stmt\Expression(
expr: new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\Yield_(
value: new Node\Expr\New_(
class: new Node\Name\FullyQualified(
name: \Kiboko\Component\Bucket\RejectionResultBucket::class
),
args: [
new Node\Arg(
value: new Node\Expr\Variable('line'),
),
],
),
),
),
),
],
),
new Node\Stmt\Catch_(
types: [
new Node\Name\FullyQualified(
name: \Akeneo\Pim\ApiClient\Exception\HttpException::class,
),
],
var: new Node\Expr\Variable('exception'),
stmts: [
new Node\Stmt\Expression(
expr: new Node\Expr\MethodCall(
var: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: 'logger',
),
name: new Node\Identifier('error'),
args: [
new Node\Arg(
value: new Node\Expr\MethodCall(
var: new Node\Expr\Variable('exception'),
name: new Node\Identifier('getMessage'),
),
),
new Node\Arg(
value: new Node\Expr\Array_(
items: [
new Node\Expr\ArrayItem(
value: new Node\Expr\Variable('exception'),
key: new Node\Scalar\String_('exception'),
),
new Node\Expr\ArrayItem(
value: new Node\Expr\Variable('line'),
key: new Node\Scalar\String_('item'),
),
],
attributes: [
'kind' => Node\Expr\Array_::KIND_SHORT,
],
),
),
],
),
),
new Node\Stmt\Expression(
expr: new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\Yield_(
value: new Node\Expr\New_(
class: new Node\Name\FullyQualified(
name: \Kiboko\Component\Bucket\RejectionResultBucket::class
),
args: [
new Node\Arg(
value: new Node\Expr\Variable('line'),
),
],
),
),
),
),
],
),
],
),
],
);
}
}
10 changes: 5 additions & 5 deletions src/Builder/Capacity/Loader/Upsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

final class Upsert implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $code = null;
private null|Node\Expr $data = null;
private null|Node\Expr $referenceEntity = null;
private null|Node\Expr $referenceEntityAttribute = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $code = null;
private ?Node\Expr $data = null;
private ?Node\Expr $referenceEntity = null;
private ?Node\Expr $referenceEntityAttribute = null;

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/Capacity/Loader/UpsertList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

final class UpsertList implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $data = null;
private null|Node\Expr $referenceEntity = null;
private null|Node\Expr $attributeCode = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $data = null;
private ?Node\Expr $referenceEntity = null;
private ?Node\Expr $attributeCode = null;

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
10 changes: 6 additions & 4 deletions src/Builder/Capacity/Lookup/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

final class All implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $search = null;
private null|Node\Expr $code = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $search = null;
private ?Node\Expr $code = null;
private string $type = '';

public function __construct() {}
public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
8 changes: 5 additions & 3 deletions src/Builder/Capacity/Lookup/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

final class Download implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $file = null;
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $file = null;

public function __construct() {}
public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
12 changes: 7 additions & 5 deletions src/Builder/Capacity/Lookup/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

final class Get implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $identifier = null;
private null|Node\Expr $code = null;
private null|string $type = '';
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $identifier = null;
private ?Node\Expr $code = null;
private ?string $type = '';

public function __construct() {}
public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
Loading

0 comments on commit 1c5ef48

Please sign in to comment.