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 |
||
| 23 | class Client extends HttpMethodsClient { |
||
|
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * The default Random.org endpoint. |
||
| 27 | * |
||
| 28 | * @var UriInterface |
||
| 29 | */ |
||
| 30 | protected $endpoint; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The URI Factory. |
||
| 34 | * |
||
| 35 | * @var UriFactory |
||
| 36 | */ |
||
| 37 | protected $uriFactory; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The HTTP plugins array. |
||
| 41 | * |
||
| 42 | * @var Plugin[] |
||
| 43 | */ |
||
| 44 | protected $plugins; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Client constructor. |
||
| 48 | * |
||
| 49 | * @param \Http\Client\HttpClient|NULL $httpClient |
||
| 50 | * @param \Http\Message\UriFactory|NULL $uriFactory |
||
| 51 | * @param Plugin[] $plugins |
||
| 52 | */ |
||
| 53 | 15 | public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, array $plugins = array()) { |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Set the Random.org endpoint. |
||
| 63 | * |
||
| 64 | * @param string $uri |
||
| 65 | */ |
||
| 66 | 15 | public function setEndpoint($uri) { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get the Random.org endpoint. |
||
| 72 | * |
||
| 73 | * @return UriInterface |
||
| 74 | */ |
||
| 75 | 12 | public function getEndpoint() { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Request. |
||
| 81 | * |
||
| 82 | * @param MethodPluginInterface $methodPlugin |
||
| 83 | * |
||
| 84 | * @return null|ResponseInterface |
||
| 85 | */ |
||
| 86 | 12 | public function request(MethodPluginInterface $methodPlugin) { |
|
| 87 | try { |
||
| 88 | 12 | $response = $this->post($this->getEndpoint(), [], json_encode($methodPlugin->getParameters())); |
|
| 89 | } catch (NetworkException $e) { |
||
| 90 | return NULL; |
||
| 91 | } |
||
| 92 | |||
| 93 | 12 | return $this->validateResponse($response); |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Validate the response. |
||
| 98 | * |
||
| 99 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 100 | * |
||
| 101 | * @return \Exception|ResponseInterface |
||
| 102 | */ |
||
| 103 | 12 | public function validateResponse(ResponseInterface $response) { |
|
| 104 | 12 | if (200 == $response->getStatusCode()) { |
|
| 105 | 12 | $body = json_decode((string) $response->getBody()->getContents(), TRUE); |
|
| 106 | |||
| 107 | 12 | if (isset($body['error']['code'])) { |
|
| 108 | switch ($body['error']['code']) { |
||
| 109 | View Code Duplication | case -32600: |
|
| 110 | throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']); |
||
| 111 | case -32601: |
||
| 112 | throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']); |
||
| 113 | View Code Duplication | case -32602: |
|
| 114 | throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']); |
||
| 115 | View Code Duplication | case -32603: |
|
| 116 | throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']); |
||
| 117 | View Code Duplication | default: |
|
| 118 | throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | 12 | $response->getBody()->rewind(); |
|
| 124 | |||
| 125 | 12 | return $response; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns the UriFactory. |
||
| 130 | * |
||
| 131 | * @return \Http\Message\UriFactory |
||
| 132 | */ |
||
| 133 | 15 | public function getUriFactory() { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param \Http\Message\UriFactory $uriFactory |
||
| 139 | */ |
||
| 140 | 15 | public function setUriFactory(UriFactory $uriFactory) { |
|
| 143 | |||
| 144 | } |
||
| 145 |