| Conditions | 7 |
| Paths | 28 |
| Total Lines | 56 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 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 | } |
||
| 170 |