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