Passed
Pull Request — develop (#169)
by
unknown
01:37
created

Bookwhen   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 831
Duplicated Lines 0 %

Test Coverage

Coverage 73.97%

Importance

Changes 15
Bugs 1 Features 0
Metric Value
eloc 390
c 15
b 1
f 0
dl 0
loc 831
ccs 287
cts 388
cp 0.7397
rs 2
wmc 103

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 5
A attachment() 0 17 2
C tickets() 0 83 13
A locations() 0 36 6
A location() 0 17 2
B event() 0 116 9
B attachments() 0 42 8
A classPass() 0 17 2
C ticket() 0 72 12
C classPasses() 0 53 12
F events() 0 188 32

How to fix   Complexity   

Complex Class

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
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen;
6
7
use InShore\Bookwhen\BookwhenApi;
8
use InShore\Bookwhen\Client;
9
use InShore\Bookwhen\Domain\Attachment;
10
use InShore\Bookwhen\Domain\ClassPass;
11
use InShore\Bookwhen\Domain\Event;
12
use InShore\Bookwhen\Domain\Location;
13
use InShore\Bookwhen\Domain\Ticket;
14
use InShore\Bookwhen\Exceptions\ConfigurationException;
15
use InShore\Bookwhen\Exceptions\ValidationException;
16
use InShore\Bookwhen\Interfaces\BookwhenInterface;
17
use InShore\Bookwhen\Validator;
18
use Monolog\Level;
19
use Monolog\Logger;
20
use Monolog\Handler\StreamHandler;
21
22
final class Bookwhen implements BookwhenInterface
23
{
24
    /**
25
     *
26
     */
27
    public Attachment $attachment;
28
29
    /**
30
     *
31
     */
32
    public array $attachments = [];
33
34
    /**
35
     *
36
     */
37
    public Client $client;
38
39
    /**
40
     *
41
     */
42
    public ClassPass $classPass;
43
44
    /**
45
     *
46
     */
47
    public array $classPasses = [];
48
49
    /**
50
     *
51
     */
52
    public Event $event;
53
54
    /**
55
     *
56
     */
57
    public array $events = [];
58
59
    /**
60
     *
61
     */
62
    private array $filters = [];
63
64
    /**
65
     *
66
     */
67
    public Location $location;
68
69
    /**
70
     *
71
     */
72
    public array $includes = [];
73
74
    /**
75
     *
76
     */
77
    public Ticket $ticket;
78
79
    /**
80
     *
81
     */
82
    public array $tickets = [];
83
84
    /**
85
     *
86
     */
87
    public $locations = [];
88
89
90
    /** @var string The path to the log file */
91
    //private $logFile;
92
93
    /** @var object loging object. */
94
    //private $logger;
95
96
    /** @var string the logging level. */
97
    //private string $logLevel;
98
99
    /**
100
     * Creates a new Bookwhen Client with the given API token.
101
     * @throws ConfigurationException
102
     * @todo logging
103
     */
104 20
    public function __construct(
105
        string $apiKey = null,
106
        Client $client = null,
107
        private $validator = new Validator()
108
    ) {
109
        //         $this->logFile = $logFile;
110
        //         $this->logLevel = $logLevel;
111
        //         $this->logger = new Logger('inShore Bookwhen API');
112
        //         $this->logger->pushHandler(new StreamHandler($this->logFile, $this->logLevel));
113
        try {
114 20
            if(!is_null($client)) {
115 11
                $this->client = $client;
116
            } else {
117 9
                $this->client = !is_null($apiKey)
118 9
                ? BookwhenApi::client($apiKey)
119 1
                : (array_key_exists('INSHORE_BOOKWHEN_API_KEY', $_ENV)
120
                    ? BookwhenApi::client($_ENV['INSHORE_BOOKWHEN_API_KEY'])
121 20
                    : throw new ConfigurationException());
122
            }
123 1
        } catch (\TypeError $e) {
124
            throw new ConfigurationException(); // @todo message
125
        }
126
    }
127
128
    /**
129
     *
130
     * {@inheritDoc}
131
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::attachment()
132
     * @todo all attachment properties
133
     */
134 3
    public function attachment(string $attachmentId): Attachment
135
    {
136 3
        if (!$this->validator->validId($attachmentId, 'attachment')) {
137 2
            throw new ValidationException('attachmentId', $attachmentId);
138
        }
139
140 1
        $attachment = $this->client->attachments()->retrieve($attachmentId);
141
142 1
        return $this->attachment = new Attachment(
143 1
            $attachment->contentType,
144 1
            $attachment->fileUrl,
145 1
            $attachment->fileSizeBytes,
146 1
            $attachment->fileSizeText,
147 1
            $attachment->fileName,
148 1
            $attachment->fileType,
149 1
            $attachment->id,
150 1
            $attachment->title,
151 1
        );
152
    }
153
154
    /**
155
     *
156
     * {@inheritDoc}
157
     *
158
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::attachment()
159
     */
160 1
    public function attachments(
161
        string $title = null,
162
        string $fileName = null,
163
        string $fileType = null
164
    ): array {
165
166 1
        if (!is_null($title)) {
167
            if ($this->validator->validTitle($title)) {
168
                $this->filters['filter[title]'] = $title;
169
            } else {
170
                throw new ValidationException('title', $title);
171
            }
172
        }
173
174 1
        if (!is_null($fileName) && !$this->validator->validFileName($fileName)) {
175
            throw new ValidationException('file name', $fileName);
176
        } else {
177 1
            $this->filters['filter[file_name]'] = $fileName;
178
        }
179
180 1
        if (!is_null($fileType) && !$this->validator->validFileType($fileType)) {
181
            throw new ValidationException('file type', $fileType);
182
        } else {
183 1
            $this->filters['filter[file_type]'] = $fileType;
184
        }
185
186 1
        $attachments = $this->client->attachments()->list($this->filters);
187
188 1
        foreach ($attachments->data as $attachment) {
189 1
            array_push($this->attachments, new Attachment(
190 1
                $attachment->contentType,
191 1
                $attachment->fileUrl,
192 1
                $attachment->fileSizeBytes,
193 1
                $attachment->fileSizeText,
194 1
                $attachment->fileName,
195 1
                $attachment->fileType,
196 1
                $attachment->id,
197 1
                $attachment->title
198 1
            ));
199
        }
200
201 1
        return $this->attachments;
202
    }
203
204
    /**
205
     *
206
     * {@inheritDoc}
207
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass()
208
     * @todo
209
     */
210 1
    public function classPass(string $classPassId): ClassPass
211
    {
212
213 1
        if (!$this->validator->validId($classPassId, 'classPass')) {
214
            throw new ValidationException('classPassId', $classPassId);
215
        }
216
217 1
        $classPass = $this->client->classPasses()->retrieve($classPassId);
218
219 1
        return new ClassPass(
220 1
            $classPass->details,
221 1
            $classPass->id,
222 1
            $classPass->numberAvailable,
223 1
            $classPass->title,
224 1
            $classPass->usageAllowance,
225 1
            $classPass->usageType,
226 1
            $classPass->useRestrictedForDays
227 1
        );
228
    }
229
230
    /**
231
     *
232
     * {@inheritDoc}
233
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses()
234
     */
235 1
    public function classPasses(
236
        $cost = null,
237
        $detail = null,
238
        $title = null,
239
        $usageAllowance = null,
240
        $usageType = null,
241
        $useRestrictedForDays = null
242
    ): array {
243
244 1
        if (!is_null($detail) && !$this->validator->validDetails($detail)) {
245
            throw new ValidationException('detail', $detail);
246
        } else {
247 1
            $this->filters['filter[detail]'] = $detail;
248
        }
249
250 1
        if (!is_null($title) && !$this->validator->validTitle($title)) {
251
            throw new ValidationException('title', $title);
252
        } else {
253 1
            $this->filters['filter[title]'] = $title;
254
        }
255
256 1
        if (!is_null($usageAllowance) && !$this->validator->validUsageAllowance($usageAllowance)) {
257
            throw new ValidationException('usageAllowance', $usageAllowance);
258
        } else {
259 1
            $this->filters['filter[usage_allowance]'] = $usageAllowance;
260
        }
261
262 1
        if (!is_null($usageType) && !$this->validator->validUsageType($usageType)) {
263
            throw new ValidationException('usageType', $usageType);
264
        } else {
265 1
            $this->filters['filter[usage_type]'] = $usageType;
266
        }
267
268 1
        if (!is_null($useRestrictedForDays) && !$this->validator->validUseRestrictedForDays($useRestrictedForDays)) {
269
            throw new ValidationException('useRestrictedForDays', $useRestrictedForDays);
270
        } else {
271 1
            $this->filters['filter[use_restricted_for_days]'] = $useRestrictedForDays;
272
        }
273
274 1
        $classPasses = $this->client->classPasses()->list($this->filters);
275
276 1
        foreach ($classPasses->data as $classPass) {
277 1
            array_push($this->classPasses, new ClassPass(
278 1
                $classPass->details,
279 1
                $classPass->id,
280 1
                $classPass->numberAvailable,
281 1
                $classPass->title,
282 1
                $classPass->usageAllowance,
283 1
                $classPass->usageType,
284 1
                $classPass->useRestrictedForDays,
285 1
            ));
286
        }
287 1
        return $this->classPasses;
288
    }
289
290
    /**
291
     *
292
     * {@inheritDoc}
293
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::event()
294
     */
295 2
    public function event(
296
        string $eventId,
297
        bool $includeAttachments = false,
298
        bool $includeLocation = false,
299
        bool $includeTickets = false,
300
        bool $includeTicketsClassPasses = false,
301
        bool $includeTicketsEvents = false
302
    ): Event {
303
304 2
        if (!$this->validator->validId($eventId, 'event')) {
305
            throw new ValidationException('eventId', $eventId);
306
        }
307
308
        // Validate $includeAttachments;
309 2
        if (!$this->validator->validInclude($includeAttachments)) {
310
            throw new ValidationException('includeAttachments', $includeAttachments);
311
        }
312
313
        // Validate $includeTickets;
314 2
        if (!$this->validator->validInclude($includeLocation)) {
315
            throw new ValidationException('includeLocation', $includeLocation);
316
        }
317
318
        // Validate $includeTickets;
319 2
        if (!$this->validator->validInclude($includeTickets)) {
320
            throw new ValidationException('includeTickets', $includeTickets);
321
        }
322
323
        // Validate $includeTicketsEvents;
324 2
        if (!$this->validator->validInclude($includeTicketsEvents)) {
325
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
326
        }
327
328
        // Validate $includeTicketsClassPasses;
329 2
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
330
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
331
        }
332
333 2
        $includesMapping = [
334 2
            'attachments' => $includeAttachments,
335 2
            'location' => $includeLocation,
336 2
            'tickets' => $includeTickets,
337 2
            'tickets.events' => $includeTicketsEvents,
338 2
            'tickets.class_passes' => $includeTicketsClassPasses,
339 2
        ];
340
341 2
        $this->includes = array_keys(array_filter($includesMapping, function ($value) {
342 2
            return $value;
343 2
        }));
344
345 2
        $event = $this->client->events()->retrieve($eventId, ['include' => implode(',', $this->includes)]);
346
347
        // attachments
348 2
        $eventAttachments = [];
349
350 2
        foreach ($event->attachments as $eventAttachment) {
351
            $attachment = $this->client->attachments()->retrieve($eventAttachment->id);
352
            array_push($eventAttachments, new Attachment(
353
                $attachment->contentType,
354
                $attachment->fileUrl,
355
                $attachment->fileSizeBytes,
356
                $attachment->fileSizeText,
357
                $attachment->fileName,
358
                $attachment->fileType,
359
                $attachment->id,
360
                $attachment->title
361
            ));
362
        }
363
364
        // eventTickets
365 2
        $eventTickets = [];
366 2
        foreach ($event->tickets as $ticket) {
367 2
            array_push($eventTickets, new Ticket(
368 2
                $ticket->available,
369 2
                $ticket->availableFrom,
370 2
                $ticket->availableTo,
371 2
                $ticket->builtBasketUrl,
372 2
                $ticket->builtBasketIframeUrl,
373 2
                $ticket->cost,
374 2
                $ticket->courseTicket,
375 2
                $ticket->details,
376 2
                $ticket->groupTicket,
377 2
                $ticket->groupMin,
378 2
                $ticket->groupMax,
379 2
                $ticket->id,
380 2
                $ticket->numberIssued,
381 2
                $ticket->numberTaken,
382 2
                $ticket->title
383 2
            ));
384
        }
385
386
        // ticketsClassPasses
387
        // @todo
388
389 2
        return $this->event = new Event(
390 2
            $event->allDay,
391 2
            $eventAttachments,
392 2
            $event->attendeeCount,
393 2
            $event->attendeeLimit,
394 2
            $event->details,
395 2
            $event->endAt,
396 2
            $event->id,
397 2
            new Location(
398 2
                $event->location->additionalInfo,
399 2
                $event->location->addressText,
400 2
                $event->location->id,
401 2
                $event->location->latitude,
402 2
                $event->location->longitude,
403 2
                $event->location->mapUrl,
404 2
                $event->location->zoom
405 2
            ),
406 2
            $event->maxTicketsPerBooking,
407 2
            $event->startAt,
408 2
            $eventTickets,
409 2
            $event->title,
410 2
            $event->waitingList
411 2
        );
412
    }
413
414
    /**
415
     *
416
     * {@inheritDoc}
417
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents()
418
     */
419 1
    public function events(
420
        $calendar = false,
421
        $entry = false,
422
        $location = [],
423
        $tags = [],
424
        $title = [],
425
        $detail = [],
426
        $from = null,
427
        $to = null,
428
        bool $includeAttachments = false,
429
        bool $includeLocation = false,
430
        bool $includeTickets = false,
431
        bool $includeTicketsClassPasses = false,
432
        bool $includeTicketsEvents = false
433
    ): array {
434
435
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
436
437
        // Validate $calendar
438
        // @todo details required
439
440
        // Validate $detail
441 1
        if (!empty($detail)) {
442
            if (!is_array($detail)) {
443
                throw new ValidationException('detail', implode(' ', $detail));
444
            } else {
445
                $detail = array_unique($detail);
446
                foreach ($detail as $item) {
447
                    if (!$this->validator->validLocation($item)) {
448
                        throw new ValidationException('detail', $item);
449
                    }
450
                }
451
                $this->filters['filter[detail]'] = implode(',', $detail);
452
            }
453
        }
454
455
        // Validate $entry
456
        // @todo details required
457
458
        // Validate $from;
459 1
        if (!empty($from)) {
460
            if (!$this->validator->validFrom($from, $to)) {
461
                throw new ValidationException('from', $from . '-' . $to);
462
            } else {
463
                $this->filters['filter[from]'] = $from;
464
            }
465
        }
466
467
        // Validate $location
468 1
        if (!empty($location)) {
469
            if (!is_array($location)) {
470
                throw new ValidationException('location', implode(' ', $title));
471
            } else {
472
                $location = array_unique($location);
473
                foreach ($location as $item) {
474
                    if (!$this->validator->validLocation($item)) {
475
                        throw new ValidationException('location', $item);
476
                    }
477
                }
478
                $this->filters['filter[location]'] = implode(',', $location);
479
            }
480
        }
481
482
        // Validate $tags.
483 1
        if (!empty($tags)) {
484
            if (!is_array($tags)) {
485
                throw new ValidationException('tags', implode(' ', $tags));
486
            } else {
487
                $tags = array_unique($tags);
488
                foreach ($tags as $tag) {
489
                    if (!empty($tag) && !$this->validator->validTag($tag)) {
490
                        throw new ValidationException('tag', $tag);
491
                    }
492
                }
493
            }
494
            $this->filters['filter[tag]'] = implode(',', $tags);
495
        }
496
497
        // Validate $title;
498 1
        if (!empty($title)) {
499
            if (!is_array($title)) {
500
                throw new ValidationException('title', implode(' ', $title));
501
            } else {
502
                $title = array_unique($title);
503
                foreach ($title as $item) {
504
                    if (!$this->validator->validTitle($item)) {
505
                        throw new ValidationException('title', $item);
506
                    }
507
                }
508
                $this->filters['filter[title]'] = implode(',', $title);
509
            }
510
        }
511
512
        // Validate $to;
513 1
        if (!empty($to)) {
514
            if (!$this->validator->validTo($to, $from)) {
515
                throw new ValidationException('to', $to . '-' . $from);
516
            } else {
517
                $this->filters['filter[to]'] = $to;
518
            }
519
        }
520
521
        // Validate $includeTickets;
522 1
        if (!$this->validator->validInclude($includeLocation)) {
523
            throw new ValidationException('includeLocation', $includeLocation);
524
        }
525 1
        if ($includeLocation) {
526
            array_push($this->includes, 'location');
527
        }
528
529
        // Validate $includeTickets;
530 1
        if (!$this->validator->validInclude($includeTickets)) {
531
            throw new ValidationException('includeTickets', $includeTickets);
532
        }
533
534 1
        if ($includeTickets) {
535
            array_push($this->includes, 'tickets');
536
        }
537
538
        // Validate $includeTicketsEvents;
539 1
        if (!$this->validator->validInclude($includeTicketsEvents)) {
540
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
541
        }
542
543 1
        if ($includeTicketsEvents) {
544
            array_push($this->includes, 'tickets.events');
545
        }
546
547
        // Validate $includeTicketsClassPasses;
548 1
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
549
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
550
        }
551
552 1
        if ($includeTicketsClassPasses) {
553
            array_push($this->includes, 'tickets.class_passes');
554
        }
555
556 1
        $events = $this->client->events()->list(array_merge($this->filters, ['include' => implode(',', $this->includes)]));
557
558 1
        foreach ($events->data as $event) {
559
560 1
            $eventTickets = [];
561 1
            foreach ($event->tickets as $ticket) {
562 1
                array_push($eventTickets, new Ticket(
563 1
                    $ticket->available,
564 1
                    $ticket->availableFrom,
565 1
                    $ticket->availableTo,
566 1
                    $ticket->builtBasketUrl,
567 1
                    $ticket->builtBasketIframeUrl,
568 1
                    $ticket->cost,
569 1
                    $ticket->courseTicket,
570 1
                    $ticket->details,
571 1
                    $ticket->groupTicket,
572 1
                    $ticket->groupMin,
573 1
                    $ticket->groupMax,
574 1
                    $ticket->id,
575 1
                    $ticket->numberIssued,
576 1
                    $ticket->numberTaken,
577 1
                    $ticket->title
578 1
                ));
579
            }
580
581 1
            array_push($this->events, new Event(
582 1
                $event->allDay,
583 1
                $event->attachments,
584 1
                $event->attendeeCount,
585 1
                $event->attendeeLimit,
586 1
                $event->details,
587 1
                $event->endAt,
588 1
                $event->id,
589 1
                new Location(
590 1
                    $event->location->additionalInfo,
591 1
                    $event->location->addressText,
592 1
                    $event->location->id,
593 1
                    $event->location->latitude,
594 1
                    $event->location->longitude,
595 1
                    $event->location->mapUrl,
596 1
                    $event->location->zoom
597 1
                ),
598 1
                $event->maxTicketsPerBooking,
599 1
                $event->startAt,
600 1
                $eventTickets,
601 1
                $event->title,
602 1
                $event->waitingList
603 1
            ));
604
        }
605
606 1
        return $this->events;
607
    }
