Passed
Push — develop ( bbfb8c...f742ee )
by Daniel
26:42 queued 11:46
created

Bookwhen::classPasses()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 31
c 0
b 0
f 0
nc 7
nop 6
dl 0
loc 55
rs 6.9666

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\ValidationException;
15
use InShore\Bookwhen\Interfaces\BookwhenInterface;
16
use InShore\Bookwhen\Validator;
17
use Monolog\Level;
18
use Monolog\Logger;
19
use Monolog\Handler\StreamHandler;
20
21
final class Bookwhen implements BookwhenInterface
22
{
23
    private null | Client $client;
24
25
    /**
26
     *
27
     * @var unknown
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\unknown was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
     */
29
    public Attachment $attachment;
30
31
    /**
32
     *
33
     * @var unknown
34
     */
35
    public array $attachments = [];
36
37
    /**
38
     *
39
     * @var unknown
40
     */
41
    public ClassPass $classPass;
42
43
    /**
44
     *
45
     * @var unknown
46
     */
47
    public array $classPasses = [];
48
49
    /**
50
     *
51
     * @var unknown
52
     */
53
    public Event $event;
54
55
    /**
56
     *
57
     * @var unknown
58
     */
59
    public array $events = [];
60
61
    /**
62
     *
63
     * @var unknown
64
     */
65
    private array $filters = [];
66
67
    /**
68
     *
69
     */
70
    public readonly Location $location;
71
72
    /**
73
     *
74
     * @var unknown
75
     */
76
    private array $includes = [];
77
    /**
78
79
     */
80
    public Ticket $ticket;
81
82
    /**
83
84
     */
85
    public array $tickets = [];
86
87
    /**
88
        *
89
     */
90
    public $locations = [];
91
92
93
    /** @var string The path to the log file */
94
    private $logFile;
0 ignored issues
show
introduced by
The private property $logFile is not used, and could be removed.
Loading history...
95
96
    /** @var object loging object. */
97
    private $logger;
0 ignored issues
show
introduced by
The private property $logger is not used, and could be removed.
Loading history...
98
99
    /** @var string the logging level. */
100
    private string $logLevel;
0 ignored issues
show
introduced by
The private property $logLevel is not used, and could be removed.
Loading history...
101
102
103
104
    /**
105
     * Creates a new Bookwhen Client with the given API token.
106
     * @todo logging
107
     */
108
    public function __construct(
109
        private $validator = new Validator()
110
    ) {
111
        //         $this->logFile = $logFile;
112
        //         $this->logLevel = $logLevel;
113
        //         $this->logger = new Logger('inShore Bookwhen API');
114
        //         $this->logger->pushHandler(new StreamHandler($this->logFile, $this->logLevel));
115
        $this->client = BookwhenApi::client($_ENV['INSHORE_BOOKWHEN_API_KEY']);
116
    }
117
118
    /**
119
     *
120
     * {@inheritDoc}
121
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::attachment()
122
     * @todo all attachment properties
123
     */
124
    public function attachment(string $attachmentId): Attachment
125
    {
126
        if (!$this->validator->validId($attachmentId, 'attachment')) {
127
            throw new ValidationException('attachmentId', $attachmentId);
128
        }
129
130
        $attachment = $this->client->attachments()->retrieve($attachmentId);
0 ignored issues
show
Bug introduced by
The method attachments() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        $attachment = $this->client->/** @scrutinizer ignore-call */ attachments()->retrieve($attachmentId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
132
        return $this->attachment = new Attachment(
0 ignored issues
show
Documentation Bug introduced by
It seems like new InShore\Bookwhen\Dom...id, $attachment->title) of type InShore\Bookwhen\Domain\Attachment is incompatible with the declared type InShore\Bookwhen\unknown of property $attachment.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
133
            $attachment->contentType,
134
            $attachment->fileUrl,
135
            $attachment->fileSizeBytes,
136
            $attachment->fileSizeText,
137
            $attachment->fileName,
138
            $attachment->fileType,
139
            $attachment->id,
140
            $attachment->title,
141
        );
142
    }
143
144
    /**
145
     *
146
     * {@inheritDoc}
147
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::attachment()
148
     * @todo
149
     */
150
    public function attachments(
151
        string $title = null,
152
        string $fileName = null,
153
        string $fileType = null
154
    ): array {
155
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
156
157
        if (!is_null($title) && !$this->validator->validTitle($title)) {
158
            throw new ValidationException('title', $title);
159
        } else {
160
            $this->filters['filter[title]'] = $title;
161
        }
162
163
        if (!is_null($fileName) && !$this->validator->validFileName($fileName)) {
164
            throw new ValidationException('file name', $fileName);
165
        } else {
166
            $this->filters['filter[file_name]'] = $fileName;
167
        }
168
169
        if (!is_null($fileType) && !$this->validator->validFileType($fileType)) {
170
            throw new ValidationException('file type', $fileType);
171
        } else {
172
            $this->filters['filter[file_type]'] = $fileType;
173
        }
174
175
        $attachments = $this->client->attachments()->list($this->filters);
176
177
        foreach ($attachments->data as $attachment) {
178
            array_push($this->attachments, new Attachment(
179
                $attachment->contentType,
180
                $attachment->fileUrl,
181
                $attachment->fileSizeBytes,
182
                $attachment->fileSizeText,
183
                $attachment->fileName,
184
                $attachment->fileType,
185
                $attachment->id,
186
                $attachment->title
187
            ));
188
        }
189
190
        return $this->attachments;
191
    }
192
193
    /**
194
     *
195
     * {@inheritDoc}
196
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass()
197
     * @todo
198
     */
199
    public function classPass(string $classPassId): ClassPass
200
    {
201
        //         $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
202
203
        if (!$this->validator->validId($classPassId, 'classPass')) {
204
            throw new ValidationException('classPassId', $classPassId);
205
        }
206
207
        $classPass = $this->client->classPasses()->retrieve($classPassId);
208
209
        return new ClassPass(
210
            $classPass->details,
211
            $classPass->id,
212
            $classPass->numberAvailable,
213
            $classPass->title,
214
            $classPass->usageAllowance,
215
            $classPass->usageType,
216
            $classPass->useRestrictedForDays
217
        );
218
    }
219
220
    /**
221
     *
222
     * {@inheritDoc}
223
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses()
224
     * @todo break params on to multiplper lines..
225
     */
226
    public function classPasses(
227
        $cost = null,
228
        $detail = null,
229
        $title = null,
230
        $usageAllowance = null,
231
        $usageType = null,
232
        $useRestrictedForDays = null
233
    ): array {
234
235
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
236
237
        if (!is_null($detail) && !$this->validator->validDetails($detail)) {
238
            throw new ValidationException('detail', $detail);
239
        } else {
240
            $this->filters['filter[detail]'] = $detail;
241
        }
242
243
        if (!is_null($title) && !$this->validator->validTitle($title)) {
244
            throw new ValidationException('title', $title);
245
        } else {
246
            $this->filters['filter[title]'] = $title;
247
        }
248
249
        if (!is_null($usageAllowance) && !$this->validator->validUsageAllowance($usageAllowance)) {
250
            throw new ValidationException('usageAllowance', $usageAllowance);
251
        } else {
252
            $this->filters['filter[usage_allowance]'] = $usageAllowance;
253
        }
254
255
        if (!is_null($usageType) && !$this->validator->validUsageType($usageType)) {
256
            throw new ValidationException('usageType', $usageType);
257
        } else {
258
            $this->filters['filter[usage_type]'] = $usageType;
259
        }
260
261
        if (!is_null($useRestrictedForDays) && !$this->validator->validUseRestrictedForDays($useRestrictedForDays)) {
262
            throw new ValidationException('useRestrictedForDays', $useRestrictedForDays);
263
        } else {
264
            $this->filters['filter[use_restricted_for_days]'] = $useRestrictedForDays;
265
        }
266
267
        $classPasses = $this->client->classPasses()->list($this->filters);
268
269
        foreach ($classPasses->data as $classPass) {
270
            array_push($this->classPasses, new ClassPass(
271
                $classPass->details,
272
                $classPass->id,
273
                $classPass->numberAvailable,
274
                $classPass->title,
275
                $classPass->usageAllowance,
276
                $classPass->usageType,
277
                $classPass->useRestrictedForDays,
278
            ));
279
        }
280
        return $this->classPasses;
281
    }
282
283
    /**
284
     *
285
     * {@inheritDoc}
286
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::event()
287
     * @todo filters.
288
     */
289
    public function event(
290
        string $eventId,
291
        bool $includeAttachments = false,
292
        bool $includeLocation = false,
293
        bool $includeTickets = false,
294
        bool $includeTicketsClassPasses = false,
295
        bool $includeTicketsEvents = false
296
    ): Event {
297
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
298
299
        if (!$this->validator->validId($eventId, 'event')) {
300
            throw new ValidationException('eventId', $eventId);
301
        }
302
303
        // Validate $includeAttachments;
304
        if (!$this->validator->validInclude($includeAttachments)) {
305
            throw new ValidationException('includeAttachments', $includeAttachments);
306
        } else {
307
            if($includeAttachments) {
308
                array_push($this->includes, 'attachments');
309
            }
310
        }
311
312
        // Validate $includeTickets;
313
        if (!$this->validator->validInclude($includeLocation)) {
314
            throw new ValidationException('includeLocation', $includeLocation);
315
        } else {
316
            if($includeLocation) {
317
                array_push($this->includes, 'location');
318
            }
319
        }
320
321
        // Validate $includeTickets;
322
        if (!$this->validator->validInclude($includeTickets)) {
323
            throw new ValidationException('includeTickets', $includeTickets);
324
        } else {
325
            if($includeTickets) {
326
                array_push($this->includes, 'tickets');
327
            }
328
        }
329
330
        // Validate $includeTicketsEvents;
331
        if (!$this->validator->validInclude($includeTicketsEvents)) {
332
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
333
        } else {
334
            if($includeTicketsEvents) {
335
                array_push($this->includes, 'tickets.events');
336
            }
337
        }
338
339
        // Validate $includeTicketsClassPasses;
340
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
341
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
342
        } else {
343
            if($includeTicketsClassPasses) {
344
                array_push($this->includes, 'tickets.class_passes');
345
            }
346
        }
347
348
        $event = $this->client->events()->retrieve($eventId, ['include' => implode(',', $this->includes)]);
349
350
        // attachments
351
        $eventAttachments = [];
352
353
        foreach ($event->attachments as $eventAttachment) {
354
            $attachment = $this->client->attachments()->retrieve($eventAttachment['id']);
355
            array_push($eventAttachments, new Attachment(
356
                $attachment->contentType,
357
                $attachment->fileUrl,
358
                $attachment->fileSizeBytes,
359
                $attachment->fileSizeText,
360
                $attachment->fileName,
361
                $attachment->fileType,
362
                $attachment->id,
363
                $attachment->title
364
            ));
365
        }
366
367
        // eventTickets
368
        $eventTickets = [];
369
        foreach($event->tickets as $ticket) {
370
            array_push($eventTickets, new Ticket(
371
                $ticket->available,
372
                $ticket->availableFrom,
373
                $ticket->availableTo,
374
                $ticket->builtBasketUrl,
375
                $ticket->builtBasketIframeUrl,
376
                $ticket->courseTicket,
377
                $ticket->details,
378
                $ticket->groupTicket,
379
                $ticket->groupMin,
380
                $ticket->groupMax,
381
                $ticket->id,
382
                $ticket->numberIssued,
383
                $ticket->numberTaken,
384
                $ticket->title
385
            ));
386
        }
387
388
        // ticketsClassPasses
389
        // @todo
390
391
        // ticketsEvents
392
        // @todo
393
394
        return $this->event = new Event(
0 ignored issues
show
Documentation Bug introduced by
It seems like new InShore\Bookwhen\Dom...e, $event->waitingList) of type InShore\Bookwhen\Domain\Event is incompatible with the declared type InShore\Bookwhen\unknown of property $event.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
395
            $event->allDay,
396
            $eventAttachments,
397
            $event->attendeeCount,
398
            $event->attendeeLimit,
399
            $event->details,
400
            $event->endAt,
401
            $event->id,
402
            new Location(
403
                $event->location->addressText,
404
                $event->location->additionalInfo,
405
                $event->location->id,
406
                $event->location->latitude,
407
                $event->location->longitude,
408
                $event->location->mapUrl,
409
                $event->location->zoom
410
            ),
411
            $event->maxTicketsPerBooking,
412
            $event->startAt,
413
            $eventTickets,
414
            $event->title,
415
            $event->waitingList
416
        );
