Passed
Push — master ( 480006...f881d2 )
by Gaël
12:44
created

DeliveryChoice::findPickupPointByCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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 array
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
    ) {
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
        $pickupPoints = array_map(
87
            function ($pickupPoint) {
88
                return new PickupPoint($pickupPoint);
89
            },
90
            $result
91
        );
92
93
        return $pickupPoints;
94
    }
95
96
    /**
97
     * @param string $country
98
     * @param string $code
99
     * @return array
100
     * @throws Exception
101
     */
102
    public function findPickupPointByCode(string $country, string $code)
103
    {
104
        $points = $this->findPickupPoints($country, null, null, null, null, $code);
105
106
        if (count($points) === 0) {
107
            throw Exception::noPickupPoint($code);
108
        }
109
110
        return $points[0];
111
    }
112
}
113