RetrieveResponse::from()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 9.0117

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 9
eloc 34
c 6
b 0
f 0
nc 8
nop 2
dl 0
loc 55
ccs 36
cts 38
cp 0.9474
crap 9.0117
rs 8.0555

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\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 4
    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 4
    }
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 4
    public static function from(array $attributes, $included = []): self
43
    {
44
45
        //var_export($attributes);die();
46
        // attachments
47 4
        $attachments = [];
48 4
        foreach ($attributes['relationships']['attachments']['data'] as $attachment) {
49 2
            array_push($attachments, AttachmentsRetrieveResponse::from([
50 2
                'id' => $attachment['id']
51 2
            ]));
52
        }
53
54
        // location
55 4
        $locationId = $attributes['relationships']['location']['data']['id'];
56 4
        $location = array_reduce($included, function ($data, $includedData) use ($locationId) {
57 1
            if ($includedData['id'] === $locationId) {
58 1
                return  LocationsRetrieveResponse::from($includedData);
59
            }
60
            return $data;
61 4
        }, LocationsRetrieveResponse::from([
62 4
            'id' => $attributes['relationships']['location']['data']['id']
63 4
        ]));
64
65
        // tickets
66 4
        $tickets = [];
67 4
        foreach ($attributes['relationships']['tickets']['data'] as $ticket) {
68 4
            array_push($tickets, TicketsRetrieveResponse::from([
69 4
                'id' => $ticket['id']
70 4
            ]));
71
        }
72
73 4
        if (!empty($included)) {
74 1
            foreach ($tickets as $index => $ticket) {
75 1
                foreach ($included as $includedData) {
76 1
                    if ($includedData['type'] && $ticket->id === $includedData['id']) {
77
                        $tickets[$index] = TicketsRetrieveResponse::from($includedData);
78
                    }
79
                }
80
            }
81
        }
82
83 4
        return new self(
84 4
            $attributes['attributes']['all_day'],
85 4
            $attachments,
86 4
            $attributes['attributes']['attendee_count'],
87 4
            $attributes['attributes']['attendee_limit'],
88 4
            $attributes['attributes']['details'],
89 4
            $attributes['attributes']['end_at'],
90 4
            $attributes['id'],
91 4
            $location,
92 4
            $attributes['attributes']['max_tickets_per_booking'],
93 4
            $attributes['attributes']['start_at'],
94 4
            $tickets,
95 4
            $attributes['attributes']['title'],
96 4
            $attributes['attributes']['waiting_list']
97 4
        );
98
    }
99
}
100