| Total Complexity | 69 |
| Total Lines | 660 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Bookwhen 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 Bookwhen, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | final class Bookwhen implements BookwhenInterface |
||
| 22 | { |
||
| 23 | private null | Client $client; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * @var unknown |
||
| 28 | */ |
||
| 29 | public Attachment $attachment; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var unknown |
||
| 34 | */ |
||
| 35 | public array $attachments = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * |
||
| 39 | * @var unknown |
||
| 40 | */ |
||
| 41 | public ClassPass $classPass; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * @var unknown |
||
| 46 | */ |
||
| 47 | public array $classPasses = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * @var unknown |
||
| 52 | */ |
||
| 53 | public Event $event; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | * @var unknown |
||
| 58 | */ |
||
| 59 | public array $events = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * |
||
| 63 | * @var unknown |
||
| 64 | */ |
||
| 65 | private array $filters = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | public readonly Location $location; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * |
||
| 74 | * @var unknown |
||
| 75 | */ |
||
| 76 | private array $includes = []; |
||
| 77 | /** |
||
| 78 | |||
| 79 | */ |
||
| 80 | public Ticket $ticket; |
||
| 81 | |||
| 82 | /** |
||
| 83 | |||
| 84 | */ |
||
| 85 | public array $tickets = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | public $locations = []; |
||
| 91 | |||
| 92 | |||
| 93 | /** @var string The path to the log file */ |
||
| 94 | private $logFile; |
||
| 95 | |||
| 96 | /** @var object loging object. */ |
||
| 97 | private $logger; |
||
| 98 | |||
| 99 | /** @var string the logging level. */ |
||
| 100 | private string $logLevel; |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Creates a new Bookwhen Client with the given API token. |
||
| 106 | * @todo logging |
||
| 107 | */ |
||
| 108 | public function __construct( |
||
| 109 | private $validator = new Validator() |
||
| 110 | ) { |
||
| 111 | // $this->logFile = $logFile; |
||
| 112 | // $this->logLevel = $logLevel; |
||
| 113 | // $this->logger = new Logger('inShore Bookwhen API'); |
||
| 114 | // $this->logger->pushHandler(new StreamHandler($this->logFile, $this->logLevel)); |
||
| 115 | $this->client = BookwhenApi::client($_ENV['INSHORE_BOOKWHEN_API_KEY']); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * |
||
| 120 | * {@inheritDoc} |
||
| 121 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::attachment() |
||
| 122 | * @todo all attachment properties |
||
| 123 | */ |
||
| 124 | public function attachment(string $attachmentId): Attachment |
||
| 125 | { |
||
| 126 | if (!$this->validator->validId($attachmentId, 'attachment')) { |
||
| 127 | throw new ValidationException('attachmentId', $attachmentId); |
||
| 128 | } |
||
| 129 | |||
| 130 | $attachment = $this->client->attachments()->retrieve($attachmentId); |
||
| 131 | |||
| 132 | return $this->attachment = new Attachment( |
||
| 133 | $attachment->id, |
||
| 134 | $attachment->title, |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * |
||
| 140 | * {@inheritDoc} |
||
| 141 | * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::attachment() |
||
| 142 | * @todo |
||
| 143 | */ |
||
| 144 | public function attachments( |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * |
||
| 176 | * {@inheritDoc} |
||
| 177 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass() |
||
| 178 | * @todo |
||
| 179 | */ |
||
| 180 | public function classPass(string $classPassId): ClassPass |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * |
||
| 203 | * {@inheritDoc} |
||
| 204 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses() |
||
| 205 | * @todo break params on to multiplper lines.. |
||
| 206 | */ |
||
| 207 | public function classPasses( |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * |
||
| 238 | * {@inheritDoc} |
||
| 239 | * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::event() |
||
| 240 | * @todo filters. |
||
| 241 | */ |
||
| 242 | public function event( |
||
| 341 | ); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * |
||
| 346 | * {@inheritDoc} |
||
| 347 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents() |
||
| 348 | */ |
||
| 349 | public function events( |
||
| 350 | $calendar = false, |
||
| 351 | $entry = false, |
||
| 352 | $location = [], |
||
| 353 | $tags = [], |
||
| 354 | $title = [], |
||
| 355 | $detail = [], |
||
| 356 | $from = null, |
||
| 357 | $to = null, |
||
| 358 | $includeAttachments = false, |
||
| 359 | $includeLocation = false, |
||
| 360 | $includeTickets = false, |
||
| 361 | $includeTicketsClassPasses = false, |
||
| 362 | $includeTicketsEvents = false |
||
| 363 | ): array { |
||
| 364 | |||
| 365 | //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')'); |
||
| 366 | |||
| 367 | // Validate $calendar |
||
| 368 | // @todo details required |
||
| 369 | |||
| 370 | // Validate $detail |
||
| 371 | if (!empty($detail)) { |
||
| 372 | if (!is_array($detail)) { |
||
| 373 | throw new ValidationException('detail', implode(' ', $detail)); |
||
| 374 | } else { |
||
| 375 | $detail = array_unique($detail); |
||
| 376 | foreach($detail as $item) { |
||
| 377 | if (!$this->validator->validLocation($item)) { |
||
| 378 | throw new ValidationException('detail', $item); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | $this->filters['filter[detail]'] = implode(',', $detail); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | // Validate $entry |
||
| 386 | // @todo details required |
||
| 387 | |||
| 388 | // Validate $from; |
||
| 389 | if (!empty($from)) { |
||
| 390 | if (!$this->validator->validFrom($from, $to)) { |
||
| 391 | throw new ValidationException('from', $from . '-' . $to); |
||
| 392 | } else { |
||
| 393 | $this->filters['filter[from]'] = $from; |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | // Validate $location |
||
| 398 | if (!empty($location)) { |
||
| 399 | if (!is_array($location)) { |
||
| 400 | throw new ValidationException('location', implode(' ', $title)); |
||
| 401 | } else { |
||
| 402 | $location = array_unique($location); |
||
| 403 | foreach($location as $item) { |
||
| 404 | if (!$this->validator->validLocation($item)) { |
||
| 405 | throw new ValidationException('location', $item); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | $this->filters['filter[location]'] = implode(',', $location); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | // Validate $tags. |
||
| 413 | if (!empty($tags)) { |
||
| 414 | if (!is_array($tags)) { |
||
| 415 | throw new ValidationException('tags', implode(' ', $tags)); |
||
| 416 | } else { |
||
| 417 | $tags = array_unique($tags); |
||
| 418 | foreach ($tags as $tag) { |
||
| 419 | if (!empty($tag) && !$this->validator->validTag($tag)) { |
||
| 420 | throw new ValidationException('tag', $tag); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | } |
||
| 424 | $this->filters['filter[tag]'] = implode(',', $tags); |
||
| 425 | } |
||
| 426 | |||
| 427 | // Validate $title; |
||
| 428 | if (!empty($title)) { |
||
| 429 | if (!is_array($title)) { |
||
| 430 | throw new ValidationException('title', implode(' ', $title)); |
||
| 431 | } else { |
||
| 432 | $title = array_unique($title); |
||
| 433 | foreach($title as $item) { |
||
| 434 | if (!$this->validator->validTitle($item)) { |
||
| 435 | throw new ValidationException('title', $item); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | $this->filters['filter[title]'] = implode(',', $title); |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | // Validate $to; |
||
| 443 | if (!empty($to)) { |
||
| 444 | if (!$this->validator->validTo($to, $from)) { |
||
| 445 | throw new ValidationException('to', $to . '-' . $from); |
||
| 446 | } else { |
||
| 447 | $this->filters['filter[to]'] = $to; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | // Validate $includeAttachments; |
||
| 452 | if (!$this->validator->validInclude($includeAttachments)) { |
||
| 453 | throw new ValidationException('includeAttachments', $includeAttachments); |
||
| 454 | } else { |
||
| 455 | if($includeAttachments) { |
||
| 456 | array_push($this->includes, 'attachments'); |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | // Validate $includeTickets; |
||
| 461 | if (!$this->validator->validInclude($includeLocation)) { |
||
| 462 | throw new ValidationException('includeLocation', $includeLocation); |
||
| 463 | } else { |
||
| 464 | if($includeLocation) { |
||
| 465 | array_push($this->includes, 'location'); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | // Validate $includeTickets; |
||
| 470 | if (!$this->validator->validInclude($includeTickets)) { |
||
| 471 | throw new ValidationException('includeTickets', $includeTickets); |
||
| 472 | } else { |
||
| 473 | if($includeTickets) { |
||
| 474 | array_push($this->includes, 'tickets'); |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | // Validate $includeTicketsEvents; |
||
| 479 | if (!$this->validator->validInclude($includeTicketsEvents)) { |
||
| 480 | throw new ValidationException('includeTicketsEvents', $includeTicketsEvents); |
||
| 481 | } else { |
||
| 482 | if($includeTickets) { |
||
| 483 | array_push($this->includes, 'tickets.events'); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | // Validate $includeTicketsClassPasses; |
||
| 488 | if (!$this->validator->validInclude($includeTicketsClassPasses)) { |
||
| 489 | throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses); |
||
| 490 | } else { |
||
| 491 | if($includeTicketsClassPasses) { |
||
| 492 | array_push($this->includes, 'tickets.class_passes'); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | $events = $this->client->events()->list(array_merge($this->filters, ['include' => implode(',', $this->includes)])); |
||
| 497 | |||
| 498 | foreach ($events->data as $event) { |
||
| 499 | |||
| 500 | $eventLocation = new Location( |
||
| 501 | $event->location->addressText, |
||
| 502 | $event->location->additionalInfo, |
||
| 503 | $event->location->id, |
||
| 504 | $event->location->latitude, |
||
| 505 | $event->location->longitude, |
||
| 506 | $event->location->mapUrl, |
||
| 507 | $event->location->zoom |
||
| 508 | ); |
||
| 509 | |||
| 510 | array_push($this->events, new Event( |
||
| 511 | $event->allDay, |
||
| 512 | $event->attachments, |
||
| 513 | $event->attendeeCount, |
||
| 514 | $event->attendeeLimit, |
||
| 515 | $event->details, |
||
| 516 | $event->endAt, |
||
| 517 | $event->id, |
||
| 518 | $eventLocation, |
||
| 519 | $event->maxTicketsPerBooking, |
||
| 520 | $event->startAt, |
||
| 521 | $event->tickets, |
||
| 522 | $event->title, |
||
| 523 | $event->waitingList |
||
| 524 | )); |
||
| 525 | } |
||
| 526 | |||
| 527 | return $this->events; |
||
| 528 | } |
||
| 529 | |||
| 530 | /* |
||
| 531 | * |
||
| 532 | * {@inheritDoc} |
||
| 533 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation() |
||
| 534 | */ |
||
| 535 | public function location(string $locationId): Location |
||
| 536 | { |
||
| 537 | |||
| 538 | if (!$this->validator->validId($locationId, 'location')) { |
||
| 539 | throw new ValidationException('locationId', $locationId); |
||
| 540 | } |
||
| 541 | |||
| 542 | $location = $this->client->locations()->retrieve($locationId); |
||
| 543 | |||
| 544 | return $this->location = new Location( |
||
| 545 | $location->additionalInfo, |
||
| 546 | $location->addressText, |
||
| 547 | $location->id, |
||
| 548 | $location->latitude, |
||
| 549 | $location->longitude, |
||
| 550 | $location->mapUrl, |
||
| 551 | $location->zoom |
||
| 552 | ); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * |
||
| 557 | * {@inheritDoc} |
||
| 558 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocations() |
||
| 559 | * @todo validate params. |
||
| 560 | */ |
||
| 561 | public function locations( |
||
| 562 | null | string $addressText = null, |
||
| 563 | null | string $additionalInfo = null |
||
| 564 | ): array { |
||
| 565 | |||
| 566 | // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')'); |
||
| 567 | |||
| 568 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 569 | throw new ValidationException('eventId', $eventId); |
||
| 570 | } |
||
| 571 | |||
| 572 | $locations = $this->client->locations()->list(); |
||
| 573 | |||
| 574 | foreach ($locations->data as $location) { |
||
| 575 | array_push($this->locations, new Location( |
||
| 576 | $location->additionalInfo, |
||
| 577 | $location->addressText, |
||
| 578 | $location->id, |
||
| 579 | $location->latitude, |
||
| 580 | $location->longitude, |
||
| 581 | $location->mapUrl, |
||
| 582 | $location->zoom |
||
| 583 | )); |
||
| 584 | } |
||
| 585 | |||
| 586 | return $this->locations; |
||
| 587 | |||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Set Debug. |
||
| 592 | * @deprecated |
||
| 593 | */ |
||
| 594 | public function setLogging($level) |
||
| 595 | { |
||
| 596 | $this->logging = $level; |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Sets the token for all future new instances |
||
| 601 | * @deprecated |
||
| 602 | * @param $token string The API access token, as obtained on diffbot.com/dev. |
||
| 603 | */ |
||
| 604 | public static function setToken($token) |
||
| 605 | { |
||
| 606 | $validator = new Validator(); |
||
| 607 | if (!$validator->validToken($token)) { |
||
| 608 | throw new \InvalidArgumentException('Invalid Token.'); |
||
| 609 | } |
||
| 610 | self::$token = $token; |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * {@inheritDoc} |
||
| 615 | * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::ticket() |
||
| 616 | */ |
||
| 617 | public function ticket($ticketId): Ticket |
||
| 618 | { |
||
| 619 | if (!$this->validator->validId($ticketId, 'ticket')) { |
||
| 620 | throw new ValidationException('ticketId', $ticketId); |
||
| 621 | } |
||
| 622 | |||
| 623 | $ticket = $this->client->tickets()->retrieve($ticketId); |
||
| 624 | |||
| 625 | return $this->ticket = new Ticket( |
||
| 626 | $ticket->available, |
||
| 627 | $ticket->availableFrom, |
||
| 628 | $ticket->availableTo, |
||
| 629 | $ticket->builtBasketUrl, |
||
| 630 | $ticket->builtBasketIframeUrl, |
||
| 631 | $ticket->courseTicket, |
||
| 632 | $ticket->details, |
||
| 633 | $ticket->groupTicket, |
||
| 634 | $ticket->groupMin, |
||
| 635 | $ticket->groupMax, |
||
| 636 | $ticket->id, |
||
| 637 | $ticket->numberIssued, |
||
| 638 | $ticket->numberTaken, |
||
| 639 | $ticket->title |
||
| 640 | ); |
||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * {@inheritDoc} |
||
| 645 | * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::tickets() |
||
| 646 | * @todo includes |
||
| 647 | */ |
||
| 648 | public function tickets(string $eventId): array |
||
| 681 | |||
| 682 | } |
||
| 683 | } |
||
| 684 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths