Passed
Push — develop ( 22aa13...73f587 )
by Daniel
33:30 queued 18:32
created

Bookwhen::ticket()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 74
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 43
c 0
b 0
f 0
nc 64
nop 6
dl 0
loc 74
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,
0 ignored issues
show
Bug introduced by
It seems like $classPass->details can also be of type null; however, parameter $id of InShore\Bookwhen\Domain\ClassPass::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

210
            /** @scrutinizer ignore-type */ $classPass->details,
Loading history...
211
            $classPass->id,
0 ignored issues
show
Unused Code introduced by
The call to InShore\Bookwhen\Domain\ClassPass::__construct() has too many arguments starting with $classPass->id. ( Ignorable by Annotation )

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

211
        return /** @scrutinizer ignore-call */ new ClassPass(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
212
            $classPass->number_available,
0 ignored issues
show
Bug introduced by
The property number_available does not exist on InShore\Bookwhen\Respons...Passes\RetrieveResponse. Did you mean available?
Loading history...
213
            $classPass->title,
214
            $classPass->usage_allowance,
0 ignored issues
show
Bug introduced by
The property usage_allowance does not seem to exist on InShore\Bookwhen\Respons...Passes\RetrieveResponse.
Loading history...
215
            $classPass->usage_type,
0 ignored issues
show
Bug introduced by
The property usage_type does not seem to exist on InShore\Bookwhen\Respons...Passes\RetrieveResponse.
Loading history...
216
            $classPass->use_restricted_for_days
0 ignored issues
show
Bug introduced by
The property use_restricted_for_days does not seem to exist on InShore\Bookwhen\Respons...Passes\RetrieveResponse.
Loading history...
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
        $title = null,
228
        $detail = null,
229
        $usageType = null,
230
        $cost = null,
231
        $usageAllowance = null,
232
        $useRestrictedForDays = null
233
    ): array {
234
235
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
236
237
        if (!is_null($title) && !$this->validator->validTitle($title)) {
238
            throw new ValidationException('title', $title);
239
        }
240
241
        // @todo remaingin validation
242
243
        $classPasses = $this->client->classPasses()->list([]);
0 ignored issues
show
Unused Code introduced by
The call to InShore\Bookwhen\Resources\ClassPasses::list() has too many arguments starting with array(). ( Ignorable by Annotation )

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

243
        $classPasses = $this->client->classPasses()->/** @scrutinizer ignore-call */ list([]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
244
245
        foreach ($classPasses->data as $classPass) {
246
            array_push($this->classPasses, new classPasses(
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\classPasses 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...
247
                $ticket->details,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ticket seems to be never defined.
Loading history...
248
                $ticket->id,
249
                $ticket->title
250
            ));
251
        }
252
        return $this->classPasses;
253
    }
254
255
    /**
256
     *
257
     * {@inheritDoc}
258
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::event()
259
     * @todo filters.
260
     */
261
    public function event(
262
        string $eventId,
263
        bool $includeAttachments = false,
264
        bool $includeLocation = false,
265
        bool $includeTickets = false,
266
        bool $includeTicketsClassPasses = false,
267
        bool $includeTicketsEvents = false
268
    ): Event {
269
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
270
271
        if (!$this->validator->validId($eventId, 'event')) {
272
            throw new ValidationException('eventId', $eventId);
273
        }
274
275
        // Validate $includeAttachments;
276
        if (!$this->validator->validInclude($includeAttachments)) {
277
            throw new ValidationException('includeAttachments', $includeAttachments);
278
        } else {
279
            if($includeAttachments) {
280
                array_push($this->includes, 'attachments');
281
            }
282
        }
283
284
        // Validate $includeTickets;
285
        if (!$this->validator->validInclude($includeLocation)) {
286
            throw new ValidationException('includeLocation', $includeLocation);
287
        } else {
288
            if($includeLocation) {
289
                array_push($this->includes, 'location');
290
            }
291
        }
292
293
        // Validate $includeTickets;
294
        if (!$this->validator->validInclude($includeTickets)) {
295
            throw new ValidationException('includeTickets', $includeTickets);
296
        } else {
297
            if($includeTickets) {
298
                array_push($this->includes, 'tickets');
299
            }
300
        }
301
302
        // Validate $includeTicketsEvents;
303
        if (!$this->validator->validInclude($includeTicketsEvents)) {
304
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
305
        } else {
306
            if($includeTicketsEvents) {
307
                array_push($this->includes, 'tickets.events');
308
            }
309
        }
310
311
        // Validate $includeTicketsClassPasses;
312
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
313
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
314
        } else {
315
            if($includeTicketsClassPasses) {
316
                array_push($this->includes, 'tickets.class_passes');
317
            }
318
        }
319
320
        $event = $this->client->events()->retrieve($eventId, ['include' => implode(',', $this->includes)]);
321
322
        // attachments
323
        $eventAttachments = [];
324
325
        foreach ($event->attachments as $eventAttachment) {
326
            $attachment = $this->client->attachments()->retrieve($eventAttachment['id']);
327
            array_push($eventAttachments, new Attachment(
328
                $attachment->contentType,
329
                $attachment->fileUrl,
330
                $attachment->fileSizeBytes,
331
                $attachment->fileSizeText,
332
                $attachment->fileName,
333
                $attachment->fileType,
334
                $attachment->id,
335
                $attachment->title
336
            ));
337
        }
338
339
        // eventTickets
340
        $eventTickets = [];
341
        foreach($event->tickets as $ticket) {
342
            array_push($eventTickets, new Ticket(
343
                $ticket->available,
344
                $ticket->availableFrom,
345
                $ticket->availableTo,
346
                $ticket->builtBasketUrl,
347
                $ticket->builtBasketIframeUrl,
348
                $ticket->courseTicket,
349
                $ticket->details,
350
                $ticket->groupTicket,
351
                $ticket->groupMin,
352
                $ticket->groupMax,
353
                $ticket->id,
354
                $ticket->numberIssued,
355
                $ticket->numberTaken,
356
                $ticket->title
357
            ));
358
        }
359
360
        // ticketsClassPasses
361
        // @todo
362
363
        // ticketsEvents
364
        // @todo
365
366
        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...
367
            $event->allDay,
368
            $eventAttachments,
369
            $event->attendeeCount,
370
            $event->attendeeLimit,
371
            $event->details,
372
            $event->endAt,
373
            $event->id,
374
            new Location(
375
                $event->location->addressText,
376
                $event->location->additionalInfo,
377
                $event->location->id,
378
                $event->location->latitude,
379
                $event->location->longitude,
380
                $event->location->mapUrl,
381
                $event->location->zoom
382
            ),
383
            $event->maxTicketsPerBooking,
384
            $event->startAt,
385
            $eventTickets,
386
            $event->title,
387
            $event->waitingList
388
        );