417
    }
418
419
    /**
420
     *
421
     * {@inheritDoc}
422
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents()
423
     */
424
    public function events(
425
        $calendar = false,
426
        $entry = false,
427
        $location = [],
428
        $tags = [],
429
        $title = [],
430
        $detail = [],
431
        $from = null,
432
        $to = null,
433
        bool $includeAttachments = false,
434
        bool $includeLocation = false,
435
        bool $includeTickets = false,
436
        bool $includeTicketsClassPasses = false,
437
        bool $includeTicketsEvents = false
438
    ): array {
439
440
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
441
442
        // Validate $calendar
443
        // @todo details required
444
445
        // Validate $detail
446
        if (!empty($detail)) {
447
            if (!is_array($detail)) {
448
                throw new ValidationException('detail', implode(' ', $detail));
449
            } else {
450
                $detail = array_unique($detail);
451
                foreach($detail as $item) {
452
                    if (!$this->validator->validLocation($item)) {
453
                        throw new ValidationException('detail', $item);
454
                    }
455
                }
456
                $this->filters['filter[detail]'] = implode(',', $detail);
457
            }
458
        }
459
460
        // Validate $entry
461
        // @todo details required
462
463
        // Validate $from;
464
        if (!empty($from)) {
465
            if (!$this->validator->validFrom($from, $to)) {
466
                throw new ValidationException('from', $from . '-' . $to);
467
            } else {
468
                $this->filters['filter[from]'] = $from;
469
            }
470
        }
471
472
        // Validate $location
473
        if (!empty($location)) {
474
            if (!is_array($location)) {
475
                throw new ValidationException('location', implode(' ', $title));
476
            } else {
477
                $location = array_unique($location);
478
                foreach($location as $item) {
479
                    if (!$this->validator->validLocation($item)) {
480
                        throw new ValidationException('location', $item);
481
                    }
482
                }
483
                $this->filters['filter[location]'] = implode(',', $location);
484
            }
485
        }
486
487
        // Validate $tags.
488
        if (!empty($tags)) {
489
            if (!is_array($tags)) {
490
                throw new ValidationException('tags', implode(' ', $tags));
491
            } else {
492
                $tags = array_unique($tags);
493
                foreach ($tags as $tag) {
494
                    if (!empty($tag) && !$this->validator->validTag($tag)) {
495
                        throw new ValidationException('tag', $tag);
496
                    }
497
                }
498
            }
499
            $this->filters['filter[tag]'] = implode(',', $tags);
500
        }
501
502
        // Validate $title;
503
        if (!empty($title)) {
504
            if (!is_array($title)) {
505
                throw new ValidationException('title', implode(' ', $title));
506
            } else {
507
                $title = array_unique($title);
508
                foreach($title as $item) {
509
                    if (!$this->validator->validTitle($item)) {
510
                        throw new ValidationException('title', $item);
511
                    }
512
                }
513
                $this->filters['filter[title]'] = implode(',', $title);
514
            }
515
        }
516
517
        // Validate $to;
518
        if (!empty($to)) {
519
            if (!$this->validator->validTo($to, $from)) {
520
                throw new ValidationException('to', $to . '-' . $from);
521
            } else {
522
                $this->filters['filter[to]'] = $to;
523
            }
524
        }
525
526
        // Validate $includeAttachments;
527
        if (!$this->validator->validInclude($includeAttachments)) {
528
            throw new ValidationException('includeAttachments', $includeAttachments);
529
        } else {
530
            if($includeAttachments) {
531
                array_push($this->includes, 'attachments');
532
            }
533
        }
534
535
        // Validate $includeTickets;
536
        if (!$this->validator->validInclude($includeLocation)) {
537
            throw new ValidationException('includeLocation', $includeLocation);
538
        } else {
539
            if($includeLocation) {
540
                array_push($this->includes, 'location');
541
            }
542
        }
543
544
        // Validate $includeTickets;
545
        if (!$this->validator->validInclude($includeTickets)) {
546
            throw new ValidationException('includeTickets', $includeTickets);
547
        } else {
548
            if($includeTickets) {
549
                array_push($this->includes, 'tickets');
550
            }
551
        }
552
553
        // Validate $includeTicketsEvents;
554
        if (!$this->validator->validInclude($includeTicketsEvents)) {
555
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
556
        } else {
557
            if($includeTicketsEvents) {
558
                array_push($this->includes, 'tickets.events');
559
            }
560
        }
561
562
        // Validate $includeTicketsClassPasses;
563
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
564
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
565
        } else {
566
            if($includeTicketsClassPasses) {
567
                array_push($this->includes, 'tickets.class_passes');
568
            }
569
        }
