Skip to content

Commit

Permalink
Merge pull request #56 from php-etl/feature/reference-entities-extractor
Browse files Browse the repository at this point in the history
Added a way to extract data from reference entities and its attribute…
  • Loading branch information
sebprt authored Oct 10, 2023
2 parents 93e913e + 3048076 commit 1750546
Show file tree
Hide file tree
Showing 28 changed files with 123 additions and 108 deletions.
3 changes: 1 addition & 2 deletions src/Builder/AlternativeLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ final class AlternativeLookup implements Builder

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

public function withMerge(Builder $merge): self
{
Expand Down
79 changes: 49 additions & 30 deletions src/Builder/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ 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|string $type = null;

public function __construct()
{
}
private null|Node\Expr $referenceEntity = null;
private null|Node\Expr $referenceEntityAttributeCode = null;

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand All @@ -40,9 +37,16 @@ public function withCode(?Node\Expr $code): self
return $this;
}

public function withType(string $type): self
public function withReferenceEntityCode(?Node\Expr $referenceEntity): self
{
$this->referenceEntity = $referenceEntity;

return $this;
}

public function withReferenceEntityAttributeOption(?Node\Expr $referenceEntityAttrbuteCode): self
{
$this->type = $type;
$this->referenceEntityAttributeCode = $referenceEntityAttrbuteCode;

return $this;
}
Expand All @@ -64,23 +68,7 @@ public function getNode(): Node
name: $this->endpoint
),
name: new Node\Identifier('all'),
args: array_filter(
[
new Node\Arg(
value: new Node\Expr\Array_(
items: $this->compileSearch(),
attributes: [
'kind' => Node\Expr\Array_::KIND_SHORT,
]
),
name: new Node\Identifier('queryParameters'),
),
null !== $this->code ? new Node\Arg(
value: $this->code,
name: $this->compileCodeNamedArgument($this->type),
) : null,
],
),
args: $this->compileArguments(),
),
valueVar: new Node\Expr\Variable('item'),
subNodes: [
Expand Down Expand Up @@ -116,12 +104,43 @@ private function compileSearch(): array
];
}

private function compileCodeNamedArgument(string $type): Node\Identifier
private function compileArguments(): array

Check failure on line 107 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan8

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor\All::compileArguments() return type has no value type specified in iterable type array.

Check failure on line 107 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan6

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor\All::compileArguments() return type has no value type specified in iterable type array.

Check failure on line 107 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan7

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor\All::compileArguments() return type has no value type specified in iterable type array.

Check failure on line 107 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan8

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor\All::compileArguments() return type has no value type specified in iterable type array.

Check failure on line 107 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan6

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor\All::compileArguments() return type has no value type specified in iterable type array.

Check failure on line 107 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan7

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor\All::compileArguments() return type has no value type specified in iterable type array.
{
return match ($type) {
'assetManager' => new Node\Identifier('assetFamilyCode'),
'referenceEntityRecord' => new Node\Identifier('referenceEntityCode'),
default => new Node\Identifier('attributeCode')
};
$args = [];

if (null !== $this->search) {

Check warning on line 111 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "NotIdentical": --- Original +++ New @@ @@ private function compileArguments() : array { $args = []; - if (null !== $this->search) { + if (null === $this->search) { $args[] = new Node\Arg(value: new Node\Expr\Array_(items: $this->compileSearch(), attributes: ['kind' => Node\Expr\Array_::KIND_SHORT]), name: new Node\Identifier('queryParameters')); } if (null !== $this->code) {

Check warning on line 111 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "NotIdentical": --- Original +++ New @@ @@ private function compileArguments() : array { $args = []; - if (null !== $this->search) { + if (null === $this->search) { $args[] = new Node\Arg(value: new Node\Expr\Array_(items: $this->compileSearch(), attributes: ['kind' => Node\Expr\Array_::KIND_SHORT]), name: new Node\Identifier('queryParameters')); } if (null !== $this->code) {
$args[] = new Node\Arg(
value: new Node\Expr\Array_(
items: $this->compileSearch(),
attributes: [
'kind' => Node\Expr\Array_::KIND_SHORT,
]
),
name: new Node\Identifier('queryParameters'),
);
}

if (null !== $this->code) {
$args[] = new Node\Arg(
value: $this->code,
name: new Node\Identifier('attributeCode'),
);
}

if (null !== $this->referenceEntity) {
$args[] = new Node\Arg(
value: $this->referenceEntity,
name: new Node\Identifier('referenceEntityCode'),
);
}

if (null !== $this->referenceEntity) {
$args[] = new Node\Arg(
value: $this->referenceEntityAttributeCode,

Check failure on line 139 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan8

Parameter $value of class PhpParser\Node\Arg constructor expects PhpParser\Node\Expr, PhpParser\Node\Expr|null given.

Check failure on line 139 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / phpstan8

Parameter $value of class PhpParser\Node\Arg constructor expects PhpParser\Node\Expr, PhpParser\Node\Expr|null given.
name: new Node\Identifier('attributeCode'),
);
}

return $args;

Check warning on line 144 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ if (null !== $this->referenceEntity) { $args[] = new Node\Arg(value: $this->referenceEntityAttributeCode, name: new Node\Identifier('attributeCode')); } - return $args; + return count($args) > 1 ? array_slice($args, 0, 1, true) : $args; } }

Check warning on line 144 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ if (null !== $this->referenceEntity) { $args[] = new Node\Arg(value: $this->referenceEntityAttributeCode, name: new Node\Identifier('attributeCode')); } - return $args; + return count($args) > 1 ? array_slice($args, 0, 1, true) : $args; } }
}
}
4 changes: 1 addition & 3 deletions src/Builder/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ final class Get implements Builder
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $identifier = null;

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

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Capacity/Extractor/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class ListPerPage implements Builder
private null|Node\Expr $search = null;
private null|Node\Expr $code = null;

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

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Capacity/Lookup/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ final class All implements Builder
private null|Node\Expr $code = null;
private string $type = '';

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

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Capacity/Lookup/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class Download implements Builder
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $file = null;

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

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Capacity/Lookup/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ final class Get implements Builder
private null|Node\Expr $code = null;
private null|string $type = '';

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

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Capacity/Lookup/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class ListPerPage implements Builder
private null|Node\Expr $search = null;
private null|Node\Expr $code = null;

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

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ final class Client implements Builder
private ?Node\Expr $httpStreamFactory = null;
private ?Node\Expr $fileSystem = null;

public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret)
{
}
public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret) {}

