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