Passed
Push — develop ( 2390ef...f7a57e )
by Daniel
24:40 queued 09:37
created

Bookwhen::location()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.9
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;
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\Domain\ClassPass 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...
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...
Bug introduced by
The call to InShore\Bookwhen\Domain\Attachment::__construct() has too few arguments starting with fileSizeBytes. ( Ignorable by Annotation )

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

132
        return $this->attachment = /** @scrutinizer ignore-call */ new Attachment(

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
133
            $attachment->id,
134
            $attachment->title,
135
        );
136
    }
137
138
    /**
139
     *
140
     * {@inheritDoc}
141
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::attachment()
142
     * @todo
143
     */
144
    public function attachments(
145
        string $title = null,
146
        string $fileName = null,
147
        string $fileType = null
148
    ): array {
149
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
150
151
        if (!is_null($title) && !$this->validator->validTitle($title)) {
152
            throw new ValidationException('title', $title);
153
        }
154
155
        if (!is_null($fileName) && !$this->validator->validFileName($fileName)) {
156
            throw new ValidationException('file name', $fileName);
157
        }
158
159
        if (!is_null($fileType) && !$this->validator->validFileType($fileType)) {
160
            throw new ValidationException('file type', $fileType);
161
        }
162
163
        $attachments = $this->client->attachments()->list();
164
165
        foreach ($attachments->data as $attachment) {
166
            array_push($this->attachment, new Attachment(
0 ignored issues
show
Bug introduced by
$this->attachment of type InShore\Bookwhen\Domain\Attachment is incompatible with the type array expected by parameter $array of array_push(). ( Ignorable by Annotation )

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

166
            array_push(/** @scrutinizer ignore-type */ $this->attachment, new Attachment(
Loading history...
Bug introduced by
The call to InShore\Bookwhen\Domain\Attachment::__construct() has too few arguments starting with fileUrl. ( Ignorable by Annotation )

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

166
            array_push($this->attachment, /** @scrutinizer ignore-call */ new Attachment(

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
167
                $attachment->id,
168
            ));
169
        }
170
171
        return $this->attachments;
172
    }
173
174
    /**
175
     *
176
     * {@inheritDoc}
177
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPass()
178
     * @todo
179
     */
180
    public function classPass(string $classPassId): ClassPass
181
    {
182
        //         $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
183
184
        //       if (!$this->validator->validId($classPassId, 'classPass')) {
185
        //           throw new ValidationException('classPassId', $classPassId);
186
        //      }
187
188
        $classPass = $this->client->classPasses()->retrieve($classPassId);
189
190
        return new ClassPass(
191
            $classPass->details,
192
            $classPass->id,
193
            $classPass->number_available,
194
            $classPass->title,
195
            $classPass->usage_allowance,
196
            $classPass->usage_type,
197
            $classPass->use_restricted_for_days
198
        );
199
    }
200
201
    /**
202
     *
203
     * {@inheritDoc}
204
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getClassPasses()
205
     * @todo break params on to multiplper lines..
206
     */
207
    public function classPasses(
208
        $title = null,
209
        $detail = null,
210
        $usageType = null,
211
        $cost = null,
212
        $usageAllowance = null,
213
        $useRestrictedForDays = null
214
    ): array {
215
216
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
217
218
        if (!is_null($title) && !$this->validator->validTitle($title)) {
219
            throw new ValidationException('title', $title);
220
        }
221
222
        // @todo remaingin validation
223
224
        $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

224
        $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...
225
226
        foreach ($classPasses->data as $classPass) {
227
            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...
228
                $ticket->details,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ticket seems to be never defined.
Loading history...
229
                $ticket->id,
230
                $ticket->title
231
            ));
232
        }
233
        return $this->classPasses;
234
    }
235
236
    /**
237
     *
238
     * {@inheritDoc}
239
     * @see \InShore\Bookwhen\Interfaces\BookwhenInterface::event()
240
     * @todo filters.
241
     */
242
    public function event(
243
        string $eventId,
244
        bool $attachments = false,
245
        bool $location = false,
246
        bool $tickets = false,
247
        bool $ticketsClassPasses = false,
248
        bool $ticketsEventslocation = false
249
    ): Event {
250
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
251
252
        if (!$this->validator->validId($eventId, 'event')) {
253
            throw new ValidationException('eventId', $eventId);
254
        }
255
256
        $event = $this->client->events()->retrieve($eventId);
257
258
        $eventAttachments = [];
259
        $eventTickets = [];
260
261
        // attachments
262
        if($attachments) {
263
            foreach ($event->attachments as $eventAttachment) {
264
                $attachment = $this->client->attachments()->retrieve($eventAttachment['id']);
265
                array_push($eventAttachments, new Attachment(
266
                    $attachment->contentType,
0 ignored issues
show
Bug introduced by
The property contentType does not seem to exist on InShore\Bookwhen\Respons...hments\RetrieveResponse.
Loading history...
267
                    $attachment->fileUrl,
0 ignored issues
show
Bug introduced by
The property fileUrl does not seem to exist on InShore\Bookwhen\Respons...hments\RetrieveResponse.
Loading history...
268
                    $attachment->fileSizeBytes,
0 ignored issues
show
Bug introduced by
The property fileSizeBytes does not seem to exist on InShore\Bookwhen\Respons...hments\RetrieveResponse.
Loading history...
269
                    $attachment->fileSizeText,
0 ignored issues
show
Bug introduced by
The property fileSizeText does not seem to exist on InShore\Bookwhen\Respons...hments\RetrieveResponse.
Loading history...
270
                    $attachment->fileName,
0 ignored issues
show
Bug introduced by
The property fileName does not seem to exist on InShore\Bookwhen\Respons...hments\RetrieveResponse.
Loading history...
271
                    $attachment->fileType,
0 ignored issues
show
Bug introduced by
The property fileType does not seem to exist on InShore\Bookwhen\Respons...hments\RetrieveResponse.
Loading history...
272
                    $attachment->id
273
                ));
274
            }
275
        }
276
277
        // location
278
        // @todo resolve dynamic type
279
        if(!$location) {
280
            $eventLocation = new Location(
281
                null,
282
                null,
283
                $event->locationId
0 ignored issues
show
Bug introduced by
The property locationId does not exist on InShore\Bookwhen\Responses\Events\RetrieveResponse. Did you mean location?
Loading history...
284
            );
285
        } else {
286
            $location = $this->client->locations()->retrieve($event->locationId);
287
            $eventLocation = new Location(
288
                $location->addressText,
289
                $location->additionalInfo,
290
                $location->id,
291
                $location->latitude,
292
                $location->longitude,
293
                $location->mapUrl,
294
                $location->zoom
295
            );
296
        }
297
298
        // tickets
299
        if($tickets) {
300
            foreach ($event->tickets as $eventTicket) {
301
                $ticket = $this->client->tickets()->retrieve($eventTicket['id']);
302
                array_push($eventTickets, new Ticket(
303
                    $ticket->available,
304
                    $ticket->availableFrom,
305
                    $ticket->availableTo,
306
                    $ticket->builtBasketUrl,
307
                    $ticket->builtBasketIframeUrl,
308
                    $ticket->courseTicket,
309
                    $ticket->details,
310
                    $ticket->groupTicket,
311
                    $ticket->groupMin,
312
                    $ticket->groupMax,
313
                    $ticket->id,
314
                    $ticket->numberIssued,
315
                    $ticket->numberTaken,
316
                    $ticket->title
317
                ));
318
            }
319
        }
320
321
        // ticketsClassPasses
322
        // @todo
323
324
        // ticketsEvents
325
        // @todo
326
327
        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...
328
            $event->allDay,
329
            $eventAttachments,
330
            $event->attendeeCount,
331
            $event->attendeeLimit,
332
            $event->details,
333
            $event->endAt,
334
            $event->id,
335
            $eventLocation,
336
            $event->maxTicketsPerBooking,
337
            $event->startAt,
338
            $eventTickets,
339
            $event->title,
340
            $event->waitingList
341
        );
342
    }
343
344
    /**
345
     *
346
     * {@inheritDoc}
347
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getEvents()
348
     */
349
    public function events(
350
        $calendar = false,
351
        $entry = false,
352
        $location = [],
353
        $tags = [],
354
        $title = [],
355
        $detail = [],
356
        $from = null,
357
        $to = null,
358
        $includeAttachments = false,
359
        $includeLocation = false,
360
        $includeTickets = false,
361
        $includeTicketsClassPasses = false,
362
        $includeTicketsEvents = false
363
    ): array {
364
365
        //$this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
366
367
        // Validate $calendar
368
        // @todo details required
369
370
        // Validate $detail
371
        if (!empty($detail)) {
372
            if (!is_array($detail)) {
373
                throw new ValidationException('detail', implode(' ', $detail));
374
            } else {
375
                $detail = array_unique($detail);
376
                foreach($detail as $item) {
377
                    if (!$this->validator->validLocation($item)) {
378
                        throw new ValidationException('detail', $item);
379
                    }
380
                }
381
                $this->filters['filter[detail]'] = implode(',', $detail);
382
            }
383
        }
384
385
        // Validate $entry
386
        // @todo details required
387
388
        // Validate $from;
389
        if (!empty($from)) {
390
            if (!$this->validator->validFrom($from, $to)) {
391
                throw new ValidationException('from', $from . '-' . $to);
392
            } else {
393
                $this->filters['filter[from]'] = $from;
394
            }
395
        }
396
397
        // Validate $location
398
        if (!empty($location)) {
399
            if (!is_array($location)) {
400
                throw new ValidationException('location', implode(' ', $title));
401
            } else {
402
                $location = array_unique($location);
403
                foreach($location as $item) {
404
                    if (!$this->validator->validLocation($item)) {
405
                        throw new ValidationException('location', $item);
406
                    }
407
                }
408
                $this->filters['filter[location]'] = implode(',', $location);
409
            }
410
        }
411
412
        // Validate $tags.
413
        if (!empty($tags)) {
414
            if (!is_array($tags)) {
415
                throw new ValidationException('tags', implode(' ', $tags));
416
            } else {
417
                $tags = array_unique($tags);
418
                foreach ($tags as $tag) {
419
                    if (!empty($tag) && !$this->validator->validTag($tag)) {
420
                        throw new ValidationException('tag', $tag);
421
                    }
422
                }
423
            }
424
            $this->filters['filter[tag]'] = implode(',', $tags);
425
        }
426
427
        // Validate $title;
428
        if (!empty($title)) {
429
            if (!is_array($title)) {
430
                throw new ValidationException('title', implode(' ', $title));
431
            } else {
432
                $title = array_unique($title);
433
                foreach($title as $item) {
434
                    if (!$this->validator->validTitle($item)) {
435
                        throw new ValidationException('title', $item);
436
                    }
437
                }
438
                $this->filters['filter[title]'] = implode(',', $title);
439
            }
440
        }
441
442
        // Validate $to;
443
        if (!empty($to)) {
444
            if (!$this->validator->validTo($to, $from)) {
445
                throw new ValidationException('to', $to . '-' . $from);
446
            } else {
447
                $this->filters['filter[to]'] = $to;
448
            }
449
        }
450
451
        // Validate $includeAttachments;
452
        if (!$this->validator->validInclude($includeAttachments)) {
453
            throw new ValidationException('includeAttachments', $includeAttachments);
454
        } else {
455
            if($includeAttachments) {
456
                array_push($this->includes, 'attachments');
457
            }
458
        }
459
460
        // Validate $includeTickets;
461
        if (!$this->validator->validInclude($includeLocation)) {
462
            throw new ValidationException('includeLocation', $includeLocation);
463
        } else {
464
            if($includeLocation) {
465
                array_push($this->includes, 'location');
466
            }
467
        }
468
469
        // Validate $includeTickets;
470
        if (!$this->validator->validInclude($includeTickets)) {
471
            throw new ValidationException('includeTickets', $includeTickets);
472
        } else {
473
            if($includeTickets) {
474
                array_push($this->includes, 'tickets');
475
            }
476
        }
477
478
        // Validate $includeTicketsEvents;
479
        if (!$this->validator->validInclude($includeTicketsEvents)) {
480
            throw new ValidationException('includeTicketsEvents', $includeTicketsEvents);
481
        } else {
482
            if($includeTickets) {
483
                array_push($this->includes, 'tickets.events');
484
            }
485
        }
486
487
        // Validate $includeTicketsClassPasses;
488
        if (!$this->validator->validInclude($includeTicketsClassPasses)) {
489
            throw new ValidationException('includeTicketsClassPasses', $includeTicketsClassPasses);
490
        } else {
491
            if($includeTicketsClassPasses) {
492
                array_push($this->includes, 'tickets.class_passes');
493
            }
494
        }
495
496
        $events = $this->client->events()->list(array_merge($this->filters, ['include' => implode(',', $this->includes)]));
497
498
        foreach ($events->data as $event) {
499
500
            $eventLocation = new Location(
501
                $event->location->addressText,
502
                $event->location->additionalInfo,
503
                $event->location->id,
504
                $event->location->latitude,
505
                $event->location->longitude,
506
                $event->location->mapUrl,
507
                $event->location->zoom
508
                );
509
                        
510
            array_push($this->events, new Event(
511
                $event->allDay,
512
                $event->attachments,
513
                $event->attendeeCount,
514
                $event->attendeeLimit,
515
                $event->details,
516
                $event->endAt,
517
                $event->id,
518
                $eventLocation,
519
                $event->maxTicketsPerBooking,
520
                $event->startAt,
521
                $event->tickets,
522
                $event->title,
523
                $event->waitingList
524
            ));
525
        }
526
527
        return $this->events;
528
    }
529
530
    /*
531
     *
532
     * {@inheritDoc}
533
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocation()
534
     */
535
    public function location(string $locationId): Location
536
    {
537
538
        if (!$this->validator->validId($locationId, 'location')) {
539
            throw new ValidationException('locationId', $locationId);
540
        }
541
542
        $location = $this->client->locations()->retrieve($locationId);
543
544
        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...
545
            $location->additionalInfo,
546
            $location->addressText,
547
            $location->id,
548
            $location->latitude,
549
            $location->longitude,
550
            $location->mapUrl,
551
            $location->zoom
552
        );
553
    }
