Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration: ANDRES #70

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Component/Action/ExpandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class ExpandAction implements OptionsInterface

/** @var array */
private $options = [
'set' => [],
'set' => null,
'list' => null,
];

public function apply(array $item): array
{
return array_replace_recursive($this->getOption('set'), $item);
return array_replace_recursive($this->getOption('set', $this->getOption('list', [])), $item);
}
}
3 changes: 2 additions & 1 deletion src/Component/Action/FormatAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ private function doApply($value)
$value = $this->getOption('prefix'). ltrim($value, $this->getOption('prefix'));
break;
case 'suffix':
$value = rtrim($value, $this->getOption('suffix')).$this->getOption('suffix');
$suffix = $this->getOption('suffix');
$value = (!str_ends_with($value, $suffix)) ? $value . $suffix : $value;
break;
case 'substr':
$value = substr($value, $this->getOption('offset'), $this->getOption('length'));
Expand Down
38 changes: 38 additions & 0 deletions src/Component/Action/GroupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Misery\Component\Action;

use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Configurator\ConfigurationAwareInterface;
use Misery\Component\Configurator\ConfigurationTrait;

class GroupAction implements OptionsInterface, ConfigurationAwareInterface
{
use OptionsTrait;
use ConfigurationTrait;

public const NAME = 'group';

/** @var array */
private $options = [
'name' => null,
'actionProcessor' => null,
];

public function apply(array $item): array
{
if ($this->getOption('name') && !$this->getOption('actionProcessor')) {
$this->setOption(
'actionProcessor',
$this->getConfiguration()->getGroupedActions($this->getOption('name'))
);
}

if ($this->getOption('actionProcessor')) {
$item = $this->getOption('actionProcessor')->process($item);
}

return $item;
}
}
1 change: 0 additions & 1 deletion src/Component/Action/SkipAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function apply(array $item): array
throw new SkipPipeLineException($message);
}


return $item;
}
}
22 changes: 22 additions & 0 deletions src/Component/Akeneo/Client/ApiFamiliesEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Misery\Component\Akeneo\Client;

use Misery\Component\Common\Client\ApiEndpointInterface;

class ApiFamiliesEndpoint implements ApiEndpointInterface
{
public const NAME = 'families';
private const ALL = 'families';
private const ONE = 'families/%s';

public function getAll(): string
{
return self::ALL;
}

public function getSingleEndPoint(): string
{
return self::ONE;
}
}
23 changes: 23 additions & 0 deletions src/Component/Akeneo/Client/ApiFamilyVariantsEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Misery\Component\Akeneo\Client;

use Misery\Component\Common\Client\ApiEndpointInterface;