389
    }
390
391
    /**
392
     *
393
     * {@inheritDoc}
394
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents()
395
     */
396
    public function events(
397
        $calendar = false,
398
        $entry = false,
399
        $location = [],
400
        $tags = [],
401
        $title = [],
402
        $detail = [],
403
        $from = null,
404
        $to = null,
405
        bool $includeAttachments = false,
406
        bool $includeLocation = false,
407
        bool $includeTickets = false,
408
        bool $includeTicketsClassPasses = false,
409
        bool $includeTicketsEvents = false
410
    ): array {
411
412
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
413
414
        // Validate $calendar
415
        // @todo details required
416
417
        // Validate $detail
418
        if (!empty($detail)) {
419
            if (!is_array($detail)) {
420
                throw new ValidationException('detail', implode(' ', $detail));
421
            } else {
422
                $detail = array_unique($detail);
423
                foreach($detail as $item) {
424
                    if (!$this->validator->validLocation($item)) {
425
                        throw new ValidationException('detail', $item);
426
                    }
427
                }
428
                $this->filters['filter[detail]'] = implode(',', $detail);
429
            }
430
        }
431
432
        // Validate $entry
433
        // @todo details required
434
435
        // Validate $from;
436
        if (!empty($from)) {
437
            if (!$this->validator->validFrom($from, $to)) {
438
                throw new ValidationException('from', $from . '-' . $to);
439
            } else {
440
                $this->filters['filter[from]'] = $from;
441
            }
442
        }
443
444
        // Validate $location
445
        if (!empty($location)) {
446
            if (!is_array($location)) {
447
                throw new ValidationException('location', implode(' ', $title));
448
            } else {
449
                $location = array_unique($location);
450
                foreach($location as $item) {
451
                    if (!$this->validator->validLocation($item)) {
452
                        throw new ValidationException('location', $item);
453
                    }
454
                }
455
                $this->filters['filter[location]'] = implode(',', $location);
456
            }
457
        }
458
459
        // Validate $tags.
460
        if (!empty($tags)) {
461
            if (!is_array($tags)) {
462
                throw new ValidationException('tags', implode(' ', $tags));
463
            } else {
464
                $tags = array_unique($tags);
465
                foreach ($tags as $tag) {
466
                    if (!empty($tag) && !$this->validator->validTag($tag)) {
467
                        throw new ValidationException('tag', $tag);
468
                    }
469
                }
470
            }
471
            $this->filters['filter[tag]'] = implode(',', $tags);
472
        }
473
474
        // Validate $title;
475
        if (!empty($title)) {
476
            if (!is_array($title)) {
477
                throw new ValidationException('title', implode(' ', $title));
478
            } else {
479
                $title = array_unique($title);
480
                foreach($title as $item) {
481
                    if (!$this->validator->validTitle($item)) {
482
                        throw new ValidationException('title', $item);
483
                    }
484
                }
485
                $this->filters['filter[title]'] = implode(',', $title);
486
            }
487
        }
488
489
        // Validate $to;
490
        if (!empty($to)) {
491
            if (!$this->validator->validTo($to, $from)) {
492
                throw new ValidationException('to', $to . '-' . $from);
493
            } else {
494
                $this->filters['filter[to]'] = $to;
495
            }
496
        }
497
498
        // Validate $includeAttachments;
499
        if (!$this->validator->validInclude($includeAttachments)) {
500
            throw new ValidationException('includeAttachments', $includeAttachments);
501
        } else {
502
            if($includeAttachments) {
503
                array_push($this->includes, 'attachments');
504
            }
505
        }
506
507
        // Validate $includeTickets;
508
        if (!$this->validator->validInclude($includeLocation)) {
509
            throw new ValidationException('includeLocation', $includeLocation);
510
        } else {
511
            if($includeLocation) {
512
                array_push($this->includes, 'location');
513
            }
514
        }
515
516
        // Validate $includeTickets;
517
        if (!$this->validator->validInclude($includeTickets)) {
518
            throw new ValidationException('includeTickets', $includeTickets);
519
        } else {
520
            if($includeTickets) {
521
                array_push($this->includes, 'tickets');
522
            }
523
        }
524
525
        // Validate $includeTicketsEvents;
526
        if (!$this->validator->validInclude($includeTicketsEvents)) {
527
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
528
        } else {
529
            if($includeTicketsEvents) {
530
                array_push($this->includes, 'tickets.events');
531
            }
532
        }
533
534
        // Validate $includeTicketsClassPasses;
535
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
536
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
537
        } else {
538
            if($includeTicketsClassPasses) {
539
                array_push($this->includes, 'tickets.class_passes');
540
            }
541
        }
