Conditions | 6 |
Paths | 6 |
Total Lines | 72 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
60 | public function send(string $url, $additionalData = null): bool |
||
61 | { |
||
62 | $credentials = SwiftypeCredentials::create($additionalData); |
||
63 | if (!$credentials->isEnabled()) { |
||
64 | $this->addMessage($credentials->getMessage()); |
||
65 | $this->getLogger()->alert($credentials->getMessage()); |
||
66 | |||
67 | return false; |
||
68 | } |
||
69 | |||
70 | $swiftypeEndpoint = sprintf( |
||
71 | self::SWIFTYPE_API, |
||
72 | $credentials->getEngineSlug(), |
||
73 | $credentials->getDomainID() |
||
74 | ); |
||
75 | |||
76 | try { |
||
77 | $response = $this->client->put( |
||
78 | $swiftypeEndpoint, |
||
79 | [ |
||
80 | 'headers' => [ |
||
81 | 'Content-Type' => 'application/json', |
||
82 | ], |
||
83 | 'body' => json_encode([ |
||
84 | 'auth_token' => $credentials->getAPIKey(), |
||
85 | 'url' => $url, |
||
86 | ]), |
||
87 | ] |
||
88 | ); |
||
89 | |||
90 | $contents = $response->getBody()->getContents(); |
||
91 | } catch (Throwable $e) { |
||
92 | $message = sprintf('Exception %s for url: %s message: %s', get_class($e), $url, $e->getMessage()); |
||
93 | |||
94 | $this->addMessage($message); |
||
95 | $this->getLogger()->alert($message); |
||
96 | |||
97 | return false; |
||
98 | } |
||
99 | |||
100 | // invalid response code |
||
101 | if (strpos((string) $response->getStatusCode(), '2') !== 0) { |
||
102 | $message = sprintf( |
||
103 | "Swiftype Crawl request failed - invalid response code \n%s\n%s\n%s", |
||
104 | $response->getStatusCode(), |
||
105 | json_encode($response->getHeaders()), |
||
106 | $contents |
||
107 | ); |
||
108 | |||
109 | $this->addMessage($message); |
||
110 | $this->getLogger()->alert($message); |
||
111 | |||
112 | return false; |
||
113 | } |
||
114 | |||
115 | // invalid response data |
||
116 | $data = json_decode($contents, true); |
||
117 | if ($data && array_key_exists('error', $data)) { |
||
118 | $message = sprintf( |
||
119 | "Swiftype Crawl request failed - invalid response data \n%s\n%s\n%s", |
||
120 | $response->getStatusCode(), |
||
121 | json_encode($response->getHeaders()), |
||
122 | $contents |
||
123 | ); |
||
124 | |||
125 | $this->addMessage($message); |
||
126 | $this->getLogger()->alert($message); |
||
127 | |||
128 | return false; |
||
129 | } |
||
130 | |||
131 | return true; |
||
132 | } |
||
162 |