|
5 | 5 | namespace Synolia\SyliusAkeneoPlugin\Checker\Product;
|
6 | 6 |
|
7 | 7 | use Psr\Log\LoggerInterface;
|
8 |
| -use Synolia\SyliusAkeneoPlugin\Client\ClientFactoryInterface; |
9 | 8 | use Synolia\SyliusAkeneoPlugin\Config\AkeneoAxesEnum;
|
| 9 | +use Synolia\SyliusAkeneoPlugin\Exceptions\Retriever\FamilyVariantNotFountException; |
10 | 10 | use Synolia\SyliusAkeneoPlugin\Provider\Configuration\Api\ApiConnectionProviderInterface;
|
| 11 | +use Synolia\SyliusAkeneoPlugin\Retriever\FamilyVariantRetrieverInterface; |
11 | 12 |
|
12 | 13 | final class IsProductProcessableChecker implements IsProductProcessableCheckerInterface
|
13 | 14 | {
|
14 | 15 | private const ONE_VARIATION_AXIS = 1;
|
15 | 16 |
|
16 |
| - private array $familyVariants; |
17 |
| - |
18 | 17 | public function __construct(
|
19 |
| - private ClientFactoryInterface $clientFactory, |
20 | 18 | private LoggerInterface $logger,
|
21 | 19 | private ApiConnectionProviderInterface $apiConnectionProvider,
|
| 20 | + private FamilyVariantRetrieverInterface $familyVariantRetriever, |
22 | 21 | ) {
|
23 |
| - $this->familyVariants = []; |
24 | 22 | }
|
25 | 23 |
|
26 | 24 | public function check(array $resource): bool
|
27 | 25 | {
|
28 |
| - if ('' === $resource['code'] || null === $resource['code']) { |
29 |
| - $this->logger->warning('Skipping product import because the code is missing.', ['resource' => $resource]); |
30 |
| - |
31 |
| - return false; |
32 |
| - } |
| 26 | + try { |
| 27 | + if ('' === $resource['code'] || null === $resource['code']) { |
| 28 | + $this->logger->warning('Skipping product import because the code is missing.', ['resource' => $resource]); |
33 | 29 |
|
34 |
| - if (!isset($resource['family'])) { |
35 |
| - $this->logger->warning('Skipping product import because the family is missing.', ['resource' => $resource]); |
| 30 | + return false; |
| 31 | + } |
36 | 32 |
|
37 |
| - return false; |
38 |
| - } |
| 33 | + if (!isset($resource['family'])) { |
| 34 | + $this->logger->warning('Skipping product import because the family is missing.', ['resource' => $resource]); |
39 | 35 |
|
40 |
| - $familyVariantPayload = $this->getFamilyVariant((string) $resource['family'], (string) $resource['family_variant']); |
| 36 | + return false; |
| 37 | + } |
41 | 38 |
|
42 |
| - $numberOfVariationAxis = isset($familyVariantPayload['variant_attribute_sets']) ? \count($familyVariantPayload['variant_attribute_sets']) : 0; |
| 39 | + $familyVariantPayload = $this->familyVariantRetriever->getVariant((string) $resource['family'], (string) $resource['family_variant']); |
43 | 40 |
|
44 |
| - if (null === $resource['parent'] && |
45 |
| - $numberOfVariationAxis > self::ONE_VARIATION_AXIS && |
46 |
| - $this->apiConnectionProvider->get()->getAxeAsModel() === AkeneoAxesEnum::FIRST |
47 |
| - ) { |
48 |
| - $this->logger->warning('Skipping product import because the parent is null and it has more than one variation axis.', ['resource' => $resource]); |
| 41 | + $numberOfVariationAxis = isset($familyVariantPayload['variant_attribute_sets']) ? \count($familyVariantPayload['variant_attribute_sets']) : 0; |
49 | 42 |
|
50 |
| - return false; |
51 |
| - } |
| 43 | + if (null === $resource['parent'] && |
| 44 | + $numberOfVariationAxis > self::ONE_VARIATION_AXIS && |
| 45 | + $this->apiConnectionProvider->get()->getAxeAsModel() === AkeneoAxesEnum::FIRST |
| 46 | + ) { |
| 47 | + $this->logger->debug('Skipping product import because the parent is null and it has more than one variation axis.', ['resource' => $resource]); |
52 | 48 |
|
53 |
| - // The common model will not be imported. The first axe on akeneo will become the product on sylius and the next axe on akeneo will become an option for the product variant |
54 |
| - if (null !== $resource['parent'] && |
55 |
| - $numberOfVariationAxis === 2 && |
56 |
| - $this->apiConnectionProvider->get()->getAxeAsModel() !== AkeneoAxesEnum::FIRST |
57 |
| - ) { |
58 |
| - $this->logger->warning('Skipping product import because the parent is null, and it has more than one variation axis.', ['resource' => $resource]); |
| 49 | + return false; |
| 50 | + } |
59 | 51 |
|
60 |
| - return false; |
61 |
| - } |
| 52 | + // The common model will not be imported. The first axe on akeneo will become the product on sylius and the next axe on akeneo will become an option for the product variant |
| 53 | + if (null !== $resource['parent'] && |
| 54 | + $numberOfVariationAxis === 2 && |
| 55 | + $this->apiConnectionProvider->get()->getAxeAsModel() !== AkeneoAxesEnum::FIRST |
| 56 | + ) { |
| 57 | + $this->logger->debug('Skipping product import because the parent is null, and it has more than one variation axis.', ['resource' => $resource]); |
62 | 58 |
|
63 |
| - return true; |
64 |
| - } |
| 59 | + return false; |
| 60 | + } |
65 | 61 |
|
66 |
| - private function getFamilyVariant(string $family, string $familyVariant): array |
67 |
| - { |
68 |
| - if (isset($this->familyVariants[$family][$familyVariant])) { |
69 |
| - return $this->familyVariants[$family][$familyVariant]; |
| 62 | + return true; |
| 63 | + } catch (FamilyVariantNotFountException) { |
| 64 | + return false; |
70 | 65 | }
|
71 |
| - |
72 |
| - $familyVariantPayload = $this->clientFactory |
73 |
| - ->createFromApiCredentials() |
74 |
| - ->getFamilyVariantApi() |
75 |
| - ->get( |
76 |
| - $family, |
77 |
| - $familyVariant, |
78 |
| - ) |
79 |
| - ; |
80 |
| - |
81 |
| - $this->familyVariants[$family][$familyVariant] = $familyVariantPayload; |
82 |
| - |
83 |
| - return $familyVariantPayload; |
84 | 66 | }
|
85 | 67 | }
|
0 commit comments