| Conditions | 7 |
| Paths | 14 |
| Total Lines | 55 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 114 | public function cloneAction(Request $request) : JsonResponse |
||
| 115 | { |
||
| 116 | $data = json_decode($request->getContent(), true); |
||
| 117 | |||
| 118 | // check 'code_to_clone' is provided otherwise HTTP bad request |
||
| 119 | if (false === isset($data['code_to_clone'])) { |
||
| 120 | return new JsonResponse('Field "code_to_clone" is missing.', Response::HTTP_BAD_REQUEST); |
||
| 121 | } |
||
| 122 | |||
| 123 | // check whether product to be cloned is found otherwise not found HTTP |
||
| 124 | $product = $this->productRepository->findOneByIdentifier($data['code_to_clone']); |
||
| 125 | if (null === $product) { |
||
| 126 | return new JsonResponse( |
||
| 127 | sprintf('Product model with code %s could not be found.', $data['code_to_clone']), |
||
| 128 | Response::HTTP_NOT_FOUND |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | unset($data['code_to_clone']); |
||
| 132 | if (isset($data['parent'])) { |
||
| 133 | // TODO: remove this as soon as support of 2.1 is dropped |
||
| 134 | $cloneProduct = $this->variantProductBuilder->createProduct(); |
||
| 135 | } else { |
||
| 136 | $cloneProduct = $this->productBuilder->createProduct( |
||
| 137 | $data['code'] |
||
| 138 | ); |
||
| 139 | unset($data['code']); |
||
| 140 | } |
||
| 141 | |||
| 142 | // clone product using Akeneo normalizer |
||
| 143 | $normalizedProduct = $this->normalizeProduct($product); |
||
| 144 | |||
| 145 | $normalizedProduct = $this->removeIdentifierAttributeValue($normalizedProduct); |
||
|
|
|||
| 146 | $this->productUpdater->update($cloneProduct, $normalizedProduct); |
||
| 147 | if (!empty($data['values'])) { |
||
| 148 | $this->updateProduct($cloneProduct, $data); |
||
| 149 | } |
||
| 150 | // validate product model clone and return violations if found |
||
| 151 | $violations = $this->validator->validate($cloneProduct); |
||
| 152 | if (count($violations) > 0) { |
||
| 153 | $normalizedViolations = []; |
||
| 154 | foreach ($violations as $violation) { |
||
| 155 | $violation = $this->constraintViolationNormalizer->normalize( |
||
| 156 | $violation, |
||
| 157 | 'internal_api', |
||
| 158 | ['product' => $cloneProduct] |
||
| 159 | ); |
||
| 160 | $normalizedViolations[] = $violation; |
||
| 161 | } |
||
| 162 | |||
| 163 | return new JsonResponse(['values' => $normalizedViolations], Response::HTTP_BAD_REQUEST); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->productSaver->save($cloneProduct); |
||
| 167 | |||
| 168 | return new JsonResponse(); |
||
| 169 | } |
||
| 216 |