Passed
Pull Request — develop (#150)
by
unknown
01:47
created

Bookwhen::event()   C

Complexity

Conditions 14
Paths 160

Size

Total Lines 124
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 18.4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 74
c 4
b 0
f 0
nc 160
nop 6
dl 0
loc 124
ccs 56
cts 78
cp 0.7179
crap 18.4
rs 5.1806

How to fix   Long Method    Complexity   

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