-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSimpleProductVariantResourceProcessor.php
More file actions
109 lines (89 loc) · 4.56 KB
/
Copy pathSimpleProductVariantResourceProcessor.php
File metadata and controls
109 lines (89 loc) · 4.56 KB
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
<?php
declare(strict_types=1);
namespace Synolia\SyliusAkeneoPlugin\Processor\Resource\ProductVariant;
use Doctrine\ORM\EntityManagerInterface;
use LogicException;
use Psr\Log\LoggerInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Product\Factory\ProductFactoryInterface;
use Sylius\Component\Product\Factory\ProductVariantFactoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Event\Product\AfterProcessingProductEvent;
use Synolia\SyliusAkeneoPlugin\Event\Product\BeforeProcessingProductEvent;
use Synolia\SyliusAkeneoPlugin\Event\Product\PostFlushEvent;
use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\AfterProcessingProductVariantEvent;
use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\BeforeProcessingProductVariantEvent;
use Synolia\SyliusAkeneoPlugin\Processor\Product\ProductProcessorChainInterface;
use Synolia\SyliusAkeneoPlugin\Processor\ProductVariant\ProductVariantProcessorChainInterface;
use Synolia\SyliusAkeneoPlugin\Processor\Resource\AkeneoResourceProcessorInterface;
use Throwable;
class SimpleProductVariantResourceProcessor implements AkeneoResourceProcessorInterface, ProductVariantAkeneoResourceProcessorInterface
{
public function __construct(
private RepositoryInterface $productRepository,
private RepositoryInterface $productVariantRepository,
private FactoryInterface $productFactory,
private ProductVariantFactoryInterface $productVariantFactory,
private EntityManagerInterface $entityManager,
private LoggerInterface $akeneoLogger,
private EventDispatcherInterface $dispatcher,
private ProductProcessorChainInterface $productProcessorChain,
private ProductVariantProcessorChainInterface $productVariantProcessorChain,
protected RepositoryInterface $channelPricingRepository,
protected FactoryInterface $channelPricingFactory,
) {
}
private function getOrCreateSimpleVariant(ProductInterface $product): ProductVariantInterface
{
/** @var ProductVariantInterface $productVariant */
$productVariant = $this->productVariantRepository->findOneBy(['code' => $product->getCode()]);
if (!$productVariant instanceof ProductVariantInterface) {
$productVariant = $this->productVariantFactory->createForProduct($product);
$productVariant->setCode($product->getCode());
$this->entityManager->persist($productVariant);
}
return $productVariant;
}
private function getOrCreateEntity(array $resource): ProductInterface
{
/** @var ProductInterface $product */
$product = $this->productRepository->findOneBy(['code' => $resource['identifier']]);
if (!$product instanceof ProductInterface) {
if (!$this->productFactory instanceof ProductFactoryInterface) {
throw new LogicException('Wrong Factory');
}
if (null === $resource['parent']) {
/** @var ProductInterface $product */
$product = $this->productFactory->createNew();
}
$product->setCode($resource['identifier']);
$this->entityManager->persist($product);
return $product;
}
return $product;
}
public function support(array $resource): bool
{
return null === $resource['parent'];
}
public function process(array $resource): void
{
try {
$this->dispatcher->dispatch(new BeforeProcessingProductEvent($resource));
$product = $this->getOrCreateEntity($resource);
$this->productProcessorChain->chain($product, $resource);
$this->dispatcher->dispatch(new AfterProcessingProductEvent($resource, $product));
$this->entityManager->flush();
$this->dispatcher->dispatch(new PostFlushEvent($resource, $product));
$this->dispatcher->dispatch(new BeforeProcessingProductVariantEvent($resource, $product));
$productVariant = $this->getOrCreateSimpleVariant($product);
$this->productVariantProcessorChain->chain($productVariant, $resource);
$this->dispatcher->dispatch(new AfterProcessingProductVariantEvent($resource, $productVariant));
} catch (Throwable $throwable) {
$this->akeneoLogger->warning($throwable->getMessage(), ['exception' => $throwable]);
}
}
}