| Conditions | 9 |
| Paths | 73 |
| Total Lines | 65 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 134 | public function cloneAction(Request $request) : JsonResponse |
||
| 135 | { |
||
| 136 | $data = json_decode($request->getContent(), true); |
||
| 137 | try { |
||
| 138 | // check 'code_to_clone' is provided otherwise HTTP bad request |
||
| 139 | if (false === isset($data['code_to_clone'])) { |
||
| 140 | $message = [['message' => 'Field "code_to_clone" is missing.']]; |
||
| 141 | return new JsonResponse(['values' => $message], Response::HTTP_BAD_REQUEST); |
||
| 142 | } |
||
| 143 | // check whether product to be cloned is found otherwise not found HTTP |
||
| 144 | $product = $this->productRepository->findOneByIdentifier($data['code_to_clone']); |
||
| 145 | if (null === $product) { |
||
| 146 | $message = [['message' => sprintf( |
||
| 147 | 'Product model with code %s could not be found.', |
||
| 148 | $data['code_to_clone'] |
||
| 149 | )]]; |
||
| 150 | return new JsonResponse( |
||
| 151 | ['values' => $message], |
||
| 152 | Response::HTTP_NOT_FOUND |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | unset($data['code_to_clone']); |
||
| 156 | if (isset($data['parent'])) { |
||
| 157 | // TODO: remove this as soon as support of 2.1 is dropped |
||
| 158 | $cloneProduct = $this->variantProductBuilder->createProduct(); |
||
| 159 | } else { |
||
| 160 | // check 'code' is provided otherwise HTTP bad request |
||
| 161 | if (false === isset($data['code'])) { |
||
| 162 | $message = [['message' => 'Failed "Code" is missing.']]; |
||
| 163 | return new JsonResponse(['values' => $message], Response::HTTP_BAD_REQUEST); |
||
| 164 | } |
||
| 165 | |||
| 166 | $cloneProduct = $this->productBuilder->createProduct( |
||
| 167 | $data['code'] |
||
| 168 | ); |
||
| 169 | unset($data['code']); |
||
| 170 | } |
||
| 171 | |||
| 172 | // clone product using Akeneo normalizer |
||
| 173 | $normalizedProduct = $this->normalizeProduct($product); |
||
| 174 | |||
| 175 | $normalizedProduct = $this->removeIdentifierAttributeValue($normalizedProduct); |
||
| 176 | $this->productUpdater->update($cloneProduct, $normalizedProduct); |
||
| 177 | if (!empty($data['values'])) { |
||
| 178 | $this->updateProduct($cloneProduct, $data); |
||
| 179 | } |
||
| 180 | // validate product model clone and return violations if found |
||
| 181 | $violations = $this->validator->validate($cloneProduct); |
||
| 182 | if (count($violations) > 0) { |
||
| 183 | $normalizedViolations = []; |
||
| 184 | foreach ($violations as $violation) { |
||
| 185 | $violation = $this->constraintViolationNormalizer->normalize( |
||
| 186 | $violation, |
||
| 187 | 'internal_api', |
||
| 188 | ['product' => $cloneProduct] |
||
| 189 | ); |
||
| 190 | $normalizedViolations[] = $violation; |
||
| 191 | } |
||
| 192 | |||
| 193 | return new JsonResponse(['values' => $normalizedViolations], Response::HTTP_BAD_REQUEST); |
||
| 194 | } |
||
| 195 | $this->productSaver->save($cloneProduct); |
||
| 196 | return new JsonResponse('Success.'); |
||
| 197 | } catch (\Exception $e) { |
||
| 198 | return new JsonResponse(['values' => [['message' => 'Failed.']]], $e->getMessage()); |
||
|
|
|||
| 199 | } |
||
| 256 |