/
ProductOptionValueHandler.php
278 lines (243 loc) · 11.9 KB
/
ProductOptionValueHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
declare(strict_types=1);
namespace Webgriffe\SyliusAkeneoPlugin\ValueHandler;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Exception\HttpException;
use InvalidArgumentException;
use LogicException;
use RuntimeException;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Sylius\Component\Product\Model\ProductOptionValueInterface;
use Sylius\Component\Product\Model\ProductOptionValueTranslationInterface;
use Sylius\Component\Product\Repository\ProductOptionRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webgriffe\SyliusAkeneoPlugin\ProductOptionValueHelperTrait;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;
use Webmozart\Assert\Assert;
/**
* @psalm-type AkeneoAttribute array{code: string, type: string, labels: array<string, ?string>}
* @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, ?string>}
*/
final class ProductOptionValueHandler implements ValueHandlerInterface
{
use ProductOptionValueHelperTrait;
/**
* @param FactoryInterface<ProductOptionValueInterface> $productOptionValueFactory
* @param FactoryInterface<ProductOptionValueTranslationInterface> $productOptionValueTranslationFactory
* @param RepositoryInterface<ProductOptionValueInterface> $productOptionValueRepository
*/
public function __construct(
private AkeneoPimClientInterface $apiClient,
private ProductOptionRepositoryInterface $productOptionRepository,
private FactoryInterface $productOptionValueFactory,
private FactoryInterface $productOptionValueTranslationFactory,
private RepositoryInterface $productOptionValueRepository,
private TranslationLocaleProviderInterface $translationLocaleProvider,
private TranslatorInterface $translator,
) {
}
/**
* @param mixed $subject
*/
public function supports($subject, string $attribute, array $value): bool
{
return $subject instanceof ProductVariantInterface && $this->isVariantOption($subject, $attribute);
}
/**
* @param mixed $productVariant
*/
public function handle($productVariant, string $optionCode, array $akeneoValue): void
{
if (!$productVariant instanceof ProductVariantInterface) {
throw new InvalidArgumentException(
sprintf(
'This option value handler only supports instances of %s, %s given.',
ProductVariantInterface::class,
get_debug_type($productVariant),
),
);
}
$product = $productVariant->getProduct();
Assert::isInstanceOf($product, ProductInterface::class);
if (count($akeneoValue) > 1) {
throw new RuntimeException(
sprintf(
'Cannot handle option value on Akeneo product "%s", the option of the parent product "%s" is ' .
'"%s". More than one value is set for this attribute on Akeneo but this handler only supports ' .
'single value for product options.',
$productVariant->getCode(),
$product->getCode(),
$optionCode,
),
);
}
try {
/** @var AkeneoAttribute $akeneoAttribute */
$akeneoAttribute = $this->apiClient->getAttributeApi()->get($optionCode);
} catch (HttpException $e) {
$response = $e->getResponse();
if ($response->getStatusCode() === 404) {
throw new RuntimeException(
sprintf(
'Cannot handle option value on Akeneo product "%s", the option of the parent product "%s" is ' .
'"%s". The attribute "%s" does not exists.',
$productVariant->getCode(),
$product->getCode(),
$optionCode,
$optionCode,
),
);
}
throw $e;
}
/** @var string|array|bool|int $akeneoValueData */
$akeneoValueData = $akeneoValue[0]['data'];
$productVariant->getOptionValues()->clear();
$productOption = $this->getProductOption($optionCode, $productVariant, $product);
/**
* See https://help.akeneo.com/serenity-build-your-catalog/30-serenity-manage-your-families-and-variant-families?from_search=132874388#manage-family-variants:~:text=An%20attribute%20of%20the%20family%20could%20be%20a%C2%A0variant%20axis%C2%A0only%20if%20its%20attribute%20type%20is%20one%20of%20the%20following%3A
*/
$attributeType = $akeneoAttribute['type'];
switch ($attributeType) {
case 'pim_catalog_simpleselect':
Assert::string($akeneoValueData);
$this->handleSelectOption($productOption, $optionCode, $akeneoValueData, $product, $productVariant);
break;
case 'pim_catalog_metric':
Assert::isArray($akeneoValueData);
$this->handleMetricOption($productOption, $optionCode, $akeneoValueData, $productVariant);
break;
case 'pim_catalog_boolean':
Assert::boolean($akeneoValueData);
$this->handleBooleanOption($productOption, $optionCode, $akeneoValueData, $productVariant);
break;
default:
throw new LogicException(sprintf('The Akeneo attribute type "%s" is not supported from the "%s"', $attributeType, self::class));
}
}
private function handleSelectOption(ProductOptionInterface $productOption, string $optionCode, string $akeneoValue, ProductInterface $product, ProductVariantInterface $productVariant): void
{
$optionValueCode = $this->getSyliusProductOptionValueCode($optionCode, $akeneoValue);
$optionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);
try {
/** @var AkeneoAttributeOption $akeneoAttributeOption */
$akeneoAttributeOption = $this->apiClient->getAttributeOptionApi()->get($optionCode, $akeneoValue);
} catch (HttpException $e) {
$response = $e->getResponse();
if ($response->getStatusCode() === 404) {
throw new RuntimeException(
sprintf(
'Cannot handle option value on Akeneo product "%s", the option of the parent product "%s" is ' .
'"%s". The option value for this variant is "%s" but there is no such option on Akeneo.',
$productVariant->getCode(),
$product->getCode(),
$optionCode,
$akeneoValue,
),
);
}
throw $e;
}
$this->importSelectProductOptionValueTranslations($akeneoAttributeOption, $optionValue);
if (!$productVariant->hasOptionValue($optionValue)) {
$productVariant->addOptionValue($optionValue);
}
}
private function handleMetricOption(ProductOptionInterface $productOption, string $optionCode, array $akeneoDataValue, ProductVariantInterface $productVariant): void
{
if (!array_key_exists('amount', $akeneoDataValue)) {
throw new LogicException('Amount key not found');
}
$floatAmount = (string) ($akeneoDataValue['amount']);
if (!array_key_exists('unit', $akeneoDataValue)) {
throw new LogicException('Unit key not found');
}
$unit = (string) $akeneoDataValue['unit'];
$optionValueCode = $this->getSyliusProductOptionValueCode($optionCode, $floatAmount, $unit);
$optionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);
foreach ($this->getDefinedLocaleCodes() as $localeCode) {
$label = $this->translator->trans('webgriffe_sylius_akeneo.ui.metric_amount_unit', ['unit' => $unit, 'amount' => $floatAmount], null, $localeCode);
$optionValue = $this->addOrUpdateProductOptionValueTranslation($optionValue, $localeCode, $label);
}
if (!$productVariant->hasOptionValue($optionValue)) {
$productVariant->addOptionValue($optionValue);
}
}
private function handleBooleanOption(ProductOptionInterface $productOption, string $optionCode, bool $akeneoDataValue, ProductVariantInterface $productVariant): void
{
$optionValueCode = $this->getSyliusProductOptionValueCode($optionCode, (string) $akeneoDataValue);
$productOptionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);
$this->addBooleanProductOptionValueTranslations($akeneoDataValue, $productOptionValue);
if (!$productVariant->hasOptionValue($productOptionValue)) {
$productVariant->addOptionValue($productOptionValue);
}
}
private function getProductOption(string $optionCode, ProductVariantInterface $productVariant, ProductInterface $product): ProductOptionInterface
{
/** @var ProductOptionInterface|null $productOption */
$productOption = $this->productOptionRepository->findOneBy(['code' => $optionCode]);
// TODO productOptionRepository could be removed by getting product option from product with something like:
// $productOption = $product->getOptions()->filter(
// function (ProductOptionInterface $productOption) use ($optionCode) {
// return $productOption->getCode() === $optionCode;
// }
// )->first();
if ($productOption === null) {
throw new RuntimeException(
sprintf(
'Cannot import Akeneo product "%s", the option "%s" is not set on the parent product "%s".',
(string) $productVariant->getCode(),
$optionCode,
(string) $product->getCode(),
),
);
}
return $productOption;
}
private function getOrCreateProductOptionValue(string $optionValueCode, ProductOptionInterface $productOption): ProductOptionValueInterface
{
$optionValue = $this->productOptionValueRepository->findOneBy(['code' => $optionValueCode]);
if (!$optionValue instanceof ProductOptionValueInterface) {
$optionValue = $this->createNewProductOptionValue($optionValueCode, $productOption);
}
return $optionValue;
}
private function isVariantOption(ProductVariantInterface $productVariant, string $attribute): bool
{
$product = $productVariant->getProduct();
Assert::isInstanceOf($product, ProductInterface::class);
foreach ($product->getOptions() as $option) {
if ($attribute === $option->getCode()) {
return true;
}
}
return false;
}
private function getDefinedLocaleCodes(): array
{
return $this->translationLocaleProvider->getDefinedLocalesCodes();
}
/**
* @return FactoryInterface<ProductOptionValueTranslationInterface>
*/
private function getProductOptionValueTranslationFactory(): FactoryInterface
{
return $this->productOptionValueTranslationFactory;
}
/**
* @return FactoryInterface<ProductOptionValueInterface>
*/
private function getProductOptionValueFactory(): FactoryInterface
{
return $this->productOptionValueFactory;
}
private function getTranslator(): TranslatorInterface
{
return $this->translator;
}
}