DansMaCulotte /
colissimo-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DansMaCulotte\Colissimo; |
||
| 4 | |||
| 5 | use DansMaCulotte\Colissimo\Exceptions\Exception; |
||
| 6 | use GuzzleHttp\Client as HttpClient; |
||
| 7 | use GuzzleHttp\Exception\GuzzleException; |
||
| 8 | |||
| 9 | class Colissimo |
||
| 10 | { |
||
| 11 | const STATUS_URL = 'https://ws.colissimo.fr/supervision-wspudo/supervision.jsp'; |
||
| 12 | |||
| 13 | public $httpClient; |
||
| 14 | protected $credentials; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Construct method to build soap client and options |
||
| 18 | * |
||
| 19 | * @param array $credentials Contains login, password and/or apiKey items |
||
| 20 | * @param string $url Url to use for SOAP client |
||
| 21 | * @param array $options Guzzle Client options |
||
| 22 | */ |
||
| 23 | public function __construct(array $credentials = [], $url = null, array $options = []) |
||
| 24 | { |
||
| 25 | if (isset($credentials['accountNumber']) && isset($credentials['password'])) { |
||
| 26 | $this->credentials = [ |
||
| 27 | 'accountNumber' => $credentials['accountNumber'], |
||
| 28 | 'password' => $credentials['password'], |
||
| 29 | ]; |
||
| 30 | } |
||
| 31 | |||
| 32 | if (isset($credentials['apikey'])) { |
||
| 33 | $this->credentials['apikey'] = $credentials['apikey']; |
||
| 34 | } |
||
| 35 | |||
| 36 | if (isset($credentials['codTiersPourPartenaire'])) { |
||
| 37 | $this->credentials['codTiersPourPartenaire'] = $credentials['codTiersPourPartenaire']; |
||
| 38 | } |
||
| 39 | |||
| 40 | $this->httpClient = new HttpClient(array_merge([ |
||
| 41 | 'base_uri' => $url, |
||
| 42 | ], $options)); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Check Web Services Endpoint and verify if response contains OK string |
||
| 47 | * |
||
| 48 | * @return bool |
||
| 49 | * @throws Exception |
||
| 50 | */ |
||
| 51 | public function checkWebServiceStatus() |
||
| 52 | { |
||
| 53 | try { |
||
| 54 | $response = $this->httpClient->request('GET', self::STATUS_URL); |
||
| 55 | |||
| 56 | $isOk = preg_match_all('/OK/m', (string) $response->getBody()); |
||
| 57 | if ($isOk == false) { |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 58 | throw Exception::serviceUnavailable(); |
||
| 59 | } |
||
| 60 | } catch (GuzzleException $e) { |
||
| 61 | throw Exception::serviceUnavailable(); |
||
| 62 | } |
||
| 63 | |||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Proxy method to automatically inject credentials |
||
| 69 | * |
||
| 70 | * @param string $method |
||
| 71 | * @param array $params |
||
| 72 | * |
||
| 73 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
| 74 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 75 | */ |
||
| 76 | public function httpRequest(string $method, array $params) |
||
| 77 | { |
||
| 78 | return $this->httpClient->request('GET', $method, [ |
||
| 79 | 'query' => array_merge( |
||
| 80 | $this->credentials, |
||
| 81 | $params |
||
| 82 | ) |
||
| 83 | ]); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Parse service errors and throw if code match |
||
| 88 | * |
||
| 89 | * @param int $code |
||
| 90 | * @param array $errors |
||
| 91 | * @throws Exception |
||
| 92 | */ |
||
| 93 | protected function parseErrorCodeAndThrow(int $code, array $errors) |
||
| 94 | { |
||
| 95 | $message = null; |
||
| 96 | |||
| 97 | if (isset($errors[$code])) { |
||
| 98 | $message = $errors[$code]; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($message) { |
||
| 102 | throw Exception::requestError($message); |
||
| 103 | } |
||
| 104 | |||
| 105 | return ; |
||
| 106 | } |
||
| 107 | } |
||
| 108 |