Passed
Pull Request — develop (#142)
by
unknown
01:56
created

Bookwhen   F

Complexity

Total Complexity 108

Size/Duplication

Total Lines 836
Duplicated Lines 0 %

Test Coverage

Coverage 2.08%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 390
c 10
b 0
f 0
dl 0
loc 836
ccs 8
cts 385
cp 0.0208
rs 2
wmc 108

12 Methods

Rating   Name   Duplication   Size   Complexity  
C tickets() 0 83 13
A location() 0 17 2
A locations() 0 36 6
C event() 0 124 14
B attachments() 0 43 8
A setLogging() 0 3 1
A attachment() 0 17 2
A __construct() 0 14 4
A classPass() 0 18 2
C ticket() 0 72 12
F events() 0 188 32
C classPasses() 0 55 12

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