542
543
        $events = $this->client->events()->list(array_merge($this->filters, ['include' => implode(',', $this->includes)]));
544
545
        foreach ($events->data as $event) {
546
547
            $eventTickets = [];
548
            foreach($event->tickets as $ticket) {
549
                array_push($eventTickets, new Ticket(
550
                    $ticket->available,
551
                    $ticket->availableFrom,
552
                    $ticket->availableTo,
553
                    $ticket->builtBasketUrl,
554
                    $ticket->builtBasketIframeUrl,
555
                    $ticket->courseTicket,
556
                    $ticket->details,
557
                    $ticket->groupTicket,
558
                    $ticket->groupMin,
559
                    $ticket->groupMax,
560
                    $ticket->id,
561
                    $ticket->numberIssued,
562
                    $ticket->numberTaken,
563
                    $ticket->title
564
                ));
565
            }
566
567
            array_push($this->events, new Event(
568
                $event->allDay,
569
                $event->attachments,
570
                $event->attendeeCount,
571
                $event->attendeeLimit,
572
                $event->details,
573
                $event->endAt,
574
                $event->id,
575
                new Location(
576
                    $event->location->addressText,
577
                    $event->location->additionalInfo,
578
                    $event->location->id,
579
                    $event->location->latitude,
580
                    $event->location->longitude,
581
                    $event->location->mapUrl,
582
                    $event->location->zoom
583
                ),
584
                $event->maxTicketsPerBooking,
585
                $event->startAt,
586
                $eventTickets,
587
                $event->title,
588
                $event->waitingList
589
            ));
590
        }