570
571
        $events = $this->client->events()->list(array_merge($this->filters, ['include' => implode(',', $this->includes)]));
572
573
        foreach ($events->data as $event) {
574
575
            $eventTickets = [];
576
            foreach($event->tickets as $ticket) {
577
                array_push($eventTickets, new Ticket(
578
                    $ticket->available,
579
                    $ticket->availableFrom,
580
                    $ticket->availableTo,
581
                    $ticket->builtBasketUrl,
582
                    $ticket->builtBasketIframeUrl,
583
                    $ticket->courseTicket,
584
                    $ticket->details,
585
                    $ticket->groupTicket,
586
                    $ticket->groupMin,
587
                    $ticket->groupMax,
588
                    $ticket->id,
589
                    $ticket->numberIssued,
590
                    $ticket->numberTaken,
591
                    $ticket->title
592
                ));
593
            }
594
595
            array_push($this->events, new Event(
596
                $event->allDay,
597
                $event->attachments,
598
                $event->attendeeCount,
599
                $event->attendeeLimit,
600
                $event->details,
601
                $event->endAt,
602
                $event->id,
603
                new Location(
604
                    $event->location->addressText,
605
                    $event->location->additionalInfo,
606
                    $event->location->id,
607
                    $event->location->latitude,
608
                    $event->location->longitude,
609
                    $event->location->mapUrl,
610
                    $event->location->zoom
611
                ),
612
                $event->maxTicketsPerBooking,
613
                $event->startAt,
614
                $eventTickets,
615
                $event->title,
616
                $event->waitingList
617
            ));
618
        }
