Skip to content

Commit

Permalink
Add create capacity for productMedialFile endpoint on Akeneo Loader
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Apr 25, 2024
1 parent 17f77cd commit 8c486c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Capacity/Loader/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\Capacity\Loader;

use Kiboko\Plugin\Akeneo;
use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;

final class Create implements Akeneo\Capacity\CapacityInterface
{
private static array $endpoints = [
'productMediaFile',
];

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

public function applies(array $config): bool
{
return isset($config['type'])
&& \in_array($config['type'], self::$endpoints)
&& isset($config['method'])
&& 'create' === $config['method'];
}

public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Loader\Upsert())
->withEndpoint(endpoint: new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withCode(code: compileValueWhenExpression($this->interpreter, $config['code'], 'line'))
->withData(line: new Node\Expr\Variable('line'))
;

if (\array_key_exists('reference_entity', $config)) {
$builder->withReferenceEntity(referenceEntity: new Node\Scalar\String_($config['reference_entity']));
}

if (\array_key_exists('reference_entity_attribute', $config)) {
$builder->withReferenceEntityAttribute(referenceEntityAttribute: compileValueWhenExpression($this->interpreter, $config['reference_entity_attribute'], 'line'));
}

return $builder;
}
}
1 change: 1 addition & 0 deletions src/Factory/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
$this->configuration = new Akeneo\Configuration\Loader();
$this->capacities = [
new Akeneo\Capacity\Loader\Upsert($this->interpreter),
new Akeneo\Capacity\Loader\Create($this->interpreter),
new Akeneo\Capacity\Loader\UpsertList(),
];
}
Expand Down

0 comments on commit 8c486c4

Please sign in to comment.