| Total Complexity | 59 |
| Total Lines | 440 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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) |
||
| 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) |
||
| 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 | if (!is_null($title) && !$this->validator->validTitle($title)) { |
||
| 153 | throw new ValidationException('title', $title); |
||
| 154 | } |
||
| 155 | |||
| 156 | if(!is_null($fileName) && !$this->validator->validFileName($fileName)) { |
||
| 157 | throw new ValidationException('file name', $fileName); |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | if(!is_null($fileType) && !$this->validator->validFileType($fileType)) { |
||
| 162 | throw new ValidationException('file type', $fileType); |
||
| 163 | } |
||
| 164 | |||
| 165 | $this->apiResource = $this->apiVersion . '/attachments'; |
||
| 166 | |||
| 167 | try { |
||
| 168 | $return = []; |
||
| 169 | $Response = $this->request(); |
||
| 170 | $body = json_decode($Response->getBody()->getContents()); |
||
| 171 | |||
| 172 | foreach ($body->data as $attachment) { |
||
| 173 | array_push($return, $attachment); |
||
| 174 | } |
||
| 175 | |||
| 176 | return $return; |
||
| 177 | } catch (Exception $e) { |
||
| 178 | throw new RestException($e, $this->logger); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * |
||
| 184 | * {@inheritDoc} |
||
| 185 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass() |
||
| 186 | */ |
||
| 187 | public function getClassPass($classPassId) |
||
| 188 | { |
||
| 189 | $this->apiResource = $this->apiVersion . '/class_passes'; |
||
| 190 | |||
| 191 | if (!$this->validator->validId($classPassId, 'classPass')) { |
||
| 192 | throw new ValidationException('classPassId', $classPassId); |
||
| 193 | } |
||
| 194 | |||
| 195 | try { |
||
| 196 | $Response = $this->request(); |
||
| 197 | $body = json_decode($Response->getBody()->getContents()); |
||
| 198 | $classPass = $body->data[0]; |
||
| 199 | $return = $classPass; |
||
| 200 | return $return; |
||
| 201 | } catch (Exception $e) { |
||
| 202 | throw new RestException($e->getMessage()); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * |
||
| 208 | * {@inheritDoc} |
||
| 209 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses() |
||
| 210 | * @todo break params on to multiplper lines.. |
||
| 211 | */ |
||
| 212 | public function getClassPasses( |
||
| 213 | $title = null, |
||
| 214 | $detail = null, |
||
| 215 | $usageType, |
||
| 216 | $cost = null, |
||
| 217 | $usageAllowance = null, |
||
| 218 | $useRestrictedForDays = null): array |
||
| 219 | { |
||
| 220 | if (!is_null($title) && !$this->validator->validTitle($title)) { |
||
| 221 | throw new ValidationException('title', $title); |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->apiResource = $this->apiVersion . '/???'; |
||
| 225 | |||
| 226 | // @todo prepocess response onto nice model objects. |
||
| 227 | $Response = $this->request(); |
||
| 228 | return json_decode($Response->getBody()->getContents()); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * |
||
| 233 | * {@inheritDoc} |
||
| 234 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvent() |
||
| 235 | */ |
||
| 236 | public function getEvent($eventId) |
||
| 237 | { |
||
| 238 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 239 | throw new ValidationException('eventId', $eventId); |
||
| 240 | } |
||
| 241 | $this->apiResource = $this->apiVersion . '/events' . '/' . $eventId; |
||
| 242 | |||
| 243 | try { |
||
| 244 | $Response = $this->request(); |
||
| 245 | $body = json_decode($Response->getBody()->getContents()); |
||
| 246 | $event = $body->data; |
||
| 247 | $event->soldOut = (bool) ($event->attributes->attendee_count >= $event->attributes->attendee_limit); |
||
| 248 | $return = $event; |
||
| 249 | return $return; |
||
| 250 | } catch (Exception $e) { |
||
| 251 | throw new RestException($e, $this->logger); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * |
||
| 257 | * {@inheritDoc} |
||
| 258 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents() |
||
| 259 | */ |
||
| 260 | public function getEvents( |
||
| 261 | $calendar = false, |
||
| 262 | $entry = false, |
||
| 263 | $location = [], |
||
| 264 | $tags = [], |
||
| 265 | $title = [], |
||
| 266 | $detail = [], |
||
| 267 | $from = null, |
||
| 268 | $to = null, |
||
| 269 | $includeLocation = false, |
||
| 270 | $includeAttachments = false, |
||
| 271 | $includeTickets = false, |
||
| 272 | $includeTicketsEvents = false, |
||
| 273 | $includeTicketsClassPasses = false): array |
||
| 274 | { |
||
| 275 | // Validate $tags. |
||
| 276 | if (!empty($tags)) { |
||
| 277 | if (!is_array($tags)) { |
||
| 278 | throw new ValidationException(); |
||
| 279 | } else { |
||
| 280 | $tags = array_unique($tags); |
||
| 281 | foreach ($tags as $tag) { |
||
| 282 | if (!empty($tag) && !$this->validator->validTag($tag)) { |
||
| 283 | throw new ValidationException(); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | $this->apiQuery['filter[tag]'] = implode(',', $tags); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Validate $from; |
||
| 291 | if (!empty($from)) { |
||
| 292 | if (!$this->validator->validFrom($from, $to)) { |
||
| 293 | throw new ValidationException('from', $from . '-' . $to); |
||
| 294 | } else { |
||
| 295 | $this->apiQuery['filter[from]'] = $from; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | // Validate $to; |
||
| 300 | if (!empty($to)) { |
||
| 301 | if (!$this->validator->validTo($to, $from)) { |
||
| 302 | throw new ValidationException('to', $to . '-' . $from); |
||
| 303 | } else { |
||
| 304 | $this->apiQuery['filter[to]'] = $to; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | // API resource. |
||
| 309 | $this->apiResource = $this->apiVersion . '/events'; |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | // Validate $includeLocation; |
||
| 314 | |||
| 315 | // Validate $includeTickets; |
||
| 316 | |||
| 317 | try { |
||
| 318 | $Response = $this->request(); |
||
| 319 | |||
| 320 | $body = json_decode($Response->getBody()->getContents()); |
||
| 321 | |||
| 322 | // Prepocess response onto nice model objects. |
||
| 323 | // @todo abstract. |
||
| 324 | $return = []; |
||
| 325 | |||
| 326 | foreach ($body->data as $event) { |
||
| 327 | // Add additional properties here. |
||
| 328 | $event->soldOut = (bool) ($event->attributes->attendee_count >= $event->attributes->attendee_limit); |
||
| 329 | array_push($return, $event); |
||
| 330 | } |
||
| 331 | |||
| 332 | return $return; |
||
| 333 | } catch (Exception $e) { |
||
| 334 | throw new RestException($e, $this->logger); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * |
||
| 340 | * {@inheritDoc} |
||
| 341 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation() |
||
| 342 | */ |
||
| 343 | public function getLocation($locationId) |
||
| 344 | { |
||
| 345 | $this->apiResource = $this->apiVersion . '/locations'; |
||
| 346 | if (!$this->Valdator->validId($locationId, 'location')) { |
||
| 347 | throw new ValidationException('locationId', $locationId); |
||
| 348 | } |
||
| 349 | |||
| 350 | try { |
||
| 351 | $Response = $this->request(); |
||
| 352 | $body = json_decode($Response->getBody()->getContents()); |
||
| 353 | $location = $body->data[0]; |
||
| 354 | $return = $location; |
||
| 355 | return $return; |
||
| 356 | } catch (Exception $e) { |
||
| 357 | throw new RestException($e->getMessage()); |
||
| 358 | } |
||
| 359 | |||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * |
||
| 364 | * {@inheritDoc} |
||
| 365 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocations() |
||
| 366 | * @todo validate params. |
||
| 367 | */ |
||
| 368 | public function getLocations($addressText = null, $additionalInfo = null): array |
||
| 369 | { |
||
| 370 | $this->apiResource = $this->apiVersion . '/locations'; |
||
| 371 | |||
| 372 | $return = []; |
||
| 373 | |||
| 374 | try { |
||
| 375 | $Response = $this->request(); |
||
| 376 | $body = json_decode($Response->getBody()->getContents()); |
||
| 377 | |||
| 378 | foreach ($body->data as $location) { |
||
| 379 | array_push($return, $location); |
||
| 380 | } |
||
| 381 | |||
| 382 | return $return; |
||
| 383 | } catch (Exception $e) { |
||
| 384 | throw new RestException($e, $this->logger); |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * |
||
| 390 | * {@inheritDoc} |
||
| 391 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getTicket() |
||
| 392 | */ |
||
| 393 | public function getTicket($ticketId) |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * |
||
| 415 | * {@inheritDoc} |
||
| 416 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getTickets() |
||
| 417 | */ |
||
| 418 | public function getTickets($eventId): array |
||
| 419 | { |
||
| 420 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 421 | throw new ValidationException('eventId', $eventId); |
||
| 422 | } |
||
| 423 | |||
| 424 | $this->apiQuery = ['event' => $eventId]; |
||
| 425 | |||
| 426 | $this->apiResource = $this->apiVersion . '/tickets'; |
||
| 427 | |||
| 428 | try { |
||
| 429 | $return = []; |
||
| 430 | |||
| 431 | $Response = $this->request(); |
||
| 432 | $body = json_decode($Response->getBody()->getContents()); |
||
| 433 | |||
| 434 | foreach ($body->data as $ticket) { |
||
| 435 | array_push($return, $ticket); |
||
| 436 | } |
||
| 437 | $this->logger->debug(var_export($return, true)); |
||
| 438 | return $return; |
||
| 439 | } catch (GuzzleHttp\Exception\ClientException $e) { |
||
| 440 | throw new RestException($e, $this->logger); |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Set Debug. |
||
| 446 | */ |
||
| 447 | public function setLogging($level) |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set Guzzle Client |
||
| 454 | */ |
||
| 455 | public function setGuzzleClient($guzzleClient) |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Sets the token for all future new instances |
||
| 462 | * @param $token string The API access token, as obtained on diffbot.com/dev. |
||
| 463 | */ |
||
| 464 | public static function setToken($token) |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | // EOF! |
||
| 475 |