Passed
Pull Request — develop (#172)
by
unknown
01:50
created

Bookwhen::event()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 116
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 61
CRAP Score 9.8379

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 9
eloc 72
c 6
b 0
f 0
nc 10
nop 6
dl 0
loc 116
ccs 61
cts 78
cp 0.7821
crap 9.8379
rs 7.0553

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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