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 |
||
| 16 | |||
| 17 | class Request { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Host to make the calls to |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $host = "https://api.pinterest.com/v1/"; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Access token |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $access_token = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Instance of the CurlBuilder class |
||
| 35 | * |
||
| 36 | * @var CurlBuilder |
||
| 37 | */ |
||
| 38 | private $curlbuilder; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Array with the headers from the last request |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $headers; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor |
||
| 49 | * |
||
| 50 | * @param CurlBuilder $curlbuilder |
||
| 51 | 39 | */ |
|
| 52 | public function __construct(CurlBuilder $curlbuilder) |
||
| 53 | 39 | { |
|
| 54 | 39 | $this->curlbuilder = $curlbuilder; |
|
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set the access token |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * @param string $token |
||
| 62 | * @return void |
||
| 63 | 39 | */ |
|
| 64 | public function setAccessToken($token) |
||
| 65 | 39 | { |
|
| 66 | 39 | $this->access_token = $token; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Make a get request to the given endpoint |
||
| 71 | * |
||
| 72 | * @access public |
||
| 73 | * @param string $endpoint |
||
| 74 | * @param array $parameters |
||
| 75 | * @return Response |
||
| 76 | 25 | */ |
|
| 77 | public function get($endpoint, array $parameters = array()) |
||
| 78 | 25 | { |
|
| 79 | 5 | if (!empty($parameters)) { |
|
| 80 | 5 | $path = sprintf("%s/?%s", $endpoint, http_build_query($parameters)); |
|
| 81 | 20 | } else { |
|
| 82 | $path = $endpoint; |
||
| 83 | } |
||
| 84 | 25 | ||
| 85 | return $this->execute("GET", sprintf("%s%s", $this->host, $path)); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Make a post request to the given endpoint |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * @param string $endpoint |
||
| 93 | * @param array $parameters |
||
| 94 | * @return Response |
||
| 95 | 3 | */ |
|
| 96 | public function post($endpoint, array $parameters = array()) |
||
| 97 | 3 | { |
|
| 98 | 1 | return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters); |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Make a delete request to the given endpoint |
||
| 103 | * |
||
| 104 | * @access public |
||
| 105 | * @param string $endpoint |
||
| 106 | * @param array $parameters |
||
| 107 | * @return Response |
||
| 108 | 5 | */ |
|
| 109 | public function delete($endpoint, array $parameters = array()) |
||
| 110 | 5 | { |
|
| 111 | return $this->execute("DELETE", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Make an update request to the given endpoint |
||
| 116 | * |
||
| 117 | * @access public |
||
| 118 | * @param string $endpoint |
||
| 119 | * @param array $parameters |
||
| 120 | * @param array $queryparameters |
||
| 121 | * @return Response |
||
| 122 | 2 | */ |
|
| 123 | public function update($endpoint, array $parameters = array(), array $queryparameters = array()) |
||
| 124 | 2 | { |
|
| 125 | if (!empty($queryparameters)) { |
||
| 126 | $path = sprintf("%s/?%s", $endpoint, http_build_query($queryparameters)); |
||
| 127 | 2 | } else { |
|
| 128 | $path = $endpoint; |
||
| 129 | } |
||
| 130 | 2 | ||
| 131 | return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Return the headers from the last request |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | 2 | */ |
|
| 139 | public function getHeaders() |
||
| 140 | 2 | { |
|
| 141 | return $this->headers; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Execute the http request |
||
| 146 | * |
||
| 147 | * @access public |
||
| 148 | * @param string $method |
||
| 149 | * @param string $apiCall |
||
| 150 | * @param array $parameters |
||
| 151 | * @param array $headers |
||
| 152 | * @return Response |
||
| 153 | 35 | */ |
|
| 154 | public function execute($method, $apiCall, array $parameters = array(), $headers = array()) |
||
| 155 | { |
||
| 156 | 35 | // Check if the access token needs to be added |
|
| 157 | 35 | if ($this->access_token != null) { |
|
| 158 | 35 | $headers = array_merge($headers, array( |
|
| 159 | "Authorization: Bearer " . $this->access_token, |
||
| 160 | //"Content-type: application/x-www-form-urlencoded; charset=UTF-8", |
||
| 161 | 35 | "Content-Type:multipart/form-data" |
|
| 162 | 35 | )); |
|
| 163 | } |
||
| 164 | |||
| 165 | 35 | // Setup CURL |
|
| 166 | $ch = $this->curlbuilder->create(); |
||
| 167 | |||
| 168 | 35 | // Set default options |
|
| 169 | 35 | $ch->setOptions(array( |
|
| 170 | 35 | CURLOPT_URL => $apiCall, |
|
| 171 | 35 | CURLOPT_HTTPHEADER => $headers, |
|
| 172 | 35 | CURLOPT_CONNECTTIMEOUT => 20, |
|
| 173 | 35 | CURLOPT_TIMEOUT => 90, |
|
| 174 | 35 | CURLOPT_RETURNTRANSFER => true, |
|
| 175 | 35 | CURLOPT_SSL_VERIFYPEER => false, |
|
| 176 | 35 | CURLOPT_SSL_VERIFYHOST => false, |
|
| 177 | 35 | CURLOPT_HEADER => false, |
|
| 178 | 35 | CURLINFO_HEADER_OUT => true |
|
| 179 | )); |
||
| 180 | |||
| 181 | 35 | switch ($method) { |
|
| 182 | 3 | case 'POST': |
|
| 183 | 3 | $ch->setOptions(array( |
|
| 184 | 3 | CURLOPT_CUSTOMREQUEST => "POST", |
|
| 185 | 3 | CURLOPT_POST => count($parameters), |
|
| 186 | 3 | CURLOPT_POSTFIELDS => ($parameters) |
|
| 187 | )); |
||
| 188 | 3 | ||
| 189 | if (!class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) { |
||
| 190 | $ch->setOption(CURLOPT_SAFE_UPLOAD, false); |
||
| 191 | 3 | } |
|
| 192 | elseif (class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) { |
||
| 193 | $ch->setOption(CURLOPT_SAFE_UPLOAD, true); |
||
| 194 | } |
||
| 195 | 3 | ||
| 196 | 32 | break; |
|
| 197 | 5 | case 'DELETE': |
|
| 198 | 5 | $ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE"); |
|
| 199 | 27 | break; |
|
| 200 | 2 | case 'PATCH': |
|
| 201 | 2 | $ch->setOptions(array( |
|
| 202 | 2 | CURLOPT_CUSTOMREQUEST => "PATCH", |
|
| 203 | 2 | CURLOPT_POST => count($parameters), |
|
| 204 | 2 | CURLOPT_POSTFIELDS => http_build_query($parameters) |
|
| 205 | 2 | )); |
|
| 206 | 25 | break; |
|
| 207 | 25 | default: |
|
| 208 | 25 | $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET"); |
|
| 209 | 25 | break; |
|
| 210 | } |
||
| 211 | |||
| 212 | 35 | // Execute request and catch response |
|
| 213 | $response_data = $ch->execute(); |
||
| 214 | |||
| 215 | 35 | if ($response_data === false && !$ch->hasErrors()) { |
|
| 216 | throw new CurlException("Error: Curl request failed") |
||
| 217 | } |
||
|
|
|||
| 218 | else if($ch->hasErrors()) { |
||
| 219 | throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber()); |
||
| 220 | 35 | } |
|
| 221 | |||
| 222 | // Initiate the response |
||
| 223 | 35 | $response = new Response($response_data, $ch); |
|
| 224 | 1 | ||
| 225 | // Check the response code |
||
| 226 | if ($response->getResponseCode() >= 400) { |
||
| 227 | throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->getMessage(), $response->getResponseCode()); |
||
| 228 | 34 | } |
|
| 229 | |||
| 230 | // Get headers from last request |
||
| 231 | 34 | $this->headers = $ch->getHeaders(); |
|
| 232 | |||
| 233 | // Close curl resource |
||
| 234 | 34 | $ch->close(); |
|
| 235 | |||
| 236 | // Return the response |
||
| 237 | return $response; |
||
| 238 | } |
||
| 241 |