ProductModelController::getAttributeRepository()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
            unset($normalizedProduct['family']);
129
            $this->productModelUpdater->update($cloneProductModel, $normalizedProduct);
130
            $this->productModelUpdater->update($cloneProductModel, $content);
131
            $cloneProductModel->setCode($content['code']);
132
            // validate product model clone and return violations if found
133
            $violations = $this->validator->validate($cloneProductModel);
134
            if (count($violations) > 0) {
135
                $normalizedViolations = [];
136
                foreach ($violations as $violation) {
137
                    $violation = $this->violationNormalizer->normalize(
138
                        $violation,
139
                        'internal_api',
140
                        ['product_model' => $cloneProductModel]
141
                    );
142
                    $normalizedViolations[] = $violation;
143
                }
144
                return new JsonResponse(['values' => $normalizedViolations], Response::HTTP_BAD_REQUEST);
145
            }
146
            $this->productModelSaver->save($cloneProductModel);
147
            return new JsonResponse();
148
        } catch (\Exception $e) {
149
            return new JsonResponse(['values' => [['message' => $e->getMessage()]]], Response::HTTP_BAD_REQUEST);
150
        }
151
    }
152
153
    /**
154
     * @return string[]
155
     */
156
    protected function getAttributeCodeBlacklist() : array
157
    {
158
        return $this->attributeCodeBlacklist;
159
    }
160
161
    protected function getNormalizer() : NormalizerInterface
162
    {
163
        return $this->normalizer;
164
    }
165
166
    protected function getAttributeRepository() : AttributeRepositoryInterface
167
    {
168
        return $this->attributeRepository;
169
    }
170
}
171