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