Passed
Pull Request — develop (#169)
by
unknown
01:36
created

Bookwhen::event()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 147
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 58
CRAP Score 10.6845

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 9
eloc 74
c 5
b 0
f 0
nc 7
nop 6
dl 0
loc 147
ccs 58
cts 80
cp 0.725
crap 10.6845
rs 7.0117

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen;
6
7
use InShore\Bookwhen\BookwhenApi;
8
use InShore\Bookwhen\Client;
9
use InShore\Bookwhen\Domain\Attachment;
10
use InShore\Bookwhen\Domain\ClassPass;
11
use InShore\Bookwhen\Domain\Event;
12
use InShore\Bookwhen\Domain\Location;
13
use InShore\Bookwhen\Domain\Ticket;
14
use InShore\Bookwhen\Exceptions\ConfigurationException;
15
use InShore\Bookwhen\Exceptions\ValidationException;
16
use InShore\Bookwhen\Interfaces\BookwhenInterface;
17
use InShore\Bookwhen\Validator;
18
use Monolog\Level;
19
use Monolog\Logger;
20
use Monolog\Handler\StreamHandler;
21
22
final class Bookwhen implements BookwhenInterface
23
{
24
    /**
25
     *
26
     */
27
    public Attachment $attachment;
28
29
    /**
30
     *
31
     */
32
    public array $attachments = [];
33
34
    /**
35
     *
36
     */
37
    public Client $client;
38
39
    /**
40
     *
41
     */
42
    public ClassPass $classPass;
43
44
    /**
45
     *
46
     */
47
    public array $classPasses = [];
48
49
    /**
50
     *
51
     */
52
    public Event $event;
53
54
    /**
55
     *
56
     */
57
    public array $events = [];
58
59
    /**
60
     *
61
     */
62
    private array $filters = [];
63
64
    /**
65
     *
66
     */
67
    public Location $location;
68
69
    /**
70
     *
71
     */
72
    public array $includes = [];
73
74
    /**
75
     *
76
     */
77
    public Ticket $ticket;
78
79
    /**
80
     *
81
     */
82
    public array $tickets = [];
83
84
    /**
85
     *
86
     */
87
    public $locations = [];
88
89
90
    /** @var string The path to the log file */
91
    //private $logFile;
92
93
    /** @var object loging object. */
94
    //private $logger;
95
96
    /** @var string the logging level. */
97
    //private string $logLevel;
98
99
    /**
100
     * Creates a new Bookwhen Client with the given API token.
101
     * @throws ConfigurationException
102
     * @todo logging
103
     */
104 20
    public function __construct(
105
        string $apiKey = null,
106
        Client $client = null,
107
        private $validator = new Validator()
108
    ) {
109
        //         $this->logFile = $logFile;
110
        //         $this->logLevel = $logLevel;
111
        //         $this->logger = new Logger('inShore Bookwhen API');
112
        //         $this->logger->pushHandler(new StreamHandler($this->logFile, $this->logLevel));
113
        try {
114 20
            if(!is_null($client)) {
115 11
                $this->client = $client;
116
            } else {
117 9
                $this->client = !is_null($apiKey)
118 9
                ? BookwhenApi::client($apiKey)
119 1
                : (array_key_exists('INSHORE_BOOKWHEN_API_KEY', $_ENV)
120
                    ? BookwhenApi::client($_ENV['INSHORE_BOOKWHEN_API_KEY'])
121 20
                    : throw new ConfigurationException());
122
            }
123 1
        } catch (\TypeError $e) {
124
            throw new ConfigurationException(); // @todo message
125
        }
126
    }
127
128
    /**
129
     *
130
     * {@inheritDoc}
131
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::attachment()
132
     * @todo all attachment properties
133
     */
134 3
    public function attachment(string $attachmentId): Attachment
135
    {
136 3
        if (!$this->validator->validId($attachmentId, 'attachment')) {
137 2
            throw new ValidationException('attachmentId', $attachmentId);
138
        }
139
140 1
        $attachment = $this->client->attachments()->retrieve($attachmentId);
141
142 1
        return $this->attachment = new Attachment(
143 1
            $attachment->contentType,
144 1
            $attachment->fileUrl,
145 1
            $attachment->fileSizeBytes,
146 1
            $attachment->fileSizeText,
147 1
            $attachment->fileName,
148 1
            $attachment->fileType,
149 1
            $attachment->id,
150 1
            $attachment->title,
151 1
        );
152
    }
153
154
    /**
155
     *
156
     * {@inheritDoc}
157
     * 
158
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::attachment()
159
     */
160 1
    public function attachments(
161
        string $title = null,
162
        string $fileName = null,
163
        string $fileType = null
164
    ): array {
165
166
        if (!is_null($title)) {
167 1
            if ($this->validator->validTitle($title)) {
168
                $this->filters['filter[title]'] = $title;
169
            } else {
170
                throw new ValidationException('title', $title);
171
            }
172
        }
173
174
        if (!is_null($fileName) && !$this->validator->validFileName($fileName)) {
175 1
            throw new ValidationException('file name', $fileName);
176
        } else {
177
            $this->filters['filter[file_name]'] = $fileName;
178 1
        }
179
180
        if (!is_null($fileType) && !$this->validator->validFileType($fileType)) {
181 1
            throw new ValidationException('file type', $fileType);
182
        } else {
183
            $this->filters['filter[file_type]'] = $fileType;
184 1
        }
185
186
        $attachments = $this->client->attachments()->list($this->filters);
187 1
188
        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 1
        }
200
201
        return $this->attachments;
202 1
    }
203
204
    /**
205
     *
206
     * {@inheritDoc}
207
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass()
208
     * @todo
209
     */
210
    public function classPass(string $classPassId): ClassPass
211 1
    {
212
213
        if (!$this->validator->validId($classPassId, 'classPass')) {
214
            throw new ValidationException('classPassId', $classPassId);
215 1
        }
216
217
        $classPass = $this->client->classPasses()->retrieve($classPassId);
218
219 1
        return new ClassPass(
220
            $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 1
    }
229 1
230
    /**
231
     *
232
     * {@inheritDoc}
233
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses()
234
     */
235
    public function classPasses(
236
        $cost = null,
237 1
        $detail = null,
238
        $title = null,
239
        $usageAllowance = null,
240
        $usageType = null,
241
        $useRestrictedForDays = null
242
    ): array {
243
244
        if (!is_null($detail) && !$this->validator->validDetails($detail)) {
245
            throw new ValidationException('detail', $detail);
246
        } else {
247
            $this->filters['filter[detail]'] = $detail;
248 1
        }
249
250
        if (!is_null($title) && !$this->validator->validTitle($title)) {
251 1
            throw new ValidationException('title', $title);
252
        } else {
253
            $this->filters['filter[title]'] = $title;
254 1
        }
255
256
        if (!is_null($usageAllowance) && !$this->validator->validUsageAllowance($usageAllowance)) {
257 1
            throw new ValidationException('usageAllowance', $usageAllowance);
258
        } else {
259
            $this->filters['filter[usage_allowance]'] = $usageAllowance;
260 1
        }
261
262
        if (!is_null($usageType) && !$this->validator->validUsageType($usageType)) {
263 1
            throw new ValidationException('usageType', $usageType);
264
        } else {
265
            $this->filters['filter[usage_type]'] = $usageType;
266 1
        }
267
268
        if (!is_null($useRestrictedForDays) && !$this->validator->validUseRestrictedForDays($useRestrictedForDays)) {
269 1
            throw new ValidationException('useRestrictedForDays', $useRestrictedForDays);
270
        } else {
271
            $this->filters['filter[use_restricted_for_days]'] = $useRestrictedForDays;
272 1
        }
273
274
        $classPasses = $this->client->classPasses()->list($this->filters);
275 1
276
        foreach ($classPasses->data as $classPass) {
277
            array_push($this->classPasses, new ClassPass(
278 1
                $classPass->details,
279
                $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 1
        }
287 1
        return $this->classPasses;
288 1
    }
289 1
290
    /**
291 1
     *
292
     * {@inheritDoc}
293
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::event()
294
     */
295
    public function event(
296
        string $eventId,
297
        bool $includeAttachments = false,
298
        bool $includeLocation = false,
299 2
        bool $includeTickets = false,
300
        bool $includeTicketsClassPasses = false,
301
        bool $includeTicketsEvents = false
302
    ): Event {
303
304
        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 2
        // Validate $includeTickets;
319
        if (!$this->validator->validInclude($includeTickets)) {
320
            throw new ValidationException('includeTickets', $includeTickets);
321
        }
322
323 2
        // Validate $includeTicketsEvents;
324
        if (!$this->validator->validInclude($includeTicketsEvents)) {
325
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
326 2
        }
327
328
        // Validate $includeTicketsClassPasses;
329
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
330
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
331 2
        }
332
        
333
        
334
//         if ($includeAttachments) {
335 2
//             array_push($this->includes, 'attachments');
336
//         }
337
        
338
//         if ($includeLocation) {
339
//             array_push($this->includes, 'location');
340 2
//         }
341
        
342
//         if ($includeTickets) {
343
//             array_push($this->includes, 'tickets');
344 2
//         }
345
        
346
//         if ($includeTicketsEvents) {
347
//             array_push($this->includes, 'tickets.events');
348
//         }
349 2
350
//         if ($includeTicketsClassPasses) {
351
//             array_push($this->includes, 'tickets.class_passes');
352
//         }
353 2
354
        $includesMapping = [
355
            'attachments' => $includeAttachments,
356
            'location' => $includeLocation,
357 2
            'tickets' => $includeTickets,
358
            'tickets.events' => $includeTicketsEvents,
359
            'tickets.class_passes' => $includeTicketsClassPasses,
360 2
        ];
361
        
362 2
//         foreach ($includesMapping as $key => $value) {
363
//             if ($value) {
364
//                 $this->includes[] = $key;
365
//             }
366
//         }
367
368
        $this->includes = array_keys(array_filter($includesMapping, function ($value) {
369
            return $value;
370
        }));
371
        
372
        var_export($this->includes);
373
        
374
        die();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return InShore\Bookwhen\Domain\Event. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
375
        
376
        $event = $this->client->events()->retrieve($eventId, ['include' => implode(',', $this->includes)]);
0 ignored issues
show
Unused Code introduced by
$event = $this->client->...',', $this->includes))) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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