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 |
||
| 25 | class Client extends HttpMethodsClient { |
||
|
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * The default Random.org endpoint. |
||
| 29 | * |
||
| 30 | * @var UriInterface |
||
| 31 | */ |
||
| 32 | protected $endpoint; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The URI Factory. |
||
| 36 | * |
||
| 37 | * @var UriFactory |
||
| 38 | */ |
||
| 39 | protected $uriFactory; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The logger. |
||
| 43 | * |
||
| 44 | * @var \Psr\Log\LoggerInterface |
||
| 45 | */ |
||
| 46 | protected $logger; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Client constructor. |
||
| 50 | * |
||
| 51 | * @param \Http\Client\HttpClient|NULL $httpClient |
||
| 52 | * @param \Http\Message\UriFactory|NULL $uriFactory |
||
| 53 | * @param \Psr\Log\LoggerInterface|NULL $logger |
||
| 54 | */ |
||
| 55 | 15 | public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, LoggerInterface $logger = NULL) { |
|
| 56 | 15 | $httpClient = $httpClient ?: HttpClientDiscovery::find(); |
|
| 57 | |||
| 58 | $plugins = [ |
||
| 59 | 15 | new HeaderDefaultsPlugin(['Content-Type' => 'application/json']), |
|
| 60 | 15 | new LoggerPlugin($logger ?: new \drupol\Yaroc\Log\Logger(), new Formatter()), |
|
| 61 | ]; |
||
| 62 | 15 | $httpClient = new HttpMethodsClient(new PluginClient($httpClient, $plugins), MessageFactoryDiscovery::find()); |
|
| 63 | |||
| 64 | 15 | $this->setUriFactory($uriFactory ?: UriFactoryDiscovery::find()); |
|
| 65 | 15 | parent::__construct($httpClient, MessageFactoryDiscovery::find()); |
|
| 66 | 15 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Set the Random.org endpoint. |
||
| 70 | * |
||
| 71 | * @param string $uri |
||
| 72 | */ |
||
| 73 | 15 | public function setEndpoint($uri) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get the Random.org endpoint. |
||
| 79 | * |
||
| 80 | * @return UriInterface |
||
| 81 | */ |
||
| 82 | 12 | public function getEndpoint() { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Request. |
||
| 88 | * |
||
| 89 | * @param MethodPluginInterface $methodPlugin |
||
| 90 | * |
||
| 91 | * @return \Exception|ResponseInterface |
||
| 92 | */ |
||
| 93 | 12 | public function request(MethodPluginInterface $methodPlugin) { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Validate the response. |
||
| 101 | * |
||
| 102 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 103 | * |
||
| 104 | * @return \Exception|ResponseInterface |
||
| 105 | */ |
||
| 106 | 12 | public function validateResponse(ResponseInterface $response) { |
|
| 107 | 12 | if (200 == $response->getStatusCode()) { |
|
| 108 | 12 | $body = json_decode((string) $response->getBody()->getContents(), TRUE); |
|
| 109 | |||
| 110 | 12 | if (isset($body['error']['code'])) { |
|
| 111 | switch ($body['error']['code']) { |
||
| 112 | View Code Duplication | case -32600: |
|
| 113 | throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']); |
||
| 114 | case -32601: |
||
| 115 | throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']); |
||
| 116 | View Code Duplication | case -32602: |
|
| 117 | throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']); |
||
| 118 | View Code Duplication | case -32603: |
|
| 119 | throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']); |
||
| 120 | View Code Duplication | default: |
|
| 121 | throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | 12 | $response->getBody()->rewind(); |
|
| 127 | |||
| 128 | 12 | return $response; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the logger. |
||
| 133 | * |
||
| 134 | * @return \Psr\Log\LoggerInterface |
||
| 135 | */ |
||
| 136 | public function getLogger() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the logger. |
||
| 142 | * |
||
| 143 | * @param \Psr\Log\LoggerInterface $logger |
||
| 144 | */ |
||
| 145 | public function setLogger(LoggerInterface $logger) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the UriFactory. |
||
| 151 | * |
||
| 152 | * @return \Http\Message\UriFactory |
||
| 153 | */ |
||
| 154 | 15 | public function getUriFactory() { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param \Http\Message\UriFactory $uriFactory |
||
| 160 | */ |
||
| 161 | 15 | public function setUriFactory(UriFactory $uriFactory) { |
|
| 164 | |||
| 165 | } |
||
| 166 |