Completed
Push — master ( a49ade...28e6df )
by Gaël
04:07
created

PickupPoint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 70
c 3
b 0
f 1
dl 0
loc 150
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseOpenings() 0 39 5
A __construct() 0 35 3
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 = (isset($parameters->$key) ? $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
            'jeudi' => $thursday,
130
            'vendredi' => $friday,
131
            'samedi' => $saturday,
132
            'dimanche' => $sunday,
133
        ];
134
135
        foreach ($weekDays as $dayKey => $hours) {
136
            $day = [];
137
            for ($i = 0; $i < count($hours); $i+=2) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
138
                if (!empty($hours[$i]) && !empty($hours[$i + 1])) {
139
                    array_push(
140
                        $day,
141
                        implode(
142
                            '-',
143
                            [
144
                                implode(':', str_split($hours[$i], 2)),
145
                                implode(':', str_split($hours[$i + 1], 2))
146
                            ]
147
                        )
148
                    );
149
                }
150
            }
151
            $weekDays[$dayKey] = $day;
152
        }
153
154
        return $weekDays;
155
    }
156
}
157