Completed
Push — develop ( ba711d...71f723 )
by Daniel
16s queued 13s
created

Bookwhen::events()   F

Complexity

Conditions 28
Paths 540

Size

Total Lines 184
Code Lines 103

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 65
CRAP Score 67.3588

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 28
eloc 103
c 2
b 0
f 0
nc 540
nop 13
dl 0
loc 184
ccs 65
cts 103
cp 0.6311
crap 67.3588
rs 0.5111

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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