Conditions | 6 |
Paths | 14 |
Total Lines | 58 |
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 |
||
42 | public function __invoke(Request $request): Response |
||
43 | { |
||
44 | $client = new Client(); |
||
45 | $client->addConnection(new Connection(['host' => $this->host, 'port' => $this->port])); |
||
46 | |||
47 | $index = $request->attributes->get('index'); |
||
48 | $type = $request->attributes->get('type'); |
||
49 | |||
50 | if (null === $type) { |
||
51 | $requestPath = sprintf('%s/_search', $index); |
||
52 | } else { |
||
53 | $requestPath = sprintf('%s_%s/%s/_search', $index, $type, $type); |
||
54 | } |
||
55 | |||
56 | $queryParameters = $request->query->all(); |
||
57 | |||
58 | $requestBody = []; |
||
59 | |||
60 | if (isset($queryParameters['request'])) { |
||
61 | $requestBody = \json_decode($queryParameters['request'], true); |
||
62 | |||
63 | $boolQueryTerms = $requestBody['query']['bool']['filter']['terms'] ?? null; |
||
64 | |||
65 | if (isset($boolQueryTerms['configurable_children.sku'])) { |
||
66 | $sku = $boolQueryTerms['configurable_children.sku'][0]; |
||
67 | } elseif (isset($boolQueryTerms['sku'])) { |
||
68 | $sku = $boolQueryTerms['sku'][0]; |
||
69 | } |
||
70 | |||
71 | if (isset($sku)) { |
||
72 | $boolQuery = new BoolQuery(); |
||
73 | |||
74 | $boolQuery->addShould( |
||
75 | (new Query()) |
||
76 | ->term()->setParam('sku', $sku) |
||
77 | ); |
||
78 | |||
79 | $boolQuery->addShould( |
||
80 | (new Query()) |
||
81 | ->nested() |
||
82 | ->setPath('configurable_children') |
||
83 | ->setQuery((new Query()) |
||
84 | ->term()->setParam('configurable_children.sku', $sku) |
||
85 | ) |
||
86 | ); |
||
87 | |||
88 | $requestBody['query'] = $boolQuery->toArray(); |
||
89 | } |
||
90 | |||
91 | $requestBody = \json_encode($requestBody); |
||
92 | |||
93 | unset($queryParameters['request']); |
||
94 | } |
||
95 | |||
96 | $elasticsearchResponse = $client->request($requestPath, Request::METHOD_GET, $requestBody, $queryParameters); |
||
97 | |||
98 | return $this->viewHandler->handle(View::create($elasticsearchResponse->getData())); |
||
99 | } |
||
100 | } |
||
101 |