591
592
        return $this->events;
593
    }
594
595
    /*
596
     *
597
     * {@inheritDoc}
598
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation()
599
     */
600
    public function location(string $locationId): Location
601
    {
602
603
        if (!$this->validator->validId($locationId, 'location')) {
604
            throw new ValidationException('locationId', $locationId);
605
        }
606
607
        $location = $this->client->locations()->retrieve($locationId);
608
609
        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...
610
            $location->additionalInfo,
611
            $location->addressText,
612
            $location->id,
613
            $location->latitude,
614
            $location->longitude,
615
            $location->mapUrl,
616
            $location->zoom
617
        );
618
    }
619
620
    /**
621
     *
622
     */
623
    public function locations(
624
        null | string $addressText = null,
625
        null | string $additionalInfo = null
626
    ): array {
627
628
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
629
630
        if (!empty($additionalInfo)) {
631
            if (!$this->validator->validAdditionalInfo($additionalInfo, 'additionalInfo')) {
632
                throw new ValidationException('additionalInfo', $additionalInfo);
633
            }
634
            $this->filters['filter[additional_info]'] = $additionalInfo;
635
        }
636
637
        if (!empty($addressText)) {
638
            if (!$this->validator->validAddressText($addressText, 'addressText')) {
639
                throw new ValidationException('addressText', $addressText);
640
            }
641
            $this->filters['filter[address_text]'] = $addressText;
642
        }
643
644
        $locations = $this->client->locations()->list($this->filters);
645
646
        foreach ($locations->data as $location) {
647
            array_push($this->locations, new Location(
648
                $location->additionalInfo,
649
                $location->addressText,
650
                $location->id,
651
                $location->latitude,
652
                $location->longitude,
653
                $location->mapUrl,
654
                $location->zoom
655
            ));
656
        }
657
658
        return $this->locations;
659
660
    }
661
662
    /**
663
     * Set Debug.
664
     * @deprecated
665
     */
666
    public function setLogging($level)
667
    {
668
        $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...
669
    }
670
671
     /**
672
     * Sets the token for all future new instances
673
     * @deprecated
674
     * @param $token string The API access token, as obtained on diffbot.com/dev.
675
     */
676
    public static function setToken($token)
677
    {
678
        $validator = new Validator();
679
        if (!$validator->validToken($token)) {
680
            throw new \InvalidArgumentException('Invalid Token.');
681
        }
682
        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...
683
    }
684
685
    /**
686
     * {@inheritDoc}
687
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::ticket()
688
     * class_passes
689
     */
690
    public function ticket(
691
        string $ticketId,
692
        bool $includeClassPasses = false,
693
        bool $includeEvents = false,
694
        bool $includeEventsAttachments = false,
695
        bool $includeEventsLocation = false,
696
        bool $includeEventsTickets = false
697
    ): Ticket {
698
        if (!$this->validator->validId($ticketId, 'ticket')) {
699
            throw new ValidationException('ticketId', $ticketId);
700
        }
701
702
        // Validate $includeClassPasses;
703
        if (!$this->validator->validInclude($includeClassPasses)) {
704
            throw new ValidationException('includeClassPasses', $includeClassPasses);
705
        } else {
706
            if($includeClassPasses) {
707
                array_push($this->includes, 'class_passes');
708
            }
709
        }
710
711
        // Validate $includeEvents;
712
        if (!$this->validator->validInclude($includeEvents)) {
713
            throw new ValidationException('includeEvents', $includeEvents);
714
        } else {
715
            if($includeEvents) {
716
                array_push($this->includes, 'events');
717
            }
718
        }
719
720
        // Validate $includeAttachments;
721
        if (!$this->validator->validInclude($includeEventsAttachments)) {
722
            throw new ValidationException('includeEventssAttachments', $includeEventsAttachments);
723
        } else {
724
            if($includeEventsAttachments) {
725
                array_push($this->includes, 'events.attachments');
726
            }
727
        }
728
729
        // Validate $includeEventsLocation;
730
        if (!$this->validator->validInclude($includeEventsLocation)) {
731
            throw new ValidationException('includeEventsLocation', $includeEventsLocation);
732
        } else {
733
            if($includeEventsLocation) {
734
                array_push($this->includes, 'events.location');
735
            }
736
        }
737
738
        // Validate $includeEventsTickets;
739
        if (!$this->validator->validInclude($includeEventsTickets)) {
740
            throw new ValidationException('includeEventsTickets', $includeEventsTickets);
741
        } else {
742
            if($includeEventsTickets) {
743
                array_push($this->includes, 'events.tickets');
744
            }
745
        }
746
747
        $ticket = $this->client->tickets()->retrieve($ticketId);
748
749
        return $this->ticket = new Ticket(
750
            $ticket->available,
751
            $ticket->availableFrom,
752
            $ticket->availableTo,
753
            $ticket->builtBasketUrl,
754
            $ticket->builtBasketIframeUrl,
755
            $ticket->courseTicket,
756
            $ticket->details,
757
            $ticket->groupTicket,
758
            $ticket->groupMin,
759
            $ticket->groupMax,
760
            $ticket->id,
761
            $ticket->numberIssued,
762
            $ticket->numberTaken,
763
            $ticket->title
764
        );
765
    }
