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 |
||
| 28 | class AuthnetWebhooksRequest |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string Base URL for processing a webhook |
||
| 32 | */ |
||
| 33 | private $url; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string Endpoint for processing a webhook |
||
| 37 | */ |
||
| 38 | private $endpoint; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string JSON formatted API request |
||
| 42 | */ |
||
| 43 | private $requestJson; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var object Wrapper object representing an endpoint |
||
| 47 | */ |
||
| 48 | private $processor; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used |
||
| 52 | * for the API call |
||
| 53 | * |
||
| 54 | * @param string $api_url URL endpoint for processing a transaction |
||
| 55 | */ |
||
| 56 | 1 | public function __construct($api_url) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Outputs the account credentials, endpoint URL, and request JSON in a human readable format |
||
| 63 | * |
||
| 64 | * @return string HTML table containing debugging information |
||
| 65 | */ |
||
| 66 | 2 | public function __toString() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Creates a new webhook |
||
| 87 | * |
||
| 88 | * @param array $webhooks Array of webhooks to be created or modified |
||
| 89 | * @param string $webhookUrl URL of where webhook notifications will be sent |
||
| 90 | * @param string $status Status of webhooks to be created or modified [active/inactive] |
||
| 91 | * @return AuthnetWebhooksResponse |
||
| 92 | * @throws AuthnetInvalidJsonException |
||
| 93 | * @throws AuthnetCurlException |
||
| 94 | */ |
||
| 95 | 1 | View Code Duplication | public function createWebhooks(array $webhooks, string $webhookUrl, string $status = 'active') : object |
| 108 | |||
| 109 | /** |
||
| 110 | * Sends a test ping to a URL for (a) designated webhook(s) |
||
| 111 | * |
||
| 112 | * @param string $webhookId Webhook ID to be tested |
||
| 113 | * @throws AuthnetCurlException |
||
| 114 | */ |
||
| 115 | 1 | public function testWebhook(string $webhookId) : void |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Gets all of the available event types |
||
| 125 | * |
||
| 126 | * @return AuthnetWebhooksResponse |
||
| 127 | * @throws AuthnetCurlException |
||
| 128 | * @throws AuthnetInvalidJsonException |
||
| 129 | */ |
||
| 130 | 1 | public function getEventTypes() : object |
|
| 136 | |||
| 137 | /** |
||
| 138 | * List all of your webhooks |
||
| 139 | * |
||
| 140 | * @return AuthnetWebhooksResponse |
||
| 141 | * @throws AuthnetCurlException |
||
| 142 | * @throws AuthnetInvalidJsonException |
||
| 143 | */ |
||
| 144 | 1 | public function getWebhooks() : object |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get a webhook |
||
| 153 | * |
||
| 154 | * @param string $webhookId Webhook ID to be retrieved |
||
| 155 | * @return AuthnetWebhooksResponse |
||
| 156 | * @throws AuthnetCurlException |
||
| 157 | * @throws AuthnetInvalidJsonException |
||
| 158 | */ |
||
| 159 | 1 | public function getWebhook(string $webhookId) : object |
|
| 165 | |||
| 166 | /** |
||
| 167 | * GET API request |
||
| 168 | * |
||
| 169 | * @param string $url API endpoint to hit |
||
| 170 | * @return object |
||
| 171 | * @throws AuthnetCurlException |
||
| 172 | * @throws AuthnetInvalidJsonException |
||
| 173 | */ |
||
| 174 | 3 | private function getByUrl(string $url) : object |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Updates webhook event types |
||
| 182 | * |
||
| 183 | * @param string $webhookId Webhook ID to be modified |
||
| 184 | * @param string $webhookUrl URL of where webhook notifications will be sent |
||
| 185 | * @param array $eventTypes Array of event types to be added/removed |
||
| 186 | * @param string $status Status of webhooks to be modified [active/inactive] |
||
| 187 | * @return AuthnetWebhooksResponse |
||
| 188 | * @throws AuthnetInvalidJsonException |
||
| 189 | * @throws AuthnetCurlException |
||
| 190 | */ |
||
| 191 | 1 | View Code Duplication | public function updateWebhook(string $webhookId, string $webhookUrl, array $eventTypes, string $status = 'active') : object |
| 204 | |||
| 205 | /** |
||
| 206 | * Delete a webhook |
||
| 207 | * |
||
| 208 | * @param string $webhookId Webhook ID to be deleted |
||
| 209 | * @throws AuthnetCurlException |
||
| 210 | */ |
||
| 211 | 1 | public function deleteWebhook(string $webhookId) : void |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Retrieve Notification History |
||
| 220 | * |
||
| 221 | * @param int $limit Default: 1000 |
||
| 222 | * @param int $offset Default: 0 |
||
| 223 | * @return AuthnetWebhooksResponse |
||
| 224 | * @throws AuthnetInvalidJsonException |
||
| 225 | * @throws AuthnetCurlException |
||
| 226 | */ |
||
| 227 | 1 | public function getNotificationHistory(int $limit = 1000, int $offset = 0) : object |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Tells the handler to make the API call to Authorize.Net |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * @throws AuthnetCurlException |
||
| 243 | */ |
||
| 244 | 4 | private function handleResponse() : string |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Make GET request via Curl |
||
| 257 | * |
||
| 258 | * @param string $url |
||
| 259 | * @param array $params |
||
| 260 | * @return string |
||
| 261 | * @throws AuthnetCurlException |
||
| 262 | * |
||
| 263 | * @codeCoverageIgnore |
||
| 264 | */ |
||
| 265 | private function get(string $url, array $params = []) : string |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Make POST request via Curl |
||
| 273 | * |
||
| 274 | * @param string $url API endpoint |
||
| 275 | * @param string $request JSON request payload |
||
| 276 | * @return string |
||
| 277 | * @throws AuthnetCurlException |
||
| 278 | * |
||
| 279 | * @codeCoverageIgnore |
||
| 280 | */ |
||
| 281 | private function post(string $url, string $request) : string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Make PUT request via Curl |
||
| 289 | * |
||
| 290 | * @param string $url API endpoint |
||
| 291 | * @param string $request JSON request payload |
||
| 292 | * @return string |
||
| 293 | * @throws AuthnetCurlException |
||
| 294 | * |
||
| 295 | * @codeCoverageIgnore |
||
| 296 | */ |
||
| 297 | private function put(string $url, string $request) : string |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Make DELETE request via Curl |
||
| 305 | * |
||
| 306 | * @param string $url API endpoint |
||
| 307 | * @return string |
||
| 308 | * @throws AuthnetCurlException |
||
| 309 | * |
||
| 310 | * @codeCoverageIgnore |
||
| 311 | */ |
||
| 312 | private function delete(string $url) : string |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets the handler to be used to handle our API call. Mainly used for unit testing as Curl is used by default. |
||
| 320 | * |
||
| 321 | * @param Curl $processor |
||
| 322 | */ |
||
| 323 | 1 | public function setProcessHandler(Curl $processor) : void |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Gets the request sent to Authorize.Net in JSON format for logging purposes |
||
| 330 | * |
||
| 331 | * @return string transaction request sent to Authorize.Net in JSON format |
||
| 332 | */ |
||
| 333 | 1 | public function getRawRequest() : string |
|
| 337 | } |
||
| 338 |