class ApiFamilyVariantsEndpoint implements ApiEndpointInterface
{
public const NAME = 'family_variants';

private const ALL = 'families/%s/variants';
private const ONE = 'families/%family%/variants/%code%';

public function getAll(): string
{
return self::ALL;
}

public function getSingleEndPoint(): string
{
return self::ONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ApiReferenceEntitiesEndpoint implements ApiEndpointInterface
public const NAME = 'reference-entities';

private const ALL = 'reference-entities/%s/records';
private const ONE = 'reference-entities/%s/records/%s';
private const ONE = 'reference-entities/%reference-code%/records/%code%';

public function getAll(): string
{
Expand Down
19 changes: 17 additions & 2 deletions src/Component/Akeneo/Client/ApiWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,32 @@ private function doWrite(array $data)
*/
private function execute(array $data): ApiResponse
{
// HERE we strip-out ex: %family% in $data but leave family as key in the $context
// so that the URL generator has a family value to work with
// it would be better if we could to this outside of our context
$context = $data;
$pattern = "/^%.*%$/";
foreach ($data as $key => $value) {
// Check if the key matches the specified pattern
if (preg_match($pattern, $key)) {
// Unset the data with the matching pattern
unset($data[$key]);
unset($context[$key]);
$context[str_replace('%','', $key)] = $value;
}
}

switch ($this->method) {
case 'DELETE':
case 'delete':
return $this->client
->delete($this->client->getUrlGenerator()->generate($this->endpoint->getSingleEndPoint(), $data['identifier']))
->delete($this->client->getUrlGenerator()->generate($this->endpoint->getSingleEndPoint(), $context['identifier']))
->getResponse()
;
case 'PATCH':
case 'patch':
return $this->client
->patch($this->client->getUrlGenerator()->format($this->endpoint->getSingleEndPoint(), $data), $data)
->patch($this->client->getUrlGenerator()->format($this->endpoint->getSingleEndPoint(), $context), $data)
->getResponse()
;
case 'MULTI_PATCH':
Expand Down
2 changes: 2 additions & 0 deletions src/Component/Akeneo/Client/HttpReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function createFromConfiguration(array $configuration, Configuration $con
ApiProductModelsEndpoint::NAME => ApiProductModelsEndpoint::class,
ApiCategoriesEndpoint::NAME => ApiCategoriesEndpoint::class,
ApiReferenceEntitiesEndpoint::NAME => ApiReferenceEntitiesEndpoint::class,
ApiFamiliesEndpoint::NAME => ApiFamiliesEndpoint::class,
ApiFamilyVariantsEndpoint::NAME => ApiFamilyVariantsEndpoint::class,
];

if (isset($configuration['identifier_filter_list'])) {
Expand Down
3 changes: 3 additions & 0 deletions src/Component/Akeneo/Client/HttpWriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function createFromConfiguration(array $configuration, Configuration $con
ApiProductsEndpoint::NAME => ApiProductsEndpoint::class,
ApiProductModelsEndpoint::NAME => ApiProductModelsEndpoint::class,
ApiCategoriesEndpoint::NAME => ApiCategoriesEndpoint::class,
ApiReferenceEntitiesEndpoint::NAME => ApiReferenceEntitiesEndpoint::class,
ApiFamiliesEndpoint::NAME => ApiFamiliesEndpoint::class,
ApiFamilyVariantsEndpoint::NAME => ApiFamilyVariantsEndpoint::class,
];

$endpoint = $endpointSet[$endpoint] ?? null;
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Common/Pipeline/LoggingPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LoggingPipe implements PipeInterface

public function pipe(array $item): array
{
dump('Result', TypeGuesser::guess($item));
dump('Result', $item);
return $item;
}
}
11 changes: 11 additions & 0 deletions src/Component/Configurator/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Configuration
{
private $pipeline = null;
private $actions = null;
private $groupedActions = null;
private $blueprints;
private $encoders;
private $decoders;
Expand Down Expand Up @@ -137,6 +138,16 @@ public function setActions(ItemActionProcessor $actionProcessor): void
$this->actions = $actionProcessor;
}

public function setGroupedActions(string $name, ItemActionProcessor $actionProcessor): void
{
$this->groupedActions[$name] = $actionProcessor;
}

public function getGroupedActions(string $name): ?ItemActionProcessor
{
return $this->groupedActions[$name] ?? null;
}

public function getActions(): ?ItemActionProcessor
{
return $this->actions;
Expand Down
4 changes: 3 additions & 1 deletion src/Component/Configurator/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Misery\Component\Configurator;

use Misery\Component\Common\FileManager\LocalFileManager;
use Misery\Component\Common\Pipeline\ActionPipe;
use Misery\Component\Common\Registry\Registry;
use Misery\Component\Common\Utils\ContextFormatter;
use Misery\Component\Common\Utils\ValueFormatter;
Expand Down Expand Up @@ -125,8 +126,9 @@ public function parseDirectivesFromConfiguration(array $configuration): Configur
case $key === 'blueprint';
$this->manager->configureBlueprints($configuration['blueprint']);
break;
case $key === 'action';
case $key === 'actions';
$this->manager->createActions($configuration['actions']);
$this->manager->configureAction($valueConfiguration);
break;
case $key === 'encoder';
$this->manager->createEncoder($configuration['encoder']);
Expand Down
11 changes: 10 additions & 1 deletion src/Component/Configurator/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ public function configureAccounts(array $configuration): void
}
}

public function configureAction(array $configuration): void
{
foreach ($configuration as $action) {
$this->config->setGroupedActions(
$action['name'],
$this->createActions($action['actions'])
);
}
}

public function createActions(array $configuration): ItemActionProcessor
{
/** @var ItemActionProcessorFactory $factory */
Expand Down Expand Up @@ -212,7 +222,6 @@ public function createConverter(array|string $configuration): ConverterInterface
/** @var ConverterFactory $factory */
$factory = $this->factory->getFactory('converter');


// stop creation if it was already created
if (is_array($configuration) && isset($configuration['name'])) {
if ($converter = $this->config->getConverter($configuration['name'])) {
Expand Down
30 changes: 30 additions & 0 deletions src/Component/Converter/Akeneo/Api/FamilyVariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Misery\Component\Converter\Akeneo\Api;

use Misery\Component\Common\Registry\RegisteredByNameInterface;
use Misery\Component\Converter\ConverterInterface;

class FamilyVariant implements ConverterInterface, RegisteredByNameInterface
{
public function convert(array $item): array
{
return $item;
}

public function revert(array $item): array
{
$item['%family%'] = $item['family'];
unset($item['family']);
// $item['labels'] = [];
// $item['variant_attribute_sets'] = [];

return $item;
}

public function getName(): string
{
return 'akeneo/family_variant/api';
}
}
25 changes: 25 additions & 0 deletions src/Component/Converter/Akeneo/Api/ProductModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Misery\Component\Converter\Akeneo\Api;

use Misery\Component\Converter\AkeneoProductApiConverter;

class ProductModel extends AkeneoProductApiConverter
{
private array $options = [
'identifier' => 'sku',
'structure' => 'matcher', # matcher OR flat
'container' => 'values',
'allow_empty_string_values' => true,
];

public function __construct()
{
$this->setOptions($this->options);
}

public function getName(): string
{
return 'akeneo/product_model/api';
}
}
Loading
Loading