| Total Complexity | 69 |
| Total Lines | 652 |
| 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( |
||
| 243 | string $eventId, |
||
| 244 | bool $attachments = false, |
||
| 245 | bool $location = false, |
||
| 246 | bool $tickets = false, |
||
| 247 | bool $ticketsClassPasses = false, |
||
| 248 | bool $ticketsEventslocation = false |
||
| 249 | ): Event { |
||
| 250 | //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')'); |
||
| 251 | |||
| 252 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 253 | throw new ValidationException('eventId', $eventId); |
||
| 254 | } |
||
| 255 | |||
| 256 | $event = $this->client->events()->retrieve($eventId); |
||
| 257 | |||
| 258 | $eventAttachments = []; |
||
| 259 | $eventTickets = []; |
||
| 260 | |||
| 261 | // attachments |
||
| 262 | if($attachments) { |
||
| 263 | foreach ($event->attachments as $eventAttachment) { |
||
| 264 | $attachment = $this->client->attachments()->retrieve($eventAttachment['id']); |
||
| 265 | array_push($eventAttachments, new Attachment( |
||
| 266 | $attachment->id, |
||
| 267 | $attachment->title, |
||
| 268 | )); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | // location |
||
| 273 | // @todo resolve dynamic type |
||
| 274 | if(!$location) { |
||
| 275 | $eventLocation = new Location( |
||
| 276 | null, |
||
| 277 | null, |
||
| 278 | $event->locationId |
||
| 279 | ); |
||
| 280 | } else { |
||
| 281 | $location = $this->client->locations()->retrieve($event->locationId); |
||
| 282 | $eventLocation = new Location( |
||
| 283 | $location->addressText, |
||
| 284 | $location->additionalInfo, |
||
| 285 | $location->id, |
||
| 286 | $location->latitude, |
||
| 287 | $location->longitude, |
||
| 288 | $location->mapUrl, |
||
| 289 | $location->zoom |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | // tickets |
||
| 294 | if($tickets) { |
||
| 295 | foreach ($event->tickets as $eventTicket) { |
||
| 296 | $ticket = $this->client->tickets()->retrieve($eventTicket['id']); |
||
| 297 | array_push($eventTickets, new Ticket( |
||
| 298 | $ticket->available, |
||
| 299 | $ticket->availableFrom, |
||
| 300 | $ticket->availableTo, |
||
| 301 | $ticket->builtBasketUrl, |
||
| 302 | $ticket->builtBasketIframeUrl, |
||
| 303 | $ticket->courseTicket, |
||
| 304 | $ticket->details, |
||
| 305 | $ticket->groupTicket, |
||
| 306 | $ticket->groupMin, |
||
| 307 | $ticket->groupMax, |
||
| 308 | $ticket->id, |
||
| 309 | $ticket->numberIssued, |
||
| 310 | $ticket->numberTaken, |
||
| 311 | $ticket->title |
||
| 312 | )); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | // ticketsClassPasses |
||
| 317 | // @todo |
||
| 318 | |||
| 319 | // ticketsEvents |
||
| 320 | // @todo |
||
| 321 | |||
| 322 | return $this->event = new Event( |
||
| 323 | $event->allDay, |
||
| 324 | $eventAttachments, |
||
| 325 | $event->attendeeCount, |
||
| 326 | $event->attendeeLimit, |
||
| 327 | $event->details, |
||
| 328 | $event->endAt, |
||
| 329 | $event->id, |
||
| 330 | $eventLocation, |
||
| 331 | $event->maxTicketsPerBooking, |
||
| 332 | $event->startAt, |
||
| 333 | $eventTickets, |
||
| 334 | $event->title, |
||
| 335 | $event->waitingList |
||
| 336 | ); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * |
||
| 341 | * {@inheritDoc} |
||
| 342 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents() |
||
| 343 | */ |
||
| 344 | public function Events( |
||
| 345 | $calendar = false, |
||
| 346 | $entry = false, |
||
| 347 | $location = [], |
||
| 348 | $tags = [], |
||
| 349 | $title = [], |
||
| 350 | $detail = [], |
||
| 351 | $from = null, |
||
| 352 | $to = null, |
||
| 353 | $includeAttachments = false, |
||
| 354 | $includeLocation = false, |
||
| 355 | $includeTickets = false, |
||
| 356 | $includeTicketsClassPasses = false, |
||
| 357 | $includeTicketsEvents = false |
||
| 358 | ): array { |
||
| 359 | |||
| 360 | //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')'); |
||
| 361 | |||
| 362 | // Validate $calendar |
||
| 363 | // @todo details required |
||
| 364 | |||
| 365 | // Validate $detail |
||
| 366 | if (!empty($detail)) { |
||
| 367 | if (!is_array($detail)) { |
||
| 368 | throw new ValidationException('detail', implode(' ', $detail)); |
||
| 369 | } else { |
||
| 370 | $detail = array_unique($detail); |
||
| 371 | foreach($detail as $item) { |
||
| 372 | if (!$this->validator->validLocation($item)) { |
||
| 373 | throw new ValidationException('detail', $item); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | $this->filters['filter[detail]'] = implode(',', $detail); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | // Validate $entry |
||
| 381 | // @todo details required |
||
| 382 | |||
| 383 | // Validate $from; |
||
| 384 | if (!empty($from)) { |
||
| 385 | if (!$this->validator->validFrom($from, $to)) { |
||
| 386 | throw new ValidationException('from', $from . '-' . $to); |
||
| 387 | } else { |
||
| 388 | $this->filters['filter[from]'] = $from; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | // Validate $location |
||
| 393 | if (!empty($location)) { |
||
| 394 | if (!is_array($location)) { |
||
| 395 | throw new ValidationException('location', implode(' ', $title)); |
||
| 396 | } else { |
||
| 397 | $location = array_unique($location); |
||
| 398 | foreach($location as $item) { |
||
| 399 | if (!$this->validator->validLocation($item)) { |
||
| 400 | throw new ValidationException('location', $item); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | $this->filters['filter[location]'] = implode(',', $location); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | // Validate $tags. |
||
| 408 | if (!empty($tags)) { |
||
| 409 | if (!is_array($tags)) { |
||
| 410 | throw new ValidationException('tags', implode(' ', $tags)); |
||
| 411 | } else { |
||
| 412 | $tags = array_unique($tags); |
||
| 413 | foreach ($tags as $tag) { |
||
| 414 | if (!empty($tag) && !$this->validator->validTag($tag)) { |
||
| 415 | throw new ValidationException('tag', $tag); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | $this->filters['filter[tag]'] = implode(',', $tags); |
||
| 420 | } |
||
| 421 | |||
| 422 | // Validate $title; |
||
| 423 | if (!empty($title)) { |
||
| 424 | if (!is_array($title)) { |
||
| 425 | throw new ValidationException('title', implode(' ', $title)); |
||
| 426 | } else { |
||
| 427 | $title = array_unique($title); |
||
| 428 | foreach($title as $item) { |
||
| 429 | if (!$this->validator->validTitle($item)) { |
||
| 430 | throw new ValidationException('title', $item); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | $this->filters['filter[title]'] = implode(',', $title); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | // Validate $to; |
||
| 438 | if (!empty($to)) { |
||
| 439 | if (!$this->validator->validTo($to, $from)) { |
||
| 440 | throw new ValidationException('to', $to . '-' . $from); |
||
| 441 | } else { |
||
| 442 | $this->filters['filter[to]'] = $to; |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | // Validate $includeAttachments; |
||
| 447 | if (!$this->validator->validInclude($includeAttachments)) { |
||
| 448 | throw new ValidationException('includeAttachments', $includeAttachments); |
||
| 449 | } else { |
||
| 450 | if($includeAttachments) { |
||
| 451 | array_push($this->includes, 'attachments'); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | // Validate $includeTickets; |
||
| 456 | if (!$this->validator->validInclude($includeLocation)) { |
||
| 457 | throw new ValidationException('includeLocation', $includeLocation); |
||
| 458 | } else { |
||
| 459 | if($includeLocation) { |
||
| 460 | array_push($this->includes, 'location'); |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | // Validate $includeTickets; |
||
| 465 | if (!$this->validator->validInclude($includeTickets)) { |
||
| 466 | throw new ValidationException('includeTickets', $includeTickets); |
||
| 467 | } else { |
||
| 468 | if($includeTickets) { |
||
| 469 | array_push($this->includes, 'tickets'); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | // Validate $includeTicketsEvents; |
||
| 474 | if (!$this->validator->validInclude($includeTicketsEvents)) { |
||
| 475 | throw new ValidationException('includeTicketsEvents', $includeTicketsEvents); |
||
| 476 | } else { |
||
| 477 | if($includeTickets) { |
||
| 478 | array_push($this->includes, 'tickets.events'); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | // Validate $includeTicketsClassPasses; |
||
| 483 | if (!$this->validator->validInclude($includeTicketsClassPasses)) { |
||
| 484 | throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses); |
||
| 485 | } else { |
||
| 486 | if($includeTicketsClassPasses) { |
||
| 487 | array_push($this->includes, 'tickets.class_passes'); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | $events = $this->client->events()->list(array_merge($this->filters, ['include' => implode(',', $this->includes)])); |
||
| 492 | |||
| 493 | $eventAttachments = []; |
||
| 494 | |||
| 495 | $eventTickets = []; |
||
| 496 | |||
| 497 | foreach ($events->data as $event) { |
||
| 498 | array_push($this->events, new Event( |
||
| 499 | $event->allDay, |
||
| 500 | $eventAttachments, |
||
| 501 | $event->attendeeCount, |
||
| 502 | $event->attendeeLimit, |
||
| 503 | $event->details, |
||
| 504 | $event->endAt, |
||
| 505 | $event->id, |
||
| 506 | new Location( |
||
| 507 | null, |
||
| 508 | null, |
||
| 509 | $event->locationId, |
||
| 510 | ), |
||
| 511 | $event->maxTicketsPerBooking, |
||
| 512 | $event->startAt, |
||
| 513 | $eventTickets, |
||
| 514 | $event->title, |
||
| 515 | $event->waitingList |
||
| 516 | )); |
||
| 517 | } |
||
| 518 | |||
| 519 | return $this->events; |
||
| 520 | } |
||
| 521 | |||
| 522 | /* |
||
| 523 | * |
||
| 524 | * {@inheritDoc} |
||
| 525 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation() |
||
| 526 | */ |
||
| 527 | public function location(string $locationId): Location |
||
| 528 | { |
||
| 529 | |||
| 530 | if (!$this->validator->validId($locationId, 'location')) { |
||
| 531 | throw new ValidationException('locationId', $locationId); |
||
| 532 | } |
||
| 533 | |||
| 534 | $location = $this->client->locations()->retrieve($locationId); |
||
| 535 | |||
| 536 | return $this->location = new Location( |
||
| 537 | $location->additionalInfo, |
||
| 538 | $location->addressText, |
||
| 539 | $location->id, |
||
| 540 | $location->latitude, |
||
| 541 | $location->longitude, |
||
| 542 | $location->mapUrl, |
||
| 543 | $location->zoom |
||
| 544 | ); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * |
||
| 549 | * {@inheritDoc} |
||
| 550 | * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocations() |
||
| 551 | * @todo validate params. |
||
| 552 | */ |
||
| 553 | public function locations( |
||
| 554 | null | string $addressText = null, |
||
| 555 | null | string $additionalInfo = null |
||
| 556 | ): array { |
||
| 557 | |||
| 558 | // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')'); |
||
| 559 | |||
| 560 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 561 | throw new ValidationException('eventId', $eventId); |
||
| 562 | } |
||
| 563 | |||
| 564 | $locations = $this->client->locations()->list(); |
||
| 565 | |||
| 566 | foreach ($locations->data as $location) { |
||
| 567 | array_push($this->locations, new Location( |
||
| 568 | $location->additionalInfo, |
||
| 569 | $location->addressText, |
||
| 570 | $location->id, |
||
| 571 | $location->latitude, |
||
| 572 | $location->longitude, |
||
| 573 | $location->mapUrl, |
||
| 574 | $location->zoom |
||
| 575 | )); |
||
| 576 | } |
||
| 577 | |||
| 578 | return $this->locations; |
||
| 579 | |||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Set Debug. |
||
| 584 | * @deprecated |
||
| 585 | */ |
||
| 586 | public function setLogging($level) |
||
| 587 | { |
||
| 588 | $this->logging = $level; |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Sets the token for all future new instances |
||
| 593 | * @deprecated |
||
| 594 | * @param $token string The API access token, as obtained on diffbot.com/dev. |
||
| 595 | */ |
||
| 596 | public static function setToken($token) |
||
| 597 | { |
||
| 598 | $validator = new Validator(); |
||
| 599 | if (!$validator->validToken($token)) { |
||
| 600 | throw new \InvalidArgumentException('Invalid Token.'); |
||
| 601 | } |
||
| 602 | self::$token = $token; |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * {@inheritDoc} |
||
| 607 | * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::ticket() |
||
| 608 | */ |
||
| 609 | public function ticket($ticketId): Ticket |
||
| 610 | { |
||
| 611 | if (!$this->validator->validId($ticketId, 'ticket')) { |
||
| 612 | throw new ValidationException('ticketId', $ticketId); |
||
| 613 | } |
||
| 614 | |||
| 615 | $ticket = $this->client->tickets()->retrieve($ticketId); |
||
| 616 | |||
| 617 | return $this->ticket = new Ticket( |
||
| 618 | $ticket->available, |
||
| 619 | $ticket->availableFrom, |
||
| 620 | $ticket->availableTo, |
||
| 621 | $ticket->builtBasketUrl, |
||
| 622 | $ticket->builtBasketIframeUrl, |
||
| 623 | $ticket->courseTicket, |
||
| 624 | $ticket->details, |
||
| 625 | $ticket->groupTicket, |
||
| 626 | $ticket->groupMin, |
||
| 627 | $ticket->groupMax, |
||
| 628 | $ticket->id, |
||
| 629 | $ticket->numberIssued, |
||
| 630 | $ticket->numberTaken, |
||
| 631 | $ticket->title |
||
| 632 | ); |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * {@inheritDoc} |
||
| 637 | * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::tickets() |
||
| 638 | * @todo includes |
||
| 639 | */ |
||
| 640 | public function tickets(string $eventId): array |
||
| 673 | |||
| 674 | } |
||
| 675 | } |
||
| 676 |
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