| Conditions | 9 |
| Paths | 42 |
| Total Lines | 56 |
| Code Lines | 30 |
| 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 |
||
| 55 | public function __invoke(Request $request): Response |
||
| 56 | { |
||
| 57 | /** @var ConstraintViolationListInterface[] $validationResults */ |
||
| 58 | $validationResults = []; |
||
| 59 | $commandRequests = []; |
||
| 60 | $commandsToExecute = []; |
||
| 61 | $token = $request->attributes->get('token'); |
||
| 62 | |||
| 63 | foreach ($request->request->all() as $item) { |
||
| 64 | $item['token'] = $token; |
||
| 65 | $commandRequests[] = $this->provideCommandRequest($item); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($commandRequests as $commandRequest) { |
||
| 69 | $validationResult = $this->validator->validate($commandRequest); |
||
| 70 | |||
| 71 | if (0 === count($validationResult)) { |
||
| 72 | $commandsToExecute[] = $commandRequest->getCommand(); |
||
| 73 | } |
||
| 74 | |||
| 75 | $validationResults[] = $validationResult; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (!$this->isValid($validationResults)) { |
||
| 79 | /** @var ValidationErrorView $errorMessage */ |
||
| 80 | $errorMessage = new $this->validationErrorViewClass(); |
||
| 81 | |||
| 82 | $errorMessage->code = Response::HTTP_BAD_REQUEST; |
||
| 83 | $errorMessage->message = 'Validation failed'; |
||
| 84 | |||
| 85 | foreach ($validationResults as $validationResult) { |
||
| 86 | $errors = []; |
||
| 87 | |||
| 88 | /** @var ConstraintViolationInterface $result */ |
||
| 89 | foreach ($validationResult as $result) { |
||
| 90 | $errors[$result->getPropertyPath()][] = $result->getMessage(); |
||
| 91 | } |
||
| 92 | |||
| 93 | $errorMessage->errors[] = $errors; |
||
| 94 | } |
||
| 95 | |||
| 96 | return $this->viewHandler->handle(View::create($errorMessage, Response::HTTP_BAD_REQUEST)); |
||
| 97 | } |
||
| 98 | |||
| 99 | foreach ($commandsToExecute as $commandToExecute) { |
||
| 100 | $this->bus->handle($commandToExecute); |
||
| 101 | } |
||
| 102 | |||
| 103 | try { |
||
| 104 | return $this->viewHandler->handle( |
||
| 105 | View::create($this->cartQuery->getOneByToken($token), Response::HTTP_CREATED) |
||
| 106 | ); |
||
| 107 | } catch (\InvalidArgumentException $exception) { |
||
| 108 | throw new BadRequestHttpException($exception->getMessage()); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 145 |