Completed
Push — master ( 83cd0b...5cd9dd )
by Gaël
26:21 queued 11:17
created

Colissimo::checkWebServiceStatus()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 14
rs 10
c 1
b 0
f 0
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
     */
22
    public function __construct($credentials = [], $url = null)
23
    {
24
        if (isset($credentials['accountNumber']) && isset($credentials['password'])) {
25
            $this->credentials = [
26
                'accountNumber' =>  $credentials['accountNumber'],
27
                'password' => $credentials['password'],
28
            ];
29
        }
30
31
        if (isset($credentials['apikey'])) {
32
            $this->credentials['apikey'] = $credentials['apikey'];
33
        }
34
35
        if (isset($credentials['codTiersPourPartenaire'])) {
36
            $this->credentials['codTiersPourPartenaire'] = $credentials['codTiersPourPartenaire'];
37
        }
38
39
        $this->httpClient = new HttpClient([
40
            'base_uri' => $url,
41
        ]);
42
    }
43
44
    /**
45
     * Check Web Services Endpoint and verify if response contains OK string
46
     *
47
     * @return bool
48
     * @throws Exception
49
     */
50
    public function checkWebServiceStatus()
51
    {
52
        try {
53
            $response = $this->httpClient->request('GET', self::STATUS_URL);
54
55
            $isOk = preg_match_all('/OK/m', (string) $response->getBody());
56
            if ($isOk == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $isOk of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
57
                throw Exception::serviceUnavailable();
58
            }
59
        } catch (GuzzleException $e) {
60
            throw Exception::serviceUnavailable();
61
        }
62
63
        return true;
64
    }
65
    
66
    /**
67
     * Proxy method to automatically inject credentials
68
     *
69
     * @param string $method
70
     * @param array $params
71
     *
72
     * @return mixed|\Psr\Http\Message\ResponseInterface
73
     * @throws \GuzzleHttp\Exception\GuzzleException
74
     */
75
    public function httpRequest(string $method, array $params)
76
    {
77
        return $this->httpClient->request('GET', $method, [
78
            'query' => array_merge(
79
                $this->credentials,
80
                $params
81
            )
82
        ]);
83
    }
84
85
    /**
86
     * Parse service errors and throw if code match
87
     *
88
     * @param int $code
89
     * @param array $errors
90
     *
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