619
620
        return $this->events;
621
    }
622
623
    /*
624
     *
625
     * {@inheritDoc}
626
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation()
627
     */
628
    public function location(string $locationId): Location
629
    {
630
631
        if (!$this->validator->validId($locationId, 'location')) {
632
            throw new ValidationException('locationId', $locationId);
633
        }
634
635
        $location = $this->client->locations()->retrieve($locationId);
636
637
        return $this->location = new Location(
0 ignored issues
show
Bug introduced by
The property location is declared read-only in InShore\Bookwhen\Bookwhen.
Loading history...
638
            $location->additionalInfo,
639
            $location->addressText,
640
            $location->id,
641
            $location->latitude,
642
            $location->longitude,
643
            $location->mapUrl,
644
            $location->zoom
645
        );
646
    }
647
648
    /**
649
     *
650
     */
651
    public function locations(
652
        null | string $addressText = null,
653
        null | string $additionalInfo = null
654
    ): array {
655
656
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
657
658
        if (!empty($additionalInfo)) {
659
            if (!$this->validator->validAdditionalInfo($additionalInfo, 'additionalInfo')) {
660
                throw new ValidationException('additionalInfo', $additionalInfo);
661
            }
662
            $this->filters['filter[additional_info]'] = $additionalInfo;
663
        }
664
665
        if (!empty($addressText)) {
666
            if (!$this->validator->validAddressText($addressText, 'addressText')) {
667
                throw new ValidationException('addressText', $addressText);
668
            }
669
            $this->filters['filter[address_text]'] = $addressText;
670
        }
671
672
        $locations = $this->client->locations()->list($this->filters);
673
674
        foreach ($locations->data as $location) {
675
            array_push($this->locations, new Location(
676
                $location->additionalInfo,
677
                $location->addressText,
678
                $location->id,
679
                $location->latitude,
680
                $location->longitude,
681
                $location->mapUrl,
682
                $location->zoom
683
            ));
684
        }
685
686
        return $this->locations;
687
688
    }
