Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 26 | class Client extends HttpMethodsClient { |
||
|
|
|||
| 27 | |||
| 28 | /** |
||
| 29 | * The default Random.org endpoint. |
||
| 30 | * |
||
| 31 | * @var UriInterface |
||
| 32 | */ |
||
| 33 | protected $endpoint; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The URI Factory. |
||
| 37 | * |
||
| 38 | * @var UriFactory |
||
| 39 | */ |
||
| 40 | protected $uriFactory; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The logger. |
||
| 44 | * |
||
| 45 | * @var \Psr\Log\LoggerInterface |
||
| 46 | */ |
||
| 47 | protected $logger; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Client constructor. |
||
| 51 | * |
||
| 52 | * @param \Http\Client\HttpClient|NULL $httpClient |
||
| 53 | * @param \Http\Message\UriFactory|NULL $uriFactory |
||
| 54 | * @param \Psr\Log\LoggerInterface|NULL $logger |
||
| 55 | */ |
||
| 56 | 15 | public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, LoggerInterface $logger = NULL) { |
|
| 57 | 15 | $httpClient = $httpClient ?: HttpClientDiscovery::find(); |
|
| 58 | |||
| 59 | $plugins = [ |
||
| 60 | 15 | new HeaderDefaultsPlugin(['Content-Type' => 'application/json']), |
|
| 61 | 15 | new LoggerPlugin($logger ?: new \drupol\Yaroc\Log\Logger(), new Formatter()), |
|
| 62 | ]; |
||
| 63 | 15 | $httpClient = new HttpMethodsClient(new PluginClient($httpClient, $plugins), MessageFactoryDiscovery::find()); |
|
| 64 | |||
| 65 | 15 | $this->setUriFactory($uriFactory ?: UriFactoryDiscovery::find()); |
|
| 66 | 15 | parent::__construct($httpClient, MessageFactoryDiscovery::find()); |
|
| 67 | 15 | } |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Set the Random.org endpoint. |
||
| 71 | * |
||
| 72 | * @param string $uri |
||
| 73 | */ |
||
| 74 | 15 | public function setEndpoint($uri) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get the Random.org endpoint. |
||
| 80 | * |
||
| 81 | * @return UriInterface |
||
| 82 | */ |
||
| 83 | 12 | public function getEndpoint() { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Request. |
||
| 89 | * |
||
| 90 | * @param MethodPluginInterface $methodPlugin |
||
| 91 | * |
||
| 92 | * @return null|ResponseInterface |
||
| 93 | */ |
||
| 94 | 12 | public function request(MethodPluginInterface $methodPlugin) { |
|
| 95 | try { |
||
| 96 | 12 | $response = $this->post($this->getEndpoint(), [], json_encode($methodPlugin->getParameters())); |
|
| 97 | } catch (NetworkException $e) { |
||
| 98 | return NULL; |
||
| 99 | } |
||
| 100 | |||
| 101 | 12 | return $this->validateResponse($response); |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Validate the response. |
||
| 106 | * |
||
| 107 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 108 | * |
||
| 109 | * @return \Exception|ResponseInterface |
||
| 110 | */ |
||
| 111 | 12 | public function validateResponse(ResponseInterface $response) { |
|
| 112 | 12 | if (200 == $response->getStatusCode()) { |
|
| 113 | 12 | $body = json_decode((string) $response->getBody()->getContents(), TRUE); |
|
| 114 | |||
| 115 | 12 | if (isset($body['error']['code'])) { |
|
| 116 | switch ($body['error']['code']) { |
||
| 117 | View Code Duplication | case -32600: |
|
| 118 | throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']); |
||
| 119 | case -32601: |
||
| 120 | throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']); |
||
| 121 | View Code Duplication | case -32602: |
|
| 122 | throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']); |
||
| 123 | View Code Duplication | case -32603: |
|
| 124 | throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']); |
||
| 125 | View Code Duplication | default: |
|
| 126 | throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | 12 | $response->getBody()->rewind(); |
|
| 132 | |||
| 133 | 12 | return $response; |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the logger. |
||
| 138 | * |
||
| 139 | * @return \Psr\Log\LoggerInterface |
||
| 140 | */ |
||
| 141 | public function getLogger() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the logger. |
||
| 147 | * |
||
| 148 | * @param \Psr\Log\LoggerInterface $logger |
||
| 149 | */ |
||
| 150 | public function setLogger(LoggerInterface $logger) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the UriFactory. |
||
| 156 | * |
||
| 157 | * @return \Http\Message\UriFactory |
||
| 158 | */ |
||
| 159 | 15 | public function getUriFactory() { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param \Http\Message\UriFactory $uriFactory |
||
| 165 | */ |
||
| 166 | 15 | public function setUriFactory(UriFactory $uriFactory) { |
|
| 169 | |||
| 170 | } |
||
| 171 |