DeliveryChoice::findPickupPoints()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 27
c 2
b 0
f 1
nc 3
nop 12
dl 0
loc 52
rs 9.488

How to fix   Long Method    Many Parameters   

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:

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
namespace DansMaCulotte\MondialRelay;
4
5
use DansMaCulotte\MondialRelay\Exceptions\Exception;
6
use DansMaCulotte\MondialRelay\Resources\PickupPoint;
7
8
/**
9
 * Implementation of Delivery Choice Web Service
10
 * https://www.mondialrelay.fr/media/108937/Solution-Web-Service-V5.6.pdf
11
 */
12
class DeliveryChoice extends MondialRelay
13
{
14
    /** @var string */
15
    const SERVICE_URL = 'https://api.mondialrelay.com/Web_Services.asmx?WSDL';
16
17
    /**
18
     * DeliveryChoice constructor.
19
     * @param array $credentials   Contains site id and site key
20
     * @param array $options       Additional parameters to submit to the web services
21
     * @throws \Exception
22
     */
23
    public function __construct(array $credentials, array $options = [])
24
    {
25
        parent::__construct($credentials, self::SERVICE_URL, $options);
26
    }
27
28
29
    /**
30
     * @param string $country ISO 3166 country code
31
     * @param string|null $city City name
32
     * @param string|null $zipCode Zip code
33
     * @param string|null $latitude Latitude
34
     * @param string|null $longitude Longitude
35
     * @param string|null $code Pickup point code
36
     * @param string|null $weight Weight in grams
37
     * @param string|null $collectType Delivery or collect type
38
     * @param string|null $sendingDelay Delay before sending
39
     * @param int|null $searchRadius Radius from the origin point
40
     * @param string|null $activityType
41
     * @param int $nbResults Number of results
42
     * @return PickupPoint[]
43
     * @throws Exception
44
     */
45
    public function findPickupPoints(
46
        string $country,
47
        string $city = null,
48
        string $zipCode = null,
49
        string $latitude = null,
50
        string $longitude = null,
51
        string $code = null,
52
        string $weight = null,
53
        string $collectType = null,
54
        string $sendingDelay = null,
55
        int $searchRadius = 0,
56
        string $activityType = null,
57
        int $nbResults = 10
58
    ): array {
59
        $options = [
60
            'Pays' => $country,
61
            'Ville' => $city,
62
            'CP' => $zipCode,
63
            'Latitude' => $latitude,
64
            'Longitude' => $longitude,
65
            'NumPointRelais' => $code,
66
            'Poids' => $weight,
67
            'Action' => $collectType,
68
            'DelaiEnvoie' => $sendingDelay,
69
            'RayonRecherche' => $searchRadius,
70
            'TypeActivite' => $activityType,
71
            'NombreResultats' => $nbResults,
72
        ];
73
74
        $result = $this->soapExec(
75
            'WSI4_PointRelais_Recherche',
76
            $options
77
        );
78
79
        $errorCode = $result->WSI4_PointRelais_RechercheResult->STAT;
80
        if ($errorCode !== '0') {
81
            throw Exception::requestError($errorCode);
82
        }
83
84
        $result = $result->WSI4_PointRelais_RechercheResult->PointsRelais->PointRelais_Details;
85
86
        if (is_array($result) === false) {
87
            return [
88
                new PickupPoint($result)
89
            ];
90
        }
91
92
        return array_map(
93
            function ($pickupPoint) {
94
                return new PickupPoint($pickupPoint);
95
            },
96
            $result
97
        );
98
    }
99
100
    /**
101
     * @param string $country
102
     * @param string $code
103
     * @return PickupPoint
104
     * @throws Exception
105
     */
106
    public function findPickupPointByCode(string $country, string $code): PickupPoint
107
    {
108
        $points = $this->findPickupPoints($country, null, null, null, null, $code);
109
110
        if (count($points) === 0) {
111
            throw Exception::noPickupPoint($code);
112
        }
113
114
        return $points[0];
115
    }
116
}
117