766
767
    /**
768
     * {@inheritDoc}
769
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::tickets()
770
     * @todo includes
771
     */
772
    public function tickets(
773
        string $eventId,
774
        bool $includeClassPasses = false,
775
        bool $includeEvents = false,
776
        bool $includeEventsAttachments = false,
777
        bool $includeEventsLocation = false,
778
        bool $includeEventsTickets = false
779
    ): array {
780
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
781
782
        if (!$this->validator->validId($eventId, 'event')) {
783
            throw new ValidationException('eventId', $eventId);
784
        }
785
786
        // Validate $includeClassPasses;
787
        if (!$this->validator->validInclude($includeClassPasses)) {
788
            throw new ValidationException('includeClassPasses', $includeClassPasses);
789
        } else {
790
            if($includeClassPasses) {
791
                array_push($this->includes, 'class_passes');
792
            }
793
        }
794
795
        // Validate $includeEvents;
796
        if (!$this->validator->validInclude($includeEvents)) {
797
            throw new ValidationException('includeEvents', $includeEvents);
798
        } else {
799
            if($includeEvents) {
800
                array_push($this->includes, 'events');
801
            }
802
        }
803
804
        // Validate $includeAttachments;
805
        if (!$this->validator->validInclude($includeEventsAttachments)) {
806
            throw new ValidationException('includeAttachments', $includeEventsAttachments);
807
        } else {
808
            if($includeEventsAttachments) {
809
                array_push($this->includes, 'events.attachments');
810
            }
811
        }
812
813
        // Validate $includeEventsLocation;
814
        if (!$this->validator->validInclude($includeEventsLocation)) {
815
            throw new ValidationException('includeEventsLocation', $includeEventsLocation);
816
        } else {
817
            if($includeEventsLocation) {
818
                array_push($this->includes, 'events.location');
819
            }
820
        }
821
822
        // Validate $includeEventsTickets;
823
        if (!$this->validator->validInclude($includeEventsTickets)) {
824
            throw new ValidationException('includeEventsTickets', $includeEventsTickets);
825
        } else {
826
            if($includeEventsTickets) {
827
                array_push($this->includes, 'events.tickets');
828
            }
829
        }
830
831
        $tickets = $this->client->tickets()->list(array_merge(['event_id' => $eventId], ['include' => implode(',', $this->includes)]));
832
833
        foreach ($tickets->data as $ticket) {
834
            array_push($this->tickets, new Ticket(
835
                $ticket->available,
836
                $ticket->availableFrom,
837
                $ticket->availableTo,
838
                $ticket->builtBasketUrl,
839
                $ticket->builtBasketIframeUrl,
840
                $ticket->courseTicket,
841
                $ticket->details,
842
                $ticket->groupTicket,
843
                $ticket->groupMin,
844
                $ticket->groupMax,
845
                $ticket->id,
846
                $ticket->numberIssued,
847
                $ticket->numberTaken,
848
                $ticket->title
849
            ));
850
        }
851
852
        return $this->tickets;
853
854
    }
855
}
856