PickupPoint::parseOpenings()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 19
c 2
b 1
f 0
nc 4
nop 7
dl 0
loc 37
rs 9.3222
1
<?php
2
3
namespace DansMaCulotte\MondialRelay\Resources;
4
5
class PickupPoint
6
{
7
    /** @var string */
8
    public $id;
9
10
    /** @var string */
11
    public $name;
12
13
    /** @var string */
14
    public $nameAdditional;
15
16
    /** @var string */
17
    public $address;
18
19
    /** @var string */
20
    public $addressOptional;
21
22
    /** @var string */
23
    public $postalCode;
24
25
    /** @var string */
26
    public $city;
27
28
    /** @var string */
29
    public $countryCode;
30
31
    /** @var string */
32
    public $latitude;
33
34
    /** @var string */
35
    public $longitude;
36
37
    /** @var string */
38
    public $activityTypeCode;
39
40
    /** @var string */
41
    public $distance;
42
43
    /** @var string */
44
    public $locationHint;
45
46
    /** @var string */
47
    public $locationHintOptional;
48
49
    /** @var array */
50
    public $openings;
51
52
    /** @var string */
53
    public $holidays;
54
55
    /** @var string */
56
    public $startHolidays;
57
58
    /** @var string */
59
    public $endHolidays;
60
61
    /** @var string */
62
    public $mapUrl;
63
64
    /** @var string */
65
    public $photoUrl;
66
67
68
    public function __construct($parameters)
69
    {
70
        $parametersMap = [
71
            'Num' => 'id',
72
            'LgAdr1' => 'name',
73
            'LgAdr2' => 'nameAdditional',
74
            'LgAdr3' => 'address',
75
            'LgAdr4' => 'addressOptional',
76
            'CP' => 'postalCode',
77
            'Ville' => 'city',
78
            'Pays' => 'countryCode',
79
            'Latitude' => 'latitude',
80
            'Longitude' => 'longitude',
81
            'TypeActivite' => 'activityTypeCode',
82
            'Distance' => 'distance',
83
            'Localisation1' => 'locationHint',
84
            'Localisation2' => 'locationHintOptional',
85
            'Debut' => 'startHolidays',
86
            'Fin' => 'endHolidays',
87
            'URL_Plan' => 'mapUrl',
88
            'URL_Photo' => 'photoUrl',
89
        ];
90
91
        foreach ($parametersMap as $key => $value) {
92
            $this->$value = ($parameters->$key ?? null);
93
        }
94
95
        $this->openings = $this->parseOpenings(
96
            $parameters->Horaires_Lundi->string,
97
            $parameters->Horaires_Mardi->string,
98
            $parameters->Horaires_Mercredi->string,
99
            $parameters->Horaires_Jeudi->string,
100
            $parameters->Horaires_Vendredi->string,
101
            $parameters->Horaires_Samedi->string,
102
            $parameters->Horaires_Dimanche->string
103
        );
104
    }
105
106
    /**
107
     * @param array $monday
108
     * @param array $tuesday
109
     * @param array $wednesday
110
     * @param array $thursday
111
     * @param array $friday
112
     * @param array $saturday
113
     * @param array $sunday
114
     * @return array
115
     */
116
    private function parseOpenings(
117
        array $monday,
118
        array $tuesday,
119
        array $wednesday,
120
        array $thursday,
121
        array $friday,
122
        array $saturday,
123
        array $sunday
124
    ) {
125
        $weekDays = [
126
            'monday' => $monday,
127
            'tuesday' => $tuesday,
128
            'wednesday' => $wednesday,
129
            'thursday' => $thursday,
130
            'friday' => $friday,
131
            'saturday' => $saturday,
132
            'sunday' => $sunday,
133
        ];
134
135
        foreach ($weekDays as $dayKey => $hours) {
136
            $day = [];
137
            $hoursCount = count($hours);
138
            for ($i = 0; $i < $hoursCount; $i+=2) {
139
                if (!empty($hours[$i]) && !empty($hours[$i + 1])) {
140
                    $day[] = implode(
141
                        '-',
142
                        [
143
                            implode(':', str_split($hours[$i], 2)),
0 ignored issues
show
Bug introduced by
It seems like str_split($hours[$i], 2) can also be of type true; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

143
                            implode(':', /** @scrutinizer ignore-type */ str_split($hours[$i], 2)),
Loading history...
144
                            implode(':', str_split($hours[$i + 1], 2))
145
                        ]
146
                    );
147
                }
148
            }
149
            $weekDays[$dayKey] = $day;
150
        }
151
152
        return $weekDays;
153
    }
154
}
155