Passed
Push — develop ( a777ee...9030e5 )
by Daniel
02:48 queued 01:05
created

RetrieveResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 82.5%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 35
c 6
b 0
f 0
dl 0
loc 81
ccs 33
cts 40
cp 0.825
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B from() 0 54 9
A __construct() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen\Responses\Events;
6
7
use InShore\Bookwhen\Contracts\ResponseContract;
8
use InShore\Bookwhen\Responses\Attachments\RetrieveResponse as AttachmentsRetrieveResponse;
9
use InShore\Bookwhen\Responses\Locations\RetrieveResponse as LocationsRetrieveResponse;
10
use InShore\Bookwhen\Responses\Tickets\RetrieveResponse as TicketsRetrieveResponse;
11
12
/**
13
 * @implements ResponseContract<array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}>
14
 */
15
final class RetrieveResponse implements ResponseContract
16
{
17
    /**
18
     * @param  array<array-key, mixed>|null  $statusDetails
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed>|null at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>|null.
Loading history...
19
     */
20 1
    private function __construct(
21
        public readonly bool $allDay,
22
        public readonly array $attachments,
23
        public readonly int $attendeeCount,
24
        public readonly int $attendeeLimit,
25
        public readonly string $details,
26
        public readonly string $endAt,
27
        public readonly string $id,
28
        public readonly \InShore\Bookwhen\Responses\Locations\RetrieveResponse $location,
29
        public readonly int $maxTicketsPerBooking,
30
        public readonly string $startAt,
31
        public readonly array $tickets,
32
        public readonly string $title,
33
        public readonly bool $waitingList
34
    ) {
35 1
    }
36
37
    /**
38
     * Acts as static factory, and returns a new Response instance.
39
     *
40
     * @param  array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}  $attributes
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{id: string, object...ey, mixed>|string|null} at position 34 could not be parsed: Unknown type name 'array-key' at position 34 in array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}.
Loading history...
41
     */
42 1
    public static function from(array $attributes, $included = []): self
43
    {
44
45
        // attachments
46 1
        $attachments = [];
47 1
        foreach ($attributes['relationships']['attachments']['data'] as $attachment) {
48 1
            array_push($attachments, AttachmentsRetrieveResponse::from([
49 1
                'id' => $attachment['id']
50 1
            ]));
51
        }
52
53
        // location
54 1
        $locationId = $attributes['relationships']['location']['data']['id'];
55 1
        $location = array_reduce($included, function ($data, $includedData) use ($locationId) {
56
            if ($includedData['id'] === $locationId) {
57
                return  LocationsRetrieveResponse::from($includedData);
58
            }
59
            return $data;
60 1
        }, LocationsRetrieveResponse::from([
61 1
            'id' => $attributes['relationships']['location']['data']['id']
62 1
        ]));
63
64
        // tickets
65 1
        $tickets = [];
66 1
        foreach ($attributes['relationships']['tickets']['data'] as $ticket) {
67 1
            array_push($tickets, TicketsRetrieveResponse::from([
68 1
                'id' => $ticket['id']
69 1
            ]));
70
        }
71
72 1
        if (!empty($included)) {
73
            foreach ($tickets as $index => $ticket) {
74
                foreach ($included as $includedData) {
75
                    if ($includedData['type'] && $ticket->id === $includedData['id']) {
76
                        $tickets[$index] = TicketsRetrieveResponse::from($includedData);
77
                    }
78
                }
79
            }
80
        }
81
82 1
        return new self(
83 1
            $attributes['attributes']['all_day'],
84 1
            $attachments,
85 1
            $attributes['attributes']['attendee_count'],
86 1
            $attributes['attributes']['attendee_limit'],
87 1
            $attributes['attributes']['details'],
88 1
            $attributes['attributes']['end_at'],
89 1
            $attributes['id'],
90 1
            $location,
91 1
            $attributes['attributes']['max_tickets_per_booking'],
92 1
            $attributes['attributes']['start_at'],
93 1
            $tickets,
94 1
            $attributes['attributes']['title'],
95 1
            $attributes['attributes']['waiting_list']
96 1
        );
97
    }
98
}
99