Passed
Pull Request — develop (#89)
by
unknown
10:54
created

RetrieveResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 2
b 0
f 0
nc 1
nop 13
dl 0
loc 15
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Concerns\ArrayAccessible;
9
use InShore\Bookwhen\Responses\Locations\RetrieveResponse as LocationsRetrieveResponse;
10
use InShore\Bookwhen\Responses\Tickets\RetrieveResponse as TicketsRetrieveResponse;
11
12
//use OpenAI\Testing\Responses\Concerns\Fakeable;
13
14
/**
15
 * @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}>
16
 */
17
final class RetrieveResponse implements ResponseContract
18
{
19
    /**
20
     * @use ArrayAccessible<array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}>
21
     */
22
    use ArrayAccessible;
23
    
24
    //use Fakeable;
25
    
26
    /**
27
     * @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...
28
     */
29
    private function __construct(
30
        public readonly bool $allDay,
31
        public readonly array $attachments,
32
        public readonly int $attendeeCount,
33
        public readonly int $attendeeLimit,
34
        public readonly string $details,
35
        public readonly string $endAt,
36
        public readonly string $id,
37
        public readonly \InShore\Bookwhen\Responses\Locations\RetrieveResponse $location,
38
        public readonly int $maxTicketsPerBooking,
39
        public readonly string $startAt,
40
        public readonly array $tickets,
41
        public readonly string $title,
42
        public readonly bool $waitingList
43
        ) {
44
    }
45
    
46
    /**
47
     * Acts as static factory, and returns a new Response instance.
48
     *
49
     * @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...
50
     */
51
    public static function from(array $attributes, $included = []): self
52
    {
53
        // location
54
        $location = LocationsRetrieveResponse::from([
55
            'attributes' => [
56
                'address_text' => null,
57
                'additional_info' => null,
58
                'latitude' => null,
59
                'longitude' => null,
60
                'map_url' => null,
61
                'zoom' => null
62
            ],
63
            'id' => $attributes['relationships']['location']['data']['id']
64
        ]);
65
        
66
        // tickets
67
        $tickets = [];
68
        foreach($attributes['relationships']['tickets']['data'] as $ticket) {
69
            array_push($tickets, TicketsRetrieveResponse::from([
70
                'attributes' => [
71
                    'available' => null,
72
                    'available_from' => null,
73
                    'available_to' => null,
74
                    'built_basket_url' => null,
75
                    'built_basket_iframe_url' => null,
76
                    'course_ticket' => null,
77
                    'details' => null,
78
                    'group_ticket' => null,
79
                    'group_min' => null,
80
                    'group_max' => null,
81
                    'number_issued' => null,
82
                    'number_taken' => null,
83
                    'title' => null
84
                ],
85
                'id' => $ticket['id']
86
            ]));
87
        }
88
        
89
        if(!empty($included)) {
90
            foreach ($included as $includedData) {
91
                if($includedData['type'] === 'location' && $includedData['id'] = $location->id) {
92
                    $location = LocationsRetrieveResponse::from($includedData);
93
                }
94
            }
95
            
96
            //tickets
97
            foreach($tickets as $index => $ticket) {
98
                foreach ($included as $includedData) {
99
                    if($includedData['type'] === 'ticket' && $includedData['id'] = $ticket->id) {
100
                        $tickets[$index] = TicketsRetrieveResponse::from($includedData);
101
                    }
102
                }
103
            }
104
        }
105
        
106
        return new self(
107
            $attributes['attributes']['all_day'],
108
            $attributes['relationships']['attachments']['data'],
109
            $attributes['attributes']['attendee_count'],
110
            $attributes['attributes']['attendee_limit'],
111
            $attributes['attributes']['details'],
112
            $attributes['attributes']['end_at'],
113
            $attributes['id'],
114
            $location,
115
            $attributes['attributes']['max_tickets_per_booking'],
116
            $attributes['attributes']['start_at'],
117
            $tickets,
118
            $attributes['attributes']['title'],
119
            $attributes['attributes']['waiting_list']
120
            );
121
    }
122
}
123