| Total Complexity | 51 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Client implements ClientInterface |
||
| 32 | { |
||
| 33 | |||
| 34 | /** @var string The API access token */ |
||
| 35 | private static $token = null; |
||
| 36 | |||
| 37 | /** @var string The instance token, settable once per new instance */ |
||
| 38 | private $instanceToken; |
||
| 39 | |||
| 40 | private $apiBaseUri; |
||
| 41 | |||
| 42 | private $apiQuery; |
||
| 43 | |||
| 44 | private $apiResource; |
||
| 45 | |||
| 46 | private $apiVersion; |
||
| 47 | |||
| 48 | private $validator; |
||
| 49 | |||
| 50 | private $guzzleClient; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|null $token The API access token, as obtained on diffbot.com/dev |
||
| 54 | * @throws DiffbotException When no token is provided |
||
| 55 | */ |
||
| 56 | public function __construct($token = null) |
||
| 57 | { |
||
| 58 | |||
| 59 | $this->apiBaseUri = 'https://api.bookwhen.com/'; |
||
| 60 | |||
| 61 | $this->apiQuery = []; |
||
| 62 | |||
| 63 | $this->apiVersion = 'v2'; |
||
| 64 | |||
| 65 | $this->validator = new Validator(); |
||
| 66 | |||
| 67 | $this->guzzleClient = new GuzzleClient([ |
||
| 68 | 'base_uri' => $this->apiBaseUri |
||
| 69 | ]); |
||
| 70 | |||
| 71 | if ($token === null) { |
||
| 72 | if (self::$token === null) { |
||
|
|
|||
| 73 | $msg = 'No token provided, and none is globally set. '; |
||
| 74 | $msg .= 'Use Diffbot::setToken, or instantiate the Diffbot class with a $token parameter.'; |
||
| 75 | throw new ConfigurationException($msg); |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | if ($this->validator->validToken($token)) { |
||
| 79 | self::$token = $token; |
||
| 80 | $this->instanceToken = self::$token; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if (empty($this->logging)) { |
||
| 85 | $this->logging = 'debug'; |
||
| 86 | $this->log = 'inShoreBookwhen.log'; |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->logger = new Logger('inShore Bookwhen API'); |
||
| 90 | $this->logger->pushHandler(new StreamHandler($this->log, Logger::DEBUG )); |
||
| 91 | $this->logger->info('Client class successfully instantiated'); |
||
| 92 | $this->logger->debug(var_export($this, true)); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @todo debug flag |
||
| 97 | */ |
||
| 98 | protected function request(): ResponseInterface |
||
| 99 | { |
||
| 100 | try { |
||
| 101 | // Authorization. |
||
| 102 | $requestOptions = [ |
||
| 103 | 'headers' => [ |
||
| 104 | 'Authorization' => 'Basic ' . base64_encode($this->instanceToken . ':') |
||
| 105 | ] |
||
| 106 | ]; |
||
| 107 | |||
| 108 | // Query. |
||
| 109 | if (!empty($this->apiQuery) && is_array($this->apiQuery)) { |
||
| 110 | $requestOptions['query'] = $this->apiQuery; |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->logger->debug('request(GET, ' . $this->apiResource . ', ' . var_export($requestOptions, true) . ')'); |
||
| 114 | //$requestOptions['debug'] = true; |
||
| 115 | |||
| 116 | return $this->guzzleClient->request('GET', $this->apiResource, $requestOptions); |
||
| 117 | |||
| 118 | } catch (Exception $e) { |
||
| 119 | throw new RestException($e, $this->logger); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @todo |
||
| 125 | */ |
||
| 126 | public function getAttachment($attachmentId) |
||
| 127 | { |
||
| 128 | if (!$this->validator->validId($attachmentId, 'attachment')) { |
||
| 129 | throw new ValidationException('attachmentId', $attachmentId); |
||
| 130 | } |
||
| 131 | $this->apiResource = $this->apiVersion . '/attachments' . '/' . $attachmentId; |
||
| 132 | |||
| 133 | try { |
||
| 134 | $Response = $this->request(); |
||
| 135 | $body = json_decode($Response->getBody()->getContents()); |
||
| 136 | $attachment = $body->data[0]; |
||
| 137 | $return = $attachment; |
||
| 138 | return $return; |
||
| 139 | } catch (Exception $e) { |
||
| 140 | throw new RestException($e, $this->logger); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * |
||
| 146 | * {@inheritDoc} |
||
| 147 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getAttachments() |
||
| 148 | * @todo is ! empty then tests each optional param and write validator method. |
||
| 149 | */ |
||
| 150 | public function getAttachments($title = null, $fileName = null, $fileType = null): array |
||
| 151 | { |
||
| 152 | $this->apiResource = $this->apiVersion . '/attachments'; |
||
| 153 | |||
| 154 | try { |
||
| 155 | $return = []; |
||
| 156 | $Response = $this->request(); |
||
| 157 | $body = json_decode($Response->getBody()->getContents()); |
||
| 158 | |||
| 159 | foreach ($body->data as $attachment) { |
||
| 160 | array_push($return, $attachment); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $return; |
||
| 164 | } catch (Exception $e) { |
||
| 165 | throw new RestException($e, $this->logger); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * |
||
| 171 | * {@inheritDoc} |
||
| 172 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass() |
||
| 173 | */ |
||
| 174 | public function getClassPass($classPassId) |
||
| 175 | { |
||
| 176 | $this->apiResource = $this->apiVersion . '/class_passes'; |
||
| 177 | |||
| 178 | if (!$this->validator->validId($classPassId, 'classPass')) { |
||
| 179 | throw new ValidationException('classPassId', $classPassId); |
||
| 180 | } |
||
| 181 | |||
| 182 | try { |
||
| 183 | $Response = $this->request(); |
||
| 184 | $body = json_decode($Response->getBody()->getContents()); |
||
| 185 | $classPass = $body->data[0]; |
||
| 186 | $return = $classPass; |
||
| 187 | return $return; |
||
| 188 | } catch (Exception $e) { |
||
| 189 | throw new RestException($e->getMessage());; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * |
||
| 195 | * {@inheritDoc} |
||
| 196 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses() |
||
| 197 | * @todo break params on to multiplper lines.. |
||
| 198 | */ |
||
| 199 | public function getClassPasses($title = null, $detail = null, $usageType, $cost = null, $usageAllowance = null, $useRestrictedForDays = null): array |
||
| 200 | { |
||
| 201 | $this->apiResource = $this->apiVersion . '/???'; |
||
| 202 | |||
| 203 | // @todo prepocess response onto nice model objects. |
||
| 204 | $Response = $this->request(); |
||
| 205 | return json_decode($Response->getBody()->getContents()); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * |
||
| 210 | * {@inheritDoc} |
||
| 211 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvent() |
||
| 212 | */ |
||
| 213 | public function getEvent($eventId) |
||
| 214 | { |
||
| 215 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 216 | throw new ValidationException('eventId', $eventId); |
||
| 217 | } |
||
| 218 | $this->apiResource = $this->apiVersion . '/events' . '/' . $eventId; |
||
| 219 | |||
| 220 | try { |
||
| 221 | $Response = $this->request(); |
||
| 222 | $body = json_decode($Response->getBody()->getContents()); |
||
| 223 | $event = $body->data; |
||
| 224 | $event->soldOut = (bool) ($event->attributes->attendee_count >= $event->attributes->attendee_limit); |
||
| 225 | $return = $event; |
||
| 226 | return $return; |
||
| 227 | } catch (Exception $e) { |
||
| 228 | throw new RestException($e, $this->logger); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * |
||
| 234 | * {@inheritDoc} |
||
| 235 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents() |
||
| 236 | */ |
||
| 237 | public function getEvents( |
||
| 238 | $calendar = false, |
||
| 239 | $entry = false, |
||
| 240 | $location = [], |
||
| 241 | $tags = [], |
||
| 242 | $title = [], |
||
| 243 | $detail = [], |
||
| 244 | $from = null, |
||
| 245 | $to = null, |
||
| 246 | $includeLocation = false, |
||
| 247 | $includeAttachments = false, |
||
| 248 | $includeTickets = false, |
||
| 249 | $includeTicketsEvents = false, |
||
| 250 | $includeTicketsClassPasses = false): array |
||
| 251 | { |
||
| 252 | // Validate $tags. |
||
| 253 | if (!empty($tags)) { |
||
| 254 | if (!is_array($tags)) { |
||
| 255 | throw new ValidationException(); |
||
| 256 | } else { |
||
| 257 | $tags = array_unique($tags); |
||
| 258 | foreach ($tags as $tag) { |
||
| 259 | if (!empty($tag) && !$this->validator->validTag($tag)) { |
||
| 260 | throw new ValidationException(); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | $this->apiQuery['filter[tag]'] = implode(',', $tags); |
||
| 265 | } |
||
| 266 | |||
| 267 | // Validate $from; |
||
| 268 | if (!empty($from)) { |
||
| 269 | if (!$this->validator->validFrom($from, $to)) { |
||
| 270 | throw new ValidationException('from', $from . '-' . $to); |
||
| 271 | } else { |
||
| 272 | $this->apiQuery['filter[from]'] = $from; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | // Validate $to; |
||
| 277 | if (!empty($to)) { |
||
| 278 | if (!$this->validator->validTo($to, $from)) { |
||
| 279 | throw new ValidationException('to', $to . '-' . $from); |
||
| 280 | } else { |
||
| 281 | $this->apiQuery['filter[to]'] = $to; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | // API resource. |
||
| 286 | $this->apiResource = $this->apiVersion . '/events'; |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | // Validate $includeLocation; |
||
| 291 | |||
| 292 | // Validate $includeTickets; |
||
| 293 | |||
| 294 | try { |
||
| 295 | $Response = $this->request(); |
||
| 296 | |||
| 297 | $body = json_decode($Response->getBody()->getContents()); |
||
| 298 | |||
| 299 | // Prepocess response onto nice model objects. |
||
| 300 | // @todo abstract. |
||
| 301 | $return = []; |
||
| 302 | |||
| 303 | foreach ($body->data as $event) { |
||
| 304 | // Add additional properties here. |
||
| 305 | $event->soldOut = (bool) ($event->attributes->attendee_count >= $event->attributes->attendee_limit); |
||
| 306 | array_push($return, $event); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $return; |
||
| 310 | } catch (Exception $e) { |
||
| 311 | throw new RestException($e, $this->logger); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * |
||
| 317 | * {@inheritDoc} |
||
| 318 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation() |
||
| 319 | */ |
||
| 320 | public function getLocation($locationId) |
||
| 321 | { |
||
| 322 | $this->apiResource = $this->apiVersion . '/locations'; |
||
| 323 | if(!$this->Valdator->validId($locationId, 'location')) { |
||
| 324 | throw new ValidationException('locationId', $locationId); |
||
| 325 | } |
||
| 326 | |||
| 327 | try { |
||
| 328 | $Response = $this->request(); |
||
| 329 | $body = json_decode($Response->getBody()->getContents()); |
||
| 330 | $location = $body->data[0]; |
||
| 331 | $return = $location; |
||
| 332 | return $return; |
||
| 333 | } catch (Exception $e) { |
||
| 334 | throw new RestException($e->getMessage()); |
||
| 335 | } |
||
| 336 | |||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * |
||
| 341 | * {@inheritDoc} |
||
| 342 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocations() |
||
| 343 | * @todo validate params. |
||
| 344 | */ |
||
| 345 | public function getLocations($addressText = null, $additionalInfo = null): array |
||
| 346 | { |
||
| 347 | $this->apiResource = $this->apiVersion . '/locations'; |
||
| 348 | |||
| 349 | $return = []; |
||
| 350 | |||
| 351 | try { |
||
| 352 | $Response = $this->request(); |
||
| 353 | $body = json_decode($Response->getBody()->getContents()); |
||
| 354 | |||
| 355 | foreach ($body->data as $location) { |
||
| 356 | array_push($return, $location); |
||
| 357 | } |
||
| 358 | |||
| 359 | return $return; |
||
| 360 | } catch (Exception $e) { |
||
| 361 | throw new RestException($e, $this->logger); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * |
||
| 367 | * {@inheritDoc} |
||
| 368 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getTicket() |
||
| 369 | */ |
||
| 370 | public function getTicket($ticketId) |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * |
||
| 392 | * {@inheritDoc} |
||
| 393 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getTickets() |
||
| 394 | */ |
||
| 395 | public function getTickets($eventId): array |
||
| 396 | { |
||
| 397 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 398 | throw new ValidationException('eventId', $eventId); |
||
| 399 | } |
||
| 400 | |||
| 401 | $this->apiQuery = ['event' => $eventId]; |
||
| 402 | |||
| 403 | $this->apiResource = $this->apiVersion . '/tickets'; |
||
| 404 | |||
| 405 | try { |
||
| 406 | $return = []; |
||
| 407 | |||
| 408 | $Response = $this->request(); |
||
| 409 | $body = json_decode($Response->getBody()->getContents()); |
||
| 410 | |||
| 411 | foreach ($body->data as $ticket) { |
||
| 412 | array_push($return, $ticket); |
||
| 413 | } |
||
| 414 | $this->logger->debug(var_export($return, true)); |
||
| 415 | return $return; |
||
| 416 | } catch (GuzzleHttp\Exception\ClientException $e) { |
||
| 417 | throw new RestException($e, $this->logger); |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set Debug. |
||
| 423 | */ |
||
| 424 | public function setLogging($level) |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set Guzzle Client |
||
| 431 | */ |
||
| 432 | public function setGuzzleClient($guzzleClient) |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Sets the token for all future new instances |
||
| 439 | * @param $token string The API access token, as obtained on diffbot.com/dev. |
||
| 440 | */ |
||
| 441 | public static function setToken($token) |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | // EOF! |
||
| 452 |