689
690
    /**
691
     * Set Debug.
692
     * @deprecated
693
     */
694
    public function setLogging($level)
695
    {
696
        $this->logging = $level;
0 ignored issues
show
Bug Best Practice introduced by
The property logging does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
697
    }
698
699
     /**
700
     * Sets the token for all future new instances
701
     * @deprecated
702
     * @param $token string The API access token, as obtained on diffbot.com/dev.
703
     */
704
    public static function setToken($token)
705
    {
706
        $validator = new Validator();
707
        if (!$validator->validToken($token)) {
708
            throw new \InvalidArgumentException('Invalid Token.');
709
        }
710
        self::$token = $token;
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
711
    }
712
713
    /**
714
     * {@inheritDoc}
715
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::ticket()
716
     * class_passes
717
     */
718
    public function ticket(
719
        string $ticketId,
720
        bool $includeClassPasses = false,
721
        bool $includeEvents = false,
722
        bool $includeEventsAttachments = false,
723
        bool $includeEventsLocation = false,
724
        bool $includeEventsTickets = false
725
    ): Ticket {
726
        
727
        // ticketId
728
        if (!$this->validator->validId($ticketId, 'ticket')) {
729
            throw new ValidationException('ticketId', $ticketId);
730
        }
731
732
        // Validate $includeClassPasses;
733
        if (!$this->validator->validInclude($includeClassPasses)) {
734
            throw new ValidationException('includeClassPasses', $includeClassPasses);
735
        }
736
            
737
        if($includeClassPasses) {
738
            array_push($this->includes, 'class_passes');
739
        }
740
741
        // Validate $includeEvents;
742
        if (!$this->validator->validInclude($includeEvents)) {
743
            throw new ValidationException('includeEvents', $includeEvents);
744
        }
745
        if($includeEvents) {
746
            array_push($this->includes, 'events');
747
        }
748
749
        // Validate $includeAttachments;
750
        if (!$this->validator->validInclude($includeEventsAttachments)) {
751
            throw new ValidationException('includeEventssAttachments', $includeEventsAttachments);
752
        }
753
        if($includeEventsAttachments) {
754
            array_push($this->includes, 'events.attachments');
755
        }
756
757
        // Validate $includeEventsLocation;
758
        if (!$this->validator->validInclude($includeEventsLocation)) {
759
            throw new ValidationException('includeEventsLocation', $includeEventsLocation);
760
        }
761
        if($includeEventsLocation) {
762
            array_push($this->includes, 'events.location');
763
        }
764
765
        // Validate $includeEventsTickets;
766
        if (!$this->validator->validInclude($includeEventsTickets)) {
767
            throw new ValidationException('includeEventsTickets', $includeEventsTickets);
768
        }
769
        if($includeEventsTickets) {
770
            array_push($this->includes, 'events.tickets');
771
        }
772
773
        $ticket = $this->client->tickets()->retrieve($ticketId);
774
775
        return $this->ticket = new Ticket(
776
            $ticket->available,
777
            $ticket->availableFrom,
778
            $ticket->availableTo,
779
            $ticket->builtBasketUrl,
780
            $ticket->builtBasketIframeUrl,
781
            $ticket->courseTicket,
782
            $ticket->details,
783
            $ticket->groupTicket,
784
            $ticket->groupMin,
785
            $ticket->groupMax,
786
            $ticket->id,
787
            $ticket->numberIssued,
788
            $ticket->numberTaken,
789
            $ticket->title
790
        );
791
    }