554
555
    /**
556
     *
557
     * {@inheritDoc}
558
     * @see \InShore\Bookwhen\Interfaces\ClientInterface::getLocations()
559
     * @todo validate params.
560
     */
561
    public function locations(
562
        null | string $addressText = null,
563
        null | string $additionalInfo = null
564
    ): array {
565
566
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
567
568
        if (!$this->validator->validId($eventId, 'event')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $eventId seems to be never defined.
Loading history...
569
            throw new ValidationException('eventId', $eventId);
570
        }
571
572
        $locations = $this->client->locations()->list();
573
574
        foreach ($locations->data as $location) {
575
            array_push($this->locations, new Location(
576
                $location->additionalInfo,
577
                $location->addressText,
578
                $location->id,
579
                $location->latitude,
580
                $location->longitude,
581
                $location->mapUrl,
582
                $location->zoom
583
            ));
584
        }
585
586
        return $this->locations;
587
588
    }
589
590
    /**
591
     * Set Debug.
592
     * @deprecated
593
     */
594
    public function setLogging($level)
595
    {
596
        $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...
597
    }
598
599
     /**
600
     * Sets the token for all future new instances
601
     * @deprecated
602
     * @param $token string The API access token, as obtained on diffbot.com/dev.
603
     */
604
    public static function setToken($token)
