forked from netgen/NetgenEzSyliusBundle
/
SyliusProductStorage.php
171 lines (145 loc) · 5.47 KB
/
SyliusProductStorage.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
<?php
namespace Netgen\Bundle\EzSyliusBundle\Core\FieldType\SyliusProduct;
use Doctrine\ORM\EntityManagerInterface;
use eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface;
use eZ\Publish\SPI\FieldType\GatewayBasedStorage;
use eZ\Publish\SPI\FieldType\StorageGateway;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Product\Factory\ProductFactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
class SyliusProductStorage extends GatewayBasedStorage
{
/**
* @var \Sylius\Component\Resource\Repository\RepositoryInterface
*/
protected $productRepository;
/**
* @var \Sylius\Component\Product\Factory\ProductFactoryInterface
*/
protected $productFactory;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $entityManager;
/**
* @var \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface
*/
protected $localeConverter;
/**
* Constructor.
*
* @param \Sylius\Component\Resource\Repository\RepositoryInterface $productRepository
* @param \Sylius\Component\Product\Factory\ProductFactoryInterface $productFactory
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
* @param \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface $localeConverter
* @param \eZ\Publish\SPI\FieldType\StorageGateway $storageGateway
*/
public function __construct(
RepositoryInterface $productRepository,
ProductFactoryInterface $productFactory,
EntityManagerInterface $entityManager,
LocaleConverterInterface $localeConverter,
StorageGateway $storageGateway
) {
$this->productRepository = $productRepository;
$this->productFactory = $productFactory;
$this->entityManager = $entityManager;
$this->localeConverter = $localeConverter;
parent::__construct($storageGateway);
}
/**
* Stores value for $field in an external data source.
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
* @param array $context
*
* @return true Indicating internal value data has changed
*/
public function storeFieldData(VersionInfo $versionInfo, Field $field, array $context)
{
$product = $field->value->externalData;
$productData = $field->value->data;
if (!$product instanceof ProductInterface) {
/** @var \Sylius\Component\Core\Model\ProductInterface $product */
$product = $this->productFactory->createWithVariant();
}
if (is_array($productData)) {
$product->setCode($productData['code']);
$product->getFirstVariant()->setCode($productData['code']);
$product->getFirstVariant()->setPrice($productData['price']);
/** @var \Sylius\Component\Product\Model\ProductTranslationInterface $translation */
$locale = $this->localeConverter->convertToPOSIX($field->languageCode);
$translation = $product->translate($locale);
$product->setName($productData['name']);
$translation->setName($productData['name']);
if (isset($productData['description'])) {
$product->setDescription($productData['description']);
$translation->setDescription($productData['description']);
}
}
$this->entityManager->persist($product);
$this->entityManager->flush();
$this->gateway->storeFieldData($versionInfo, $product->getId());
return true;
}
/**
* Populates $field value property based on the external data.
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
* @param array $context
*/
public function getFieldData(VersionInfo $versionInfo, Field $field, array $context)
{
$productId = $this->gateway->getFieldData($versionInfo);
$product = null;
if (!empty($productId)) {
$product = $this->productRepository->find($productId);
}
if ($product instanceof ProductInterface) {
$product->setCurrentLocale(
$this->localeConverter->convertToPOSIX($field->languageCode)
);
}
$field->value->externalData = $product;
}
/**
* Deletes field data.
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param array $fieldIds Array of field IDs
* @param array $context
*
* @return bool
*/
public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
{
$this->gateway->deleteFieldData($versionInfo, $fieldIds);
return true;
}
/**
* Checks if field type has external data to deal with.
*
* @return bool
*/
public function hasFieldData()
{
return true;
}
/**
* Get index data for external data for search backend.
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
* @param array $context
*
* @return \eZ\Publish\SPI\Search\Field[]
*/
public function getIndexData(VersionInfo $versionInfo, Field $field, array $context)
{
return array();
}
}