DansMaCulotte /
colissimo-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DansMaCulotte\Colissimo; |
||
| 4 | |||
| 5 | use DansMaCulotte\Colissimo\Exceptions\Exception; |
||
| 6 | use DansMaCulotte\Colissimo\Resources\PickupPoint; |
||
| 7 | use SimpleXMLElement; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Implementation of Delivery Choice Web Service |
||
| 11 | * https://www.colissimo.entreprise.laposte.fr/system/files/imagescontent/docs/spec_ws_livraison.pdf |
||
| 12 | */ |
||
| 13 | class DeliveryChoice extends Colissimo |
||
| 14 | { |
||
| 15 | /** @var string */ |
||
| 16 | const SERVICE_URL = 'https://ws.colissimo.fr/pointretrait-ws-cxf/PointRetraitServiceWS/2.0/'; |
||
| 17 | |||
| 18 | /** @var array */ |
||
| 19 | const ERRORS = [ |
||
| 20 | 101 => 'Missing account number', |
||
| 21 | 102 => 'Missing password', |
||
| 22 | 104 => 'Missing postal code', |
||
| 23 | 105 => 'Missing city', |
||
| 24 | 106 => 'Missing estimated shipping date', |
||
| 25 | 107 => 'Missing pickup point ID', |
||
| 26 | 117 => 'Missing country code', |
||
| 27 | 120 => 'Weight value is not an integer', |
||
| 28 | 121 => 'Weight value is not between 1 and 99999', |
||
| 29 | 122 => 'Date format does not match DD/MM/YYYY', |
||
| 30 | 123 => 'Relay filter is not a bool', |
||
| 31 | 124 => 'Invalid pickup point ID', |
||
| 32 | 125 => 'Invalid postal code (not between 01XXX, 95XXX or 980XX)', |
||
| 33 | 127 => 'Invalid RequestId', |
||
| 34 | 129 => 'Invalid address', |
||
| 35 | 143 => 'Postal code does not match XXXX', |
||
| 36 | 201 => 'Invalid account number or password', |
||
| 37 | 144 => 'Invalid postal code', |
||
| 38 | 145 => 'Missing postal code', |
||
| 39 | 146 => 'Country not valid for Colissimo Europe', |
||
| 40 | 202 => 'Request unauthorized for this account', |
||
| 41 | 203 => 'International option not available for this country', |
||
| 42 | 300 => 'No pickup points found with rules applied', |
||
| 43 | 301 => 'No pickup points found', |
||
| 44 | 1000 => 'Internal server error', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Construct Method |
||
| 49 | * |
||
| 50 | * @param array $credentials Contains accountNumber and password for authentication |
||
| 51 | * @param array $options Guzzle Client options |
||
| 52 | */ |
||
| 53 | public function __construct(array $credentials, array $options = []) |
||
| 54 | { |
||
| 55 | parent::__construct($credentials, self::SERVICE_URL, $options); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Retrieve available pickup points by selectors |
||
| 60 | * |
||
| 61 | * @param string $city City name |
||
| 62 | * @param string $zipCode Zip Code |
||
| 63 | * @param string $countryCode ISO 3166 country code |
||
| 64 | * @param string $shippingDate Shipping date (DD/MM/YYYY) |
||
| 65 | * @param array $options Additional parameters |
||
| 66 | * |
||
| 67 | * @return PickupPoint[] |
||
| 68 | * @throws \Exception |
||
| 69 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 70 | */ |
||
| 71 | public function findPickupPoints( |
||
| 72 | string $city, |
||
| 73 | string $zipCode, |
||
| 74 | string $countryCode, |
||
| 75 | string $shippingDate, |
||
| 76 | array $options = [] |
||
| 77 | ) { |
||
| 78 | $params = array_merge( |
||
| 79 | [ |
||
| 80 | 'city' => $city, |
||
| 81 | 'zipCode' => $zipCode, |
||
| 82 | 'countryCode' => $countryCode, |
||
| 83 | 'shippingDate' => $shippingDate, |
||
| 84 | ], |
||
| 85 | $options |
||
| 86 | ); |
||
| 87 | |||
| 88 | $response = $this->httpRequest( |
||
| 89 | 'findRDVPointRetraitAcheminement', |
||
| 90 | $params |
||
| 91 | ); |
||
| 92 | |||
| 93 | $xml = new SimpleXMLElement((string) $response->getBody()); |
||
| 94 | |||
| 95 | $return = $xml->xpath('//return'); |
||
| 96 | if (count($return) && $return[0]->errorCode != 0) { |
||
| 97 | $this->parseErrorCodeAndThrow((int) $return[0]->errorCode, self::ERRORS); |
||
| 98 | } |
||
| 99 | |||
| 100 | $pickupPoints = []; |
||
| 101 | foreach ($xml->xpath('//listePointRetraitAcheminement') as $pickupPoint) { |
||
| 102 | $pickupPoints[] = new PickupPoint( |
||
| 103 | $pickupPoint->accesPersonneMobiliteReduite, |
||
| 104 | $pickupPoint->adresse1, |
||
| 105 | $pickupPoint->adresse2, |
||
| 106 | $pickupPoint->adresse3, |
||
| 107 | $pickupPoint->codePostal, |
||
| 108 | $pickupPoint->congesPartiel, |
||
| 109 | $pickupPoint->congesTotal, |
||
| 110 | $pickupPoint->coordGeolocalisationLatitude, |
||
| 111 | $pickupPoint->coordGeolocalisationLongitude, |
||
| 112 | $pickupPoint->distanceEnMetre, |
||
| 113 | $pickupPoint->horairesOuvertureLundi, |
||
| 114 | $pickupPoint->horairesOuvertureMardi, |
||
| 115 | $pickupPoint->horairesOuvertureMercredi, |
||
| 116 | $pickupPoint->horairesOuvertureJeudi, |
||
| 117 | $pickupPoint->horairesOuvertureVendredi, |
||
| 118 | $pickupPoint->horairesOuvertureSamedi, |
||
| 119 | $pickupPoint->horairesOuvertureDimanche, |
||
| 120 | $pickupPoint->identifiant, |
||
| 121 | $pickupPoint->indiceDeLocalisation, |
||
| 122 | $pickupPoint->listeConges, |
||
| 123 | $pickupPoint->localite, |
||
| 124 | $pickupPoint->nom, |
||
| 125 | $pickupPoint->periodeActiviteHoraireDeb, |
||
| 126 | $pickupPoint->periodeActiviteHoraireFin, |
||
| 127 | $pickupPoint->poidsMaxi, |
||
| 128 | $pickupPoint->typeDePoint, |
||
| 129 | $pickupPoint->codePays, |
||
| 130 | $pickupPoint->langue, |
||
| 131 | $pickupPoint->libellePays, |
||
| 132 | $pickupPoint->loanOfHandlingTool, |
||
| 133 | $pickupPoint->parking, |
||
| 134 | $pickupPoint->reseau, |
||
| 135 | $pickupPoint->distributionSort, |
||
| 136 | $pickupPoint->lotAcheminement, |
||
| 137 | $pickupPoint->versionPlanTri |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $pickupPoints; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Retreive pickup point by ID |
||
| 146 | * |
||
| 147 | * @param int $id Pickup point ID |
||
| 148 | * @param string $shippingDate Shipping date (DD/MM/YYYY) |
||
| 149 | * @param array $options Additional parameters |
||
| 150 | * |
||
| 151 | * @return PickupPoint |
||
| 152 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 153 | * @throws Exception |
||
| 154 | */ |
||
| 155 | public function findPickupPointByID(int $id, string $shippingDate, array $options = []) |
||
| 156 | { |
||
| 157 | $options = array_merge( |
||
| 158 | [ |
||
| 159 | 'id' => $id, |
||
| 160 | 'date' => $shippingDate, |
||
| 161 | ], |
||
| 162 | $options |
||
| 163 | ); |
||
| 164 | |||
| 165 | $response = $this->httpRequest( |
||
| 166 | 'findPointRetraitAcheminementByID', |
||
| 167 | $options |
||
| 168 | ); |
||
| 169 | |||
| 170 | $xml = new SimpleXMLElement((string) $response->getBody()); |
||
| 171 | |||
| 172 | $return = $xml->xpath('//return'); |
||
| 173 | if (count($return) && $return[0]->errorCode != 0) { |
||
| 174 | $this->parseErrorCodeAndThrow((int) $return[0]->errorCode, self::ERRORS); |
||
| 175 | } |
||
| 176 | |||
| 177 | $rawPickupPoint = $xml->xpath('//pointRetraitAcheminement'); |
||
| 178 | if (count($rawPickupPoint) && $rawPickupPoint[0]) { |
||
| 179 | $pickupPoint = new PickupPoint( |
||
| 180 | $rawPickupPoint[0]->accesPersonneMobiliteReduite, |
||
| 181 | $rawPickupPoint[0]->adresse1, |
||
| 182 | $rawPickupPoint[0]->adresse2, |
||
| 183 | $rawPickupPoint[0]->adresse3, |
||
| 184 | $rawPickupPoint[0]->codePostal, |
||
| 185 | $rawPickupPoint[0]->congesPartiel, |
||
| 186 | $rawPickupPoint[0]->congesTotal, |
||
| 187 | $rawPickupPoint[0]->coordGeolocalisationLatitude, |
||
| 188 | $rawPickupPoint[0]->coordGeolocalisationLongitude, |
||
| 189 | $rawPickupPoint[0]->distanceEnMetre, |
||
| 190 | $rawPickupPoint[0]->horairesOuvertureLundi, |
||
| 191 | $rawPickupPoint[0]->horairesOuvertureMardi, |
||
| 192 | $rawPickupPoint[0]->horairesOuvertureMercredi, |
||
| 193 | $rawPickupPoint[0]->horairesOuvertureJeudi, |
||
| 194 | $rawPickupPoint[0]->horairesOuvertureVendredi, |
||
| 195 | $rawPickupPoint[0]->horairesOuvertureSamedi, |
||
| 196 | $rawPickupPoint[0]->horairesOuvertureDimanche, |
||
| 197 | $rawPickupPoint[0]->identifiant, |
||
| 198 | $rawPickupPoint[0]->indiceDeLocalisation, |
||
| 199 | $rawPickupPoint[0]->listeConges, |
||
| 200 | $rawPickupPoint[0]->localite, |
||
| 201 | $rawPickupPoint[0]->nom, |
||
| 202 | $rawPickupPoint[0]->periodeActiviteHoraireDeb, |
||
| 203 | $rawPickupPoint[0]->periodeActiviteHoraireFin, |
||
| 204 | $rawPickupPoint[0]->poidsMaxi, |
||
| 205 | $rawPickupPoint[0]->typeDePoint, |
||
| 206 | $rawPickupPoint[0]->codePays, |
||
| 207 | $rawPickupPoint[0]->langue, |
||
| 208 | $rawPickupPoint[0]->libellePays, |
||
| 209 | $rawPickupPoint[0]->loanOfHandlingTool, |
||
| 210 | $rawPickupPoint[0]->parking, |
||
| 211 | $rawPickupPoint[0]->reseau, |
||
| 212 | $rawPickupPoint[0]->distributionSort, |
||
| 213 | $rawPickupPoint[0]->lotAcheminement, |
||
| 214 | $rawPickupPoint[0]->versionPlanTri |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $pickupPoint; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 219 | } |
||
| 220 | } |
||
| 221 |