605
    {
606
        $validator = new Validator();
607
        if (!$validator->validToken($token)) {
608
            throw new \InvalidArgumentException('Invalid Token.');
609
        }
610
        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...
611
    }
612
613
    /**
614
     * {@inheritDoc}
615
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::ticket()
616
     */
617
    public function ticket($ticketId): Ticket
618
    {
619
        if (!$this->validator->validId($ticketId, 'ticket')) {
620
            throw new ValidationException('ticketId', $ticketId);
621
        }
622
623
        $ticket = $this->client->tickets()->retrieve($ticketId);
624
625
        return $this->ticket = new Ticket(
626
            $ticket->available,
627
            $ticket->availableFrom,
628
            $ticket->availableTo,
629
            $ticket->builtBasketUrl,
630
            $ticket->builtBasketIframeUrl,
631
            $ticket->courseTicket,
632
            $ticket->details,
633
            $ticket->groupTicket,
634
            $ticket->groupMin,
635
            $ticket->groupMax,
636
            $ticket->id,
637
            $ticket->numberIssued,
638
            $ticket->numberTaken,
639
            $ticket->title
640
        );
641
    }
642
643
    /**
644
     * {@inheritDoc}
645
     * @see \InShore\Bookwhen\Interfaces\BookWhenInterface::tickets()
646
     * @todo includes
647
     */
648
    public function tickets(string $eventId): array
649
    {
650
        // $this->logger->debug(__METHOD__ . '(' . var_export(func_get_args(), true) . ')');
651
652
        if (!$this->validator->validId($eventId, 'event')) {
653
            throw new ValidationException('eventId', $eventId);
654
        }
655
656
        $tickets = $this->client->tickets()->list([
657
            'event_id' => $eventId,
658
            'include' => 'events.tickets'
659
        ]);
660
661
        foreach ($tickets->data as $ticket) {
662
            array_push($this->tickets, new Ticket(
663
                $ticket->available,
664
                $ticket->availableFrom,
665
                $ticket->availableTo,
666
                $ticket->builtBasketUrl,
667
                $ticket->builtBasketIframeUrl,
668
                $ticket->courseTicket,
669
                $ticket->details,
670
                $ticket->groupTicket,
671
                $ticket->groupMin,
672
                $ticket->groupMax,
673
                $ticket->id,
674
                $ticket->numberIssued,
675
                $ticket->numberTaken,
676
                $ticket->title
677
            ));
678
        }
679
680
        return $this->tickets;
681
682
    }
683
}
684