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

RetrieveResponse::from()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9.5062

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 54
ccs 31
cts 38
cp 0.8158
crap 9.5062
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 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