DeliveryChoice::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 1
c 2
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
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