608
609
    /*
610
     *
611
     * {@inheritDoc}
612
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation()
613
     */
614 1
    public function location(string $locationId): Location
615
    {
616
617 1
        if (!$this->validator->validId($locationId, 'location')) {
618
            throw new ValidationException('locationId', $locationId);
619
        }
620
621 1
        $location = $this->client->locations()->retrieve($locationId);
622
623 1
        return $this->location = new Location(
624 1
            $location->additionalInfo,
625 1
            $location->addressText,
626 1
            $location->id,
627 1
            $location->latitude,
628 1
            $location->longitude,
629 1
            $location->mapUrl,
630 1
            $location->zoom
631 1
        );
632
    }
633
634
    /**
635
     *
636
     */
637 1
    public function locations(
638
        null | string $addressText = null,
639
        null | string $additionalInfo = null
640
    ): array {
641
642
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
643
644 1
        if (!empty($additionalInfo)) {
645
            if (!$this->validator->validAdditionalInfo($additionalInfo, 'additionalInfo')) {
646
                throw new ValidationException('additionalInfo', $additionalInfo);
647
            }
648
            $this->filters['filter[additional_info]'] = $additionalInfo;
649
        }
650
651 1
        if (!empty($addressText)) {
652
            if (!$this->validator->validAddressText($addressText, 'addressText')) {
653
                throw new ValidationException('addressText', $addressText);
654
            }
655
            $this->filters['filter[address_text]'] = $addressText;
656
        }
657
658 1
        $locations = $this->client->locations()->list($this->filters);
659
660 1
        foreach ($locations->data as $location) {
661 1
            array_push($this->locations, new Location(
662 1
                $location->additionalInfo,
663 1
                $location->addressText,
664 1
                $location->id,
665 1
                $location->latitude,
666 1
                $location->longitude,
667 1
                $location->mapUrl,
668 1
                $location->zoom
669 1
            ));
670
        }
671
672 1
        return $this->locations;
673
674
    }