792
793
    /**
794
     * {@inheritDoc}
795
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::tickets()
796
     * @todo includes
797
     */
798
    public function tickets(
799
        string $eventId,
800
        bool $includeClassPasses = false,
801
        bool $includeEvents = false,
802
        bool $includeEventsAttachments = false,
803
        bool $includeEventsLocation = false,
804
        bool $includeEventsTickets = false
805
    ): array {
806
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
807
808
        if (!$this->validator->validId($eventId, 'event')) {
809
            throw new ValidationException('eventId', $eventId);
810
        }
811
812
        // Validate $includeClassPasses;
813
        if (!$this->validator->validInclude($includeClassPasses)) {
814
            throw new ValidationException('includeClassPasses', $includeClassPasses);
815
        }
816
        
817
        if($includeClassPasses) {
818
            array_push($this->includes, 'class_passes');
819
        }
820
        
821
        // Validate $includeEvents;
822
        if (!$this->validator->validInclude($includeEvents)) {
823
            throw new ValidationException('includeEvents', $includeEvents);
824
        }
825
        
826
        if($includeEvents) {
827
            array_push($this->includes, 'events');
828
        }
829
        
830
        // Validate $includeAttachments;
831
        if (!$this->validator->validInclude($includeEventsAttachments)) {
832
            throw new ValidationException('includeEventssAttachments', $includeEventsAttachments);
833
        }
834
        
835
        if($includeEventsAttachments) {
836
            array_push($this->includes, 'events.attachments');
837
        }
838
        
839
        // Validate $includeEventsLocation;
840
        if (!$this->validator->validInclude($includeEventsLocation)) {
841
            throw new ValidationException('includeEventsLocation', $includeEventsLocation);
842
        }
843
        
844
        if($includeEventsLocation) {
845
            array_push($this->includes, 'events.location');
846
        }
847
        
848
        // Validate $includeEventsTickets;
849
        if (!$this->validator->validInclude($includeEventsTickets)) {
850
            throw new ValidationException('includeEventsTickets', $includeEventsTickets);
851
        }
852
        
853
        if($includeEventsTickets) {
854
            array_push($this->includes, 'events.tickets');
855
        }
856
857
        $tickets = $this->client->tickets()->list(array_merge(['event_id' => $eventId], ['include' => implode(',', $this->includes)]));
858
859
        foreach ($tickets->data as $ticket) {
860
            array_push($this->tickets, new Ticket(
861
                $ticket->available,
862
                $ticket->availableFrom,
863
                $ticket->availableTo,
864
                $ticket->builtBasketUrl,
865
                $ticket->builtBasketIframeUrl,
866
                $ticket->courseTicket,
867
                $ticket->details,
868
                $ticket->groupTicket,
869
                $ticket->groupMin,
870
                $ticket->groupMax,
871
                $ticket->id,
872
                $ticket->numberIssued,
873
                $ticket->numberTaken,
874
                $ticket->title
875
            ));
876
        }
877
878
        return $this->tickets;
879
880
    }
881
}
882