Skip to content

Commit

Permalink
add capacity "get"
Browse files Browse the repository at this point in the history
  • Loading branch information
clemzarch committed Jun 26, 2023
1 parent bad4420 commit 9516871
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/Builder/Capacity/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Prestashop\Builder\Capacity;

use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;

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

public function __construct(
private readonly ExpressionLanguage $interpreter,
public array $options = [],
) {
}

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

return $this;
}

public function getNode(): Node
{
if (null === $this->endpoint) {
throw new \Exception(message: 'Please check your capacity builder, you should have selected an endpoint.');
}

$options = [];
foreach ($this->options as $key => $value) {
$options[] = new Node\Expr\ArrayItem(
value: compileValueWhenExpression($this->interpreter, $value),
key: compileValueWhenExpression($this->interpreter, $key),
);
}

return new Node\Stmt\Expression(
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\MethodCall(
var: new Node\Expr\MethodCall(
var: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('client')
),
name: $this->endpoint
),
name: new Node\Identifier('get'),
args: [
new Node\Arg(new Node\Expr\Array_($options)),
]
),
unpack: true,
),
],
),
),
);
}
}
90 changes: 90 additions & 0 deletions src/Capacity/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Prestashop\Capacity;

use Kiboko\Plugin\Prestashop;
use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

final class Get implements CapacityInterface
{
private static array $endpoints = [
'categories',
'combinations',
'manufacturers',
'product_features',
'product_feature_values',
'product_images',
'product_options',
'product_option_values',
'products',
'shops',
'stock_availables',
'suppliers',
'tax_rule_groups',
'tax_rules',
];

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'])
&& 'get' === $config['method'];
}

public function getBuilder(array $config): Builder
{
$options = array_filter($config['options'] ?? []);

if (isset($options['sorters']) && \is_array($options['sorters'])) {
$options['sort'] = $this->compileSorters($options['sorters']);
unset($options['sorters']);
}

if (isset($options['languages']) && \is_array($options['languages'])) {
$options['language'] = $this->compileLanguages($options['languages']);
unset($options['languages']);
}

if (isset($options['columns']) && \is_array($options['columns'])) {
$options['display'] = $this->compileColumns($options['columns']);
unset($options['columns']);
}

return (new Prestashop\Builder\Capacity\Get($this->interpreter, $options))
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
;
}

private function compileSorters(array $sorters): string
{
$results = [];
foreach ($sorters as $key => $value) {
$results[] = $key.'_'.$value;
}

return sprintf('[%s]', implode(',', $results));
}

private function compileLanguages(array $languages): string
{
if (\array_key_exists('from', $languages) && \array_key_exists('to', $languages)) {
return sprintf('[%s,%s]', $languages['from'], $languages['to']);
}

return sprintf('[%s]', implode('|', $languages));
}

private function compileColumns(array $columns): string
{
return sprintf('[%s]', implode(',', $columns));
}
}
1 change: 1 addition & 0 deletions src/Factory/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(private ExpressionLanguage $interpreter)
$this->configuration = new Prestashop\Configuration\Extractor();
$this->capacities = [
new Prestashop\Capacity\All($this->interpreter),
new Prestashop\Capacity\Get($this->interpreter),
];
}

Expand Down

0 comments on commit 9516871

Please sign in to comment.