675
676
//     /**
677
//      * Set Debug.
678
//      * @deprecated
679
//      */
680
//     public function setLogging($level)
681
//     {
682
//         $this->logLevel = $level;
683
//     }
684
685
    /**
686
     * {@inheritDoc}
687
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::ticket()
688
     * class_passes
689
     */
690 1
    public function ticket(
691
        string $ticketId,
692
        bool $includeClassPasses = false,
693
        bool $includeEvents = false,
694
        bool $includeEventsAttachments = false,
695
        bool $includeEventsLocation = false,
696
        bool $includeEventsTickets = false
697
    ): Ticket {
698
699
        // ticketId
700 1
        if (!$this->validator->validId($ticketId, 'ticket')) {
701
            throw new ValidationException('ticketId', $ticketId);
702
        }
703
704
        // Validate $includeClassPasses;
705 1
        if (!$this->validator->validInclude($includeClassPasses)) {
706
            throw new ValidationException('includeClassPasses', $includeClassPasses);
707
        }
708
709 1
        if ($includeClassPasses) {
710
            array_push($this->includes, 'class_passes');
711
        }
712
713
        // Validate $includeEvents;
714 1
        if (!$this->validator->validInclude($includeEvents)) {
715
            throw new ValidationException('includeEvents', $includeEvents);
716
        }
717 1
        if ($includeEvents) {
718
            array_push($this->includes, 'events');
719
        }
720
721
        // Validate $includeAttachments;
722 1
        if (!$this->validator->validInclude($includeEventsAttachments)) {
723
            throw new ValidationException('includeEventssAttachments', $includeEventsAttachments);
724
        }
725 1
        if ($includeEventsAttachments) {
726
            array_push($this->includes, 'events.attachments');
727
        }
728
729
        // Validate $includeEventsLocation;
730 1
        if (!$this->validator->validInclude($includeEventsLocation)) {
731
            throw new ValidationException('includeEventsLocation', $includeEventsLocation);
732
        }
733 1
        if ($includeEventsLocation) {
734
            array_push($this->includes, 'events.location');
735
        }
736
737
        // Validate $includeEventsTickets;
738 1
        if (!$this->validator->validInclude($includeEventsTickets)) {
739
            throw new ValidationException('includeEventsTickets', $includeEventsTickets);
740
        }
741 1
        if ($includeEventsTickets) {
742
            array_push($this->includes, 'events.tickets');
743
        }
744
745 1
        $ticket = $this->client->tickets()->retrieve($ticketId);
746 1
        return $this->ticket = new Ticket(
747 1
            $ticket->available,
748 1
            $ticket->availableFrom,
749 1
            $ticket->availableTo,
750 1
            $ticket->builtBasketUrl,
751 1
            $ticket->builtBasketIframeUrl,
752 1
            $ticket->cost,
753 1
            $ticket->courseTicket,
754 1
            $ticket->details,
755 1
            $ticket->groupTicket,
756 1
            $ticket->groupMin,
757 1
            $ticket->groupMax,
758 1
            $ticket->id,
759 1
            $ticket->numberIssued,
760 1
            $ticket->numberTaken,
761 1
            $ticket->title
762 1
        );
763
    }