public function withToken(Node\Expr $token, Node\Expr $refreshToken): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/ConditionalLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class ConditionalLookup implements StepBuilderInterface
private iterable $alternatives = [];
private ?Node\Expr $client = null;

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

public function withClient(Node\Expr $client): self
{
Expand Down
3 changes: 1 addition & 2 deletions src/Builder/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ final class Extractor implements StepBuilderInterface

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

public function withClient(Node\Expr $client): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

final readonly class Inline implements Builder
{
public function __construct(private ArrayBuilderInterface|ObjectBuilderInterface $mapper)
{
}
public function __construct(private ArrayBuilderInterface|ObjectBuilderInterface $mapper) {}

public function getNode(): Node
{
Expand Down
3 changes: 1 addition & 2 deletions src/Builder/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ final class Loader implements StepBuilderInterface

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

public function withClient(Node\Expr $client): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Builder/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ final class Lookup implements StepBuilderInterface
private ?Node\Expr $logger = null;
private ?Node\Expr $client = null;

public function __construct(private readonly AlternativeLookup $alternative)
{
}
public function __construct(private readonly AlternativeLookup $alternative) {}

public function withClient(Node\Expr $client): self
{
Expand Down
3 changes: 1 addition & 2 deletions src/Builder/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class Search implements Builder
{
public function __construct(
private array $filters = []
) {
}
) {}

public function addFilter(
Node\Expr $field,
Expand Down
18 changes: 12 additions & 6 deletions src/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ final class All implements Akeneo\Capacity\CapacityInterface
'UNCLASSIFIED',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down Expand Up @@ -94,17 +92,25 @@ public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Extractor\All())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withType($config['type'])
;

if (isset($config['search']) && \is_array($config['search'])) {
if (isset($config['search']) && \is_array($config['search']) && \count($config['search']) > 0) {
$builder->withSearch($this->compileFilters(...$config['search']));
}

if (\in_array($config['type'], ['attributeOption', 'assetManager', 'referenceEntityRecord']) && \array_key_exists('code', $config)) {
if (\in_array($config['type'], ['attributeOption', 'assetManager']) && \array_key_exists('code', $config)) {
$builder->withCode(compileValueWhenExpression($this->interpreter, $config['code']));
}

if ('referenceEntityRecord' == $config['type']) {
$builder->withReferenceEntityCode(compileValueWhenExpression($this->interpreter, $config['reference_entity']));
}

if ('referenceEntityAttributeOption' == $config['type']) {
$builder->withReferenceEntityCode(compileValueWhenExpression($this->interpreter, $config['reference_entity']));
$builder->withReferenceEntityAttributeOption(compileValueWhenExpression($this->interpreter, $config['reference_entity_option']));
}

return $builder;
}
}
4 changes: 1 addition & 3 deletions src/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ final class Get implements Akeneo\Capacity\CapacityInterface
'assetManager',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
4 changes: 1 addition & 3 deletions src/Capacity/Extractor/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ final class ListPerPage implements Akeneo\Capacity\CapacityInterface
'UNCLASSIFIED',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
4 changes: 1 addition & 3 deletions src/Capacity/Loader/Upsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ final class Upsert implements Akeneo\Capacity\CapacityInterface
'referenceEntity',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
4 changes: 1 addition & 3 deletions src/Capacity/Lookup/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ final class All implements Akeneo\Capacity\CapacityInterface
'UNCLASSIFIED',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
4 changes: 1 addition & 3 deletions src/Capacity/Lookup/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ final class Download implements Akeneo\Capacity\CapacityInterface
'assetMediaFile',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
4 changes: 1 addition & 3 deletions src/Capacity/Lookup/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ final class Get implements Akeneo\Capacity\CapacityInterface
'assetManager',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
4 changes: 1 addition & 3 deletions src/Capacity/Lookup/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ final class ListPerPage implements Akeneo\Capacity\CapacityInterface
'UNCLASSIFIED',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
}
public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
Expand Down
Loading

0 comments on commit 1750546

Please sign in to comment.