-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new InMemory Akeneo client implementation
- Loading branch information
Showing
10 changed files
with
754 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\Operation\CreatableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Api\Operation\DeletableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Api\Operation\GettableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Api\Operation\ListableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Api\Operation\UpsertableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Api\Operation\UpsertableResourceListInterface; | ||
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException; | ||
use Akeneo\Pim\ApiClient\Pagination\PageInterface; | ||
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface; | ||
use ArrayIterator; | ||
use GuzzleHttp\Promise\PromiseInterface; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
use Http\Promise\Promise; | ||
use Psr\Http\Message\StreamInterface; | ||
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\ResourceInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @template T of ResourceInterface | ||
*/ | ||
abstract class InMemoryApi implements | ||
ListableResourceInterface, | ||
GettableResourceInterface, | ||
CreatableResourceInterface, | ||
UpsertableResourceInterface, | ||
UpsertableResourceListInterface, | ||
DeletableResourceInterface | ||
{ | ||
/** | ||
* @var array<string, T> | ||
*/ | ||
public static array $resources = []; | ||
|
||
/** @return class-string */ | ||
abstract protected function getResourceClass(): string; | ||
|
||
public static function addResource(ResourceInterface $resource) | ||
{ | ||
self::$resources[$resource->getIdentifier()] = $resource; | ||
} | ||
|
||
public function create(string $code, array $data = []): int | ||
{ | ||
$class = $this->getResourceClass(); | ||
Assert::isInstanceOf($class, ResourceInterface::class); | ||
|
||
self::$resources[] = call_user_func([$class, 'create'], [$code, $data]); | ||
|
||
return 201; | ||
} | ||
|
||
public function delete(string $code): int | ||
{ | ||
if (!array_key_exists($code, self::$resources)) { | ||
throw $this->createNotFoundException(); | ||
} | ||
unset(self::$resources[$code]); | ||
|
||
return 204; | ||
} | ||
|
||
public function listPerPage(int $limit = 100, bool $withCount = false, array $queryParameters = []): PageInterface | ||
{ | ||
// TODO: Implement listPerPage() method. | ||
} | ||
|
||
public function all(int $pageSize = 100, array $queryParameters = []): ResourceCursorInterface | ||
{ | ||
return new class(new ArrayIterator(self::$resources), $pageSize) implements ResourceCursorInterface { | ||
public function __construct(private ArrayIterator $iterator, private int $pageSize) | ||
{ | ||
} | ||
|
||
public function current() | ||
{ | ||
return $this->iterator->current(); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
$this->iterator->next(); | ||
} | ||
|
||
public function key(): mixed | ||
{ | ||
return $this->iterator->key(); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return $this->iterator->valid(); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->iterator->rewind(); | ||
} | ||
|
||
public function getPageSize(): ?int | ||
{ | ||
return $this->pageSize; | ||
} | ||
}; | ||
} | ||
|
||
public function get(string $code, array $queryParameters = []): array | ||
{ | ||
if (!array_key_exists($code, self::$resources)) { | ||
throw $this->createNotFoundException(); | ||
} | ||
|
||
return (array) self::$resources[$code]; | ||
} | ||
|
||
public function upsert(string $code, array $data = []): int | ||
{ | ||
// TODO: Implement upsert() method. | ||
} | ||
|
||
public function upsertAsync(string $code, array $data = []): PromiseInterface|Promise | ||
{ | ||
// TODO: Implement upsertAsync() method. | ||
} | ||
|
||
public function upsertList(StreamInterface|array $resources): \Traversable | ||
{ | ||
// TODO: Implement upsertList() method. | ||
} | ||
|
||
public function upsertAsyncList(StreamInterface|array $resources): PromiseInterface|Promise | ||
{ | ||
// TODO: Implement upsertAsyncList() method. | ||
} | ||
|
||
private function createNotFoundException(): NotFoundHttpException | ||
{ | ||
return new NotFoundHttpException('Resource not found', new Request('GET', '/'), new Response(404)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\AttributeApiInterface; | ||
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\Attribute; | ||
|
||
final class InMemoryAttributeApi extends InMemoryApi implements AttributeApiInterface | ||
{ | ||
protected function getResourceClass(): string | ||
{ | ||
return Attribute::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\AttributeOptionApiInterface; | ||
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException; | ||
use Akeneo\Pim\ApiClient\Pagination\PageInterface; | ||
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface; | ||
use GuzzleHttp\Promise\PromiseInterface; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
use Http\Promise\Promise; | ||
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\AttributeOption; | ||
|
||
final class InMemoryAttributeOptionApi implements AttributeOptionApiInterface | ||
{ | ||
/** | ||
* @var array<string, <string, AttributeOption>> | ||
*/ | ||
public static array $attributeOptions = []; | ||
|
||
public static function addResource(AttributeOption $attributeOption): void | ||
{ | ||
self::$attributeOptions[$attributeOption->attribute][$attributeOption->code] = $attributeOption; | ||
} | ||
|
||
public function get($attributeCode, $code): array | ||
{ | ||
if (!array_key_exists($attributeCode, self::$attributeOptions)) { | ||
throw $this->createNotFoundException(); | ||
} | ||
$attributeOptions = self::$attributeOptions[$attributeCode]; | ||
if (!array_key_exists($code, $attributeOptions)) { | ||
throw $this->createNotFoundException(); | ||
} | ||
|
||
return (array) $attributeOptions[$code]; | ||
} | ||
|
||
public function create($attributeCode, $attributeOptionCode, array $data = []): int | ||
{ | ||
self::$attributeOptions[] = AttributeOption::create($attributeCode, $attributeOptionCode, $data); | ||
|
||
return 201; | ||
} | ||
|
||
public function listPerPage($attributeCode, $limit = 100, $withCount = false, array $queryParameters = []): PageInterface | ||
{ | ||
// TODO: Implement listPerPage() method. | ||
} | ||
|
||
public function all($attributeCode, $pageSize = 10, array $queryParameters = []): ResourceCursorInterface | ||
{ | ||
// TODO: Implement all() method. | ||
} | ||
|
||
public function upsert($attributeCode, $attributeOptionCode, array $data = []): int | ||
{ | ||
// TODO: Implement upsert() method. | ||
} | ||
|
||
public function upsertAsync($attributeCode, $attributeOptionCode, array $data = []): PromiseInterface|Promise | ||
{ | ||
// TODO: Implement upsertAsync() method. | ||
} | ||
|
||
public function upsertList($attributeCode, $attributeOptions): \Traversable | ||
{ | ||
// TODO: Implement upsertList() method. | ||
} | ||
|
||
public function upsertAsyncList($attributeCode, $attributeOptions): PromiseInterface|Promise | ||
{ | ||
// TODO: Implement upsertAsyncList() method. | ||
} | ||
|
||
private function createNotFoundException(): NotFoundHttpException | ||
{ | ||
return new NotFoundHttpException('Attribute option not found', new Request('GET', '/'), new Response(404)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\ProductApiInterface; | ||
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\Product; | ||
|
||
final class InMemoryProductApi extends InMemoryApi implements ProductApiInterface | ||
{ | ||
|
||
protected function getResourceClass(): string | ||
{ | ||
return Product::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model; | ||
|
||
use DateTimeInterface; | ||
|
||
final class Attribute implements ResourceInterface | ||
{ | ||
public function __construct( | ||
public string $code, | ||
public string $type = AttributeType::TEXT, | ||
public ?string $group = null, | ||
public bool $unique = false, | ||
public bool $useableAsGridFilter = true, | ||
public array $allowedExtension = [], | ||
public ?string $metricFamily = null, | ||
public ?string $defaultMetricUnit = null, | ||
public ?string $referenceDataName = null, | ||
public array $availableLocales = [], | ||
public ?int $maxCharacters = null, | ||
public ?string $validationRule = null, | ||
public ?string $validationRegexp = null, | ||
public ?bool $wysiwygEnabled = null, | ||
public ?int $numberMin = null, | ||
public ?int $numberMax = null, | ||
public ?bool $decimalsAllowed = null, | ||
public ?bool $negativeAllowed = null, | ||
public ?DateTimeInterface $dateMin = null, | ||
public ?DateTimeInterface $dateMax = null, | ||
public ?int $maxFileSize = null, | ||
public ?int $minimumInputLength = null, | ||
public int $sortOrder = 0, | ||
public bool $localizable = false, | ||
public bool $scopable = false, | ||
public array $labels = [], | ||
public array $guidelines = [], | ||
public ?bool $autoOptionSorting = null, | ||
public mixed $defaultValue = null, | ||
public array $groupLabels = [], | ||
) { | ||
} | ||
|
||
public static function create(string $code, array $data = []): ResourceInterface | ||
{ | ||
return new self( | ||
$code, | ||
$data['type'] ?? AttributeType::TEXT, | ||
$data['group'] ?? null, | ||
$data['unique'] ?? false, | ||
$data['useable_as_grid_filter'] ?? true, | ||
$data['allowed_extensions'] ?? [], | ||
$data['metric_family'] ?? null, | ||
$data['default_metric_unit'] ?? null, | ||
$data['reference_data_name'] ?? null, | ||
$data['available_locales'] ?? [], | ||
$data['max_characters'] ?? null, | ||
$data['validation_rule'] ?? null, | ||
$data['validation_regexp'] ?? null, | ||
$data['wysiwyg_enabled'] ?? null, | ||
$data['number_min'] ?? null, | ||
$data['number_max'] ?? null, | ||
$data['decimals_allowed'] ?? null, | ||
$data['negative_allowed'] ?? null, | ||
$data['date_min'] ?? null, | ||
$data['date_max'] ?? null, | ||
$data['max_file_size'] ?? null, | ||
$data['minimum_input_length'] ?? null, | ||
$data['sort_order'] ?? 0, | ||
$data['localizable'] ?? false, | ||
$data['scopable'] ?? false, | ||
$data['labels'] ?? [], | ||
$data['guidelines'] ?? [], | ||
$data['auto_option_sorting'] ?? null, | ||
$data['default_value'] ?? null, | ||
$data['group_labels'] ?? [], | ||
); | ||
} | ||
|
||
public function __serialize(): array | ||
{ | ||
return [ | ||
'code' => $this->code, | ||
'type' => $this->type, | ||
'group' => $this->group, | ||
'unique' => $this->unique, | ||
'useable_as_grid_filter' => $this->useableAsGridFilter, | ||
'allowed_extensions' => $this->allowedExtension, | ||
'metric_family' => $this->metricFamily, | ||
'default_metric_unit' => $this->defaultMetricUnit, | ||
'reference_data_name' => $this->referenceDataName, | ||
'available_locales' => $this->availableLocales, | ||
'max_characters' => $this->maxCharacters, | ||
'validation_rule' => $this->validationRule, | ||
'validation_regexp' => $this->validationRegexp, | ||
'wysiwyg_enabled' => $this->wysiwygEnabled, | ||
'number_min' => $this->numberMin, | ||
'number_max' => $this->numberMax, | ||
'decimals_allowed' => $this->decimalsAllowed, | ||
'negative_allowed' => $this->negativeAllowed, | ||
'date_min' => $this->dateMin, | ||
'date_max' => $this->dateMax, | ||
'max_file_size' => $this->maxFileSize, | ||
'minimum_input_length' => $this->minimumInputLength, | ||
'sort_order' => $this->sortOrder, | ||
'localizable' => $this->localizable, | ||
'scopable' => $this->scopable, | ||
'labels' => $this->labels, | ||
'guidelines' => $this->guidelines, | ||
'auto_option_sorting' => $this->autoOptionSorting, | ||
'default_value' => $this->defaultValue, | ||
'group_labels' => $this->groupLabels, | ||
]; | ||
} | ||
|
||
public function getIdentifier(): string | ||
{ | ||
return $this->code; | ||
} | ||
} |
Oops, something went wrong.