1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flagbit\Bundle\ProductClonerBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Akeneo\Component\StorageUtils\Factory\SimpleFactoryInterface; |
6
|
|
|
use Akeneo\Component\StorageUtils\Saver\SaverInterface; |
7
|
|
|
use Akeneo\Component\StorageUtils\Updater\ObjectUpdaterInterface; |
8
|
|
|
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor; |
9
|
|
|
use Pim\Component\Catalog\Repository\AttributeRepositoryInterface; |
10
|
|
|
use Pim\Component\Catalog\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
|
|
|
* @var AttributeRepositoryInterface |
55
|
|
|
*/ |
56
|
|
|
private $attributeRepository; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* DefaultController constructor. |
60
|
|
|
* |
61
|
|
|
* @param ProductModelRepositoryInterface $productModelRepository |
62
|
|
|
* @param NormalizerInterface $normalizer |
63
|
|
|
* @param SimpleFactoryInterface $productModelFactory |
64
|
|
|
* @param ObjectUpdaterInterface $productModelUpdater |
65
|
|
|
* @param SaverInterface $productModelSaver |
66
|
|
|
* @param ValidatorInterface $validator |
67
|
|
|
* @param NormalizerInterface $violiationNormalizer |
68
|
|
|
* @param AttributeRepositoryInterface $attributeRepository |
69
|
|
|
*/ |
70
|
|
|
public function __construct( |
71
|
|
|
ProductModelRepositoryInterface $productModelRepository, |
72
|
|
|
AttributeRepositoryInterface $attributeRepository, |
73
|
|
|
NormalizerInterface $normalizer, |
74
|
|
|
SimpleFactoryInterface $productModelFactory, |
75
|
|
|
ObjectUpdaterInterface $productModelUpdater, |
76
|
|
|
SaverInterface $productModelSaver, |
77
|
|
|
ValidatorInterface $validator, |
78
|
|
|
NormalizerInterface $violiationNormalizer |
79
|
|
|
) { |
80
|
|
|
$this->productModelRepository = $productModelRepository; |
81
|
|
|
$this->normalizer = $normalizer; |
82
|
|
|
$this->productModelFactory = $productModelFactory; |
83
|
|
|
$this->productModelUpdater = $productModelUpdater; |
84
|
|
|
$this->productModelSaver = $productModelSaver; |
85
|
|
|
$this->validator = $validator; |
86
|
|
|
$this->violationNormalizer = $violiationNormalizer; |
87
|
|
|
$this->attributeRepository = $attributeRepository; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Request $request |
92
|
|
|
* |
93
|
|
|
* @AclAncestor("pim_enrich_product_model_create") |
94
|
|
|
* |
95
|
|
|
* @return JsonResponse |
96
|
|
|
*/ |
97
|
|
|
public function cloneAction(Request $request) : JsonResponse |
98
|
|
|
{ |
99
|
|
|
$content = json_decode($request->getContent(), true); |
100
|
|
|
|
101
|
|
|
// check 'code_to_clone' is provided otherwise HTTP bad request |
102
|
|
|
if (false === isset($content['code_to_clone'])) { |
103
|
|
|
return new JsonResponse('Field "code_to_clone" is missing.', Response::HTTP_BAD_REQUEST); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// check whether product to be cloned is found otherwise not found HTTP |
107
|
|
|
$productModel = $this->productModelRepository->findOneByIdentifier($content['code_to_clone']); |
108
|
|
|
if (null === $productModel) { |
109
|
|
|
return new JsonResponse( |
110
|
|
|
sprintf('Product model with code %s could not be found.', $content['code_to_clone']), |
111
|
|
|
Response::HTTP_NOT_FOUND |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
unset($content['code_to_clone']); |
115
|
|
|
// create a new product model |
116
|
|
|
$cloneProductModel = $this->productModelFactory->create(); |
117
|
|
|
|
118
|
|
|
// clone product using Akeneo normalizer and updater for reusing code |
119
|
|
|
$normalizedProduct = $this->normalizeProduct($productModel); |
120
|
|
|
$this->productModelUpdater->update($cloneProductModel, $normalizedProduct); |
|
|
|
|
121
|
|
|
$this->productModelUpdater->update($cloneProductModel, $content); |
122
|
|
|
$cloneProductModel->setCode($content['code']); |
123
|
|
|
// validate product model clone and return violations if found |
124
|
|
|
$violations = $this->validator->validate($cloneProductModel); |
125
|
|
|
if (count($violations) > 0) { |
126
|
|
|
$normalizedViolations = []; |
127
|
|
|
foreach ($violations as $violation) { |
128
|
|
|
$violation = $this->violationNormalizer->normalize( |
129
|
|
|
$violation, |
130
|
|
|
'internal_api', |
131
|
|
|
['product_model' => $cloneProductModel] |
132
|
|
|
); |
133
|
|
|
$normalizedViolations[] = $violation; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new JsonResponse(['values' => $normalizedViolations], Response::HTTP_BAD_REQUEST); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->productModelSaver->save($cloneProductModel); |
140
|
|
|
|
141
|
|
|
return new JsonResponse(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function getNormalizer(): NormalizerInterface |
145
|
|
|
{ |
146
|
|
|
return $this->normalizer; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function getAttributeRepository(): AttributeRepositoryInterface |
150
|
|
|
{ |
151
|
|
|
return $this->attributeRepository; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|