1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flagbit\Bundle\ProductClonerBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Akeneo\Tool\Component\StorageUtils\Factory\SimpleFactoryInterface; |
6
|
|
|
use Akeneo\Tool\Component\StorageUtils\Saver\SaverInterface; |
7
|
|
|
use Akeneo\Tool\Component\StorageUtils\Updater\ObjectUpdaterInterface; |
8
|
|
|
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor; |
9
|
|
|
use Akeneo\Pim\Structure\Component\Repository\AttributeRepositoryInterface; |
10
|
|
|
use Akeneo\Pim\Enrichment\Component\Product\Repository\ProductModelRepositoryInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
16
|
|
|
|
17
|
|
|
class ProductModelController extends AbstractController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ProductModelRepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $productModelRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var NormalizerInterface |
26
|
|
|
*/ |
27
|
|
|
private $normalizer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SimpleFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $productModelFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ObjectUpdaterInterface |
36
|
|
|
*/ |
37
|
|
|
private $productModelUpdater; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var SaverInterface |
41
|
|
|
*/ |
42
|
|
|
private $productModelSaver; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ValidatorInterface |
46
|
|
|
*/ |
47
|
|
|
private $validator; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var NormalizerInterface |
51
|
|
|
*/ |
52
|
|
|
private $violationNormalizer; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var AttributeRepositoryInterface |
56
|
|
|
*/ |
57
|
|
|
private $attributeRepository; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string[] |
61
|
|
|
*/ |
62
|
|
|
private $attributeCodeBlacklist; |
63
|
|
|
|
64
|
|
|
public function __construct( |
65
|
|
|
ProductModelRepositoryInterface $productModelRepository, |
66
|
|
|
AttributeRepositoryInterface $attributeRepository, |
67
|
|
|
NormalizerInterface $normalizer, |
68
|
|
|
SimpleFactoryInterface $productModelFactory, |
69
|
|
|
ObjectUpdaterInterface $productModelUpdater, |
70
|
|
|
SaverInterface $productModelSaver, |
71
|
|
|
ValidatorInterface $validator, |
72
|
|
|
NormalizerInterface $violationNormalizer, |
73
|
|
|
array $attributeCodeBlacklist = [] |
74
|
|
|
) { |
75
|
|
|
$this->productModelRepository = $productModelRepository; |
76
|
|
|
$this->normalizer = $normalizer; |
77
|
|
|
$this->productModelFactory = $productModelFactory; |
78
|
|
|
$this->productModelUpdater = $productModelUpdater; |
79
|
|
|
$this->productModelSaver = $productModelSaver; |
80
|
|
|
$this->validator = $validator; |
81
|
|
|
$this->violationNormalizer = $violationNormalizer; |
82
|
|
|
$this->attributeRepository = $attributeRepository; |
83
|
|
|
$this->attributeCodeBlacklist = $attributeCodeBlacklist; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Request $request |
88
|
|
|
* |
89
|
|
|
* @AclAncestor("pim_enrich_product_model_create") |
90
|
|
|
* |
91
|
|
|
* @return JsonResponse |
92
|
|
|
*/ |
93
|
|
|
public function cloneAction(Request $request) : JsonResponse |
94
|
|
|
{ |
95
|
|
|
$content = json_decode($request->getContent(), true); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
// check 'code_to_clone' is provided otherwise HTTP bad request |
99
|
|
|
if (false === isset($content['code_to_clone'])) { |
100
|
|
|
$message = [['message' => 'Field "code_to_clone" is missing.']]; |
101
|
|
|
return new JsonResponse(['values' => $message], Response::HTTP_BAD_REQUEST); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// check 'code' is provided otherwise HTTP bad request |
105
|
|
|
if (false === isset($content['code'])) { |
106
|
|
|
$message = [['message' => 'Failed "Code" is missing.']]; |
107
|
|
|
return new JsonResponse(['values' => $message], Response::HTTP_BAD_REQUEST); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// check whether product to be cloned is found otherwise not found HTTP |
111
|
|
|
$productModel = $this->productModelRepository->findOneByIdentifier($content['code_to_clone']); |
112
|
|
|
if (null === $productModel) { |
113
|
|
|
$message = [['message' => sprintf( |
114
|
|
|
'Product model with code %s could not be found.', |
115
|
|
|
$content['code_to_clone'] |
116
|
|
|
)]]; |
117
|
|
|
return new JsonResponse( |
118
|
|
|
['values' => $message], |
119
|
|
|
Response::HTTP_NOT_FOUND |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
unset($content['code_to_clone']); |
123
|
|
|
// create a new product model |
124
|
|
|
$cloneProductModel = $this->productModelFactory->create(); |
125
|
|
|
|
126
|
|
|
// clone product using Akeneo normalizer and updater for reusing code |
127
|
|
|
$normalizedProduct = $this->normalizeProduct($productModel); |
128
|
|
|
$this->productModelUpdater->update($cloneProductModel, $normalizedProduct); |
|
|
|
|
129
|
|
|
$this->productModelUpdater->update($cloneProductModel, $content); |
130
|
|
|
$cloneProductModel->setCode($content['code']); |
131
|
|
|
// validate product model clone and return violations if found |
132
|
|
|
$violations = $this->validator->validate($cloneProductModel); |
133
|
|
|
if (count($violations) > 0) { |
134
|
|
|
$normalizedViolations = []; |
135
|
|
|
foreach ($violations as $violation) { |
136
|
|
|
$violation = $this->violationNormalizer->normalize( |
137
|
|
|
$violation, |
138
|
|
|
'internal_api', |
139
|
|
|
['product_model' => $cloneProductModel] |
140
|
|
|
); |
141
|
|
|
$normalizedViolations[] = $violation; |
142
|
|
|
} |
143
|
|
|
return new JsonResponse(['values' => $normalizedViolations], Response::HTTP_BAD_REQUEST); |
144
|
|
|
} |
145
|
|
|
$this->productModelSaver->save($cloneProductModel); |
146
|
|
|
return new JsonResponse(); |
147
|
|
|
} catch (\Exception $e) { |
148
|
|
|
return new JsonResponse(['values' => [['message' => $e->getMessage()]]], Response::HTTP_BAD_REQUEST); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string[] |
154
|
|
|
*/ |
155
|
|
|
protected function getAttributeCodeBlacklist() : array |
156
|
|
|
{ |
157
|
|
|
return $this->attributeCodeBlacklist; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function getNormalizer() : NormalizerInterface |
161
|
|
|
{ |
162
|
|
|
return $this->normalizer; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function getAttributeRepository() : AttributeRepositoryInterface |
166
|
|
|
{ |
167
|
|
|
return $this->attributeRepository; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|