Completed
Push — master ( 87afc0...a49ade )
by Gaël
12s queued 11s
created

MondialRelay::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 19
rs 9.9332
1
<?php
2
3
namespace DansMaCulotte\MondialRelay;
4
5
use DansMaCulotte\MondialRelay\Exceptions\Exception;
6
use Zend\Soap\Client as SoapClient;
7
8
class MondialRelay
9
{
10
    /** @var */
11
    public $soapClient;
12
13
    /** @var array */
14
    public $soapOptions = [
15
        'soap_version' => SOAP_1_2,
16
    ];
17
18
    /** @var array */
19
    private $credentials = [];
20
21
22
    /**
23
     * Construct method to build soap client and options
24
     *
25
     * @param array $credentials Contains site_id and and site_key
26
     * @param string $url Url to use for SOAP client
27
     * @param array $options Options to use for SOAP client
28
     * @throws Exception
29
     */
30
    public function __construct(array $credentials, string $url = null, array $options = [])
31
    {
32
        if (isset($credentials['site_id']) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
33
            throw Exception::invalidCredentials("site id");
34
        }
35
36
        if (isset($credentials['site_key']) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
37
            throw Exception::invalidCredentials("site key");
38
        }
39
40
        $this->credentials = [
41
            'Enseigne' =>  $credentials['site_id'],
42
            'Key' =>  $credentials['site_key'],
43
        ];
44
45
46
        $this->soapClient = new SoapClient(
47
            $url,
48
            array_merge($this->soapOptions, $options)
49
        );
50
    }
51
52
53
    private function getSecurityKey(array $params)
54
    {
55
        $security = $this->credentials['Enseigne'];
56
        foreach ($params as $param) {
57
            $security.= $param;
58
        }
59
        $security.= $this->credentials['Key'];
60
61
        return strtoupper(md5($security));
62
    }
63
64
65
    /**
66
     * Proxy method to automaticaly inject credentials and options
67
     *
68
     * @param string $method Method to with SOAP web services
69
     * @param array $params Parameters to send with method
70
     *
71
     * @return Object
72
     * @throws Exception
73
     */
74
    public function soapExec(string $method, array $params)
75
    {
76
        $params['Security'] = $this->getSecurityKey($params);
77
        $params['Enseigne'] = $this->credentials['Enseigne'];
78
79
        $result = $this->soapClient->$method($params);
80
81
        return $result;
82
    }
83
}
84