764
765
    /**
766
     * {@inheritDoc}
767
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::tickets()
768
     * @todo includes
769
     */
770 1
    public function tickets(
771
        string $eventId,
772
        bool $includeClassPasses = false,
773
        bool $includeEvents = false,
774
        bool $includeEventsAttachments = false,
775
        bool $includeEventsLocation = false,
776
        bool $includeEventsTickets = false
777
    ): array {
778
779
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
780
781 1
        if (!$this->validator->validId($eventId, 'event')) {
782
            throw new ValidationException('eventId', $eventId);
783
        }
784
785
        // Validate $includeClassPasses;
786 1
        if (!$this->validator->validInclude($includeClassPasses)) {
787
            throw new ValidationException('includeClassPasses', $includeClassPasses);
788
        }
789
790 1
        if ($includeClassPasses) {
791
            array_push($this->includes, 'class_passes');
792
        }
793
794
        // Validate $includeEvents;
795 1
        if (!$this->validator->validInclude($includeEvents)) {
796
            throw new ValidationException('includeEvents', $includeEvents);
797
        }
798
799 1
        if ($includeEvents) {
800
            array_push($this->includes, 'events');
801
        }
802
803
        // Validate $includeAttachments;
804 1
        if (!$this->validator->validInclude($includeEventsAttachments)) {
805
            throw new ValidationException('includeEventssAttachments', $includeEventsAttachments);
806
        }
807
808 1
        if ($includeEventsAttachments) {
809
            array_push($this->includes, 'events.attachments');
810
        }
811
812
        // Validate $includeEventsLocation;
813 1
        if (!$this->validator->validInclude($includeEventsLocation)) {
814
            throw new ValidationException('includeEventsLocation', $includeEventsLocation);
815
        }
816
817 1
        if ($includeEventsLocation) {
818
            array_push($this->includes, 'events.location');
819
        }
820
821
        // Validate $includeEventsTickets;
822 1
        if (!$this->validator->validInclude($includeEventsTickets)) {
823
            throw new ValidationException('includeEventsTickets', $includeEventsTickets);
824
        }
825
826 1
        if ($includeEventsTickets) {
827
            array_push($this->includes, 'events.tickets');
828
        }
829
830 1
        $tickets = $this->client->tickets()->list(array_merge(['event_id' => $eventId], ['include' => implode(',', $this->includes)]));
831
832 1
        foreach ($tickets->data as $ticket) {
833 1
            array_push($this->tickets, new Ticket(
834 1
                $ticket->available,
835 1
                $ticket->availableFrom,
836 1
                $ticket->availableTo,
837 1
                $ticket->builtBasketUrl,
838 1
                $ticket->builtBasketIframeUrl,
839 1
                $ticket->cost,
840 1
                $ticket->courseTicket,
841 1
                $ticket->details,
842 1
                $ticket->groupTicket,
843 1
                $ticket->groupMin,
844 1
                $ticket->groupMax,
845 1
                $ticket->id,
846 1
                $ticket->numberIssued,
847 1
                $ticket->numberTaken,
848 1
                $ticket->title
849 1
            ));
850
        }
851
852 1
        return $this->tickets;
853
854
    }
855
}
856