Passed
Push — master ( 43ad44...c27638 )
by Radosław
02:45
created

Client::__call()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.1502

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 11
cp 0.8182
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 2
crap 5.1502
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use stdClass;
6
use SoapClient;
7
use Radowoj\Yaah\Constants\Sysvars;
8
9
class Client
10
{
11
    protected $config = null;
12
13
    protected $soapClient = null;
14
15
    protected $allegroSessionHandle = null;
16
17
    protected $allegroUserId = null;
18
19
    protected $localVersionKey = null;
20
21 7
    public function __construct(Config $config, SoapClient $soapClient)
22
    {
23 7
        $this->config = $config;
24 7
        $this->soapClient = $soapClient;
25 7
    }
26
27
28 5
    protected function getLocalVersionKey()
29
    {
30 5
        if (is_null($this->localVersionKey)) {
31 5
            $response = $this->soapClient->doQuerySysStatus([
32 5
                'sysvar' => Sysvars::SYSVAR_CATEGORY_TREE,
33 5
                'countryId' => $this->config->getCountryCode(),
34 5
                'webapiKey' => $this->config->getApiKey(),
35 5
            ]);
36
37 5
            if (!isset($response->verKey)) {
38 1
                throw new Exception("Invalid WebAPI doQuerySysStatus() response: " . print_r($response, 1));
39
            }
40
41 4
            $this->localVersionKey = $response->verKey;
42 4
        }
43
44 4
        return $this->localVersionKey;
45
    }
46
47 5
    protected function getWebApiRequest($data)
48 1
    {
49 5
        return array_merge($data, [
50 5
            'webapiKey'     => $this->config->getApiKey(),
51 5
            'localVersion'  => $this->getLocalVersionKey(),
52
53
            //for some methods...
54 4
            'countryId'     => $this->config->getCountryCode(),
55 4
            'sessionId'     => $this->allegroSessionHandle,
56
57
            //...for some other methods
58 4
            'countryCode'   => $this->config->getCountryCode(),
59 4
            'sessionHandle' => $this->allegroSessionHandle,
60 4
        ]);
61
    }
62
63 5
    public function login()
64
    {
65
        $data = [
66 5
            'userLogin' => $this->config->getLogin()
67 5
        ];
68
69 5
        if ($this->config->getPasswordHash()) {
70 4
            $data['userHashPassword'] = $this->config->getPasswordHash();
71 4
            $response = $this->soapClient->doLoginEnc($this->getWebApiRequest($data));
72 3
        } else {
73
            //ugly non-encrypted way, but sometimes sandbox fails to accept correct hashed login data
74 1
            $data['userPassword'] = $this->config->getPasswordPlain();
75 1
            $response = $this->soapClient->doLogin($this->getWebApiRequest($data));
76
        }
77
78
        //validate basic response properties
79 4
        if (!is_object($response) || !isset($response->sessionHandlePart) || !isset($response->userId)) {
80 2
            throw new Exception("Invalid WebAPI doLogin[Enc]() response: " . print_r($response, 1));
81
        }
82
83 2
        $this->allegroSessionHandle = $response->sessionHandlePart;
84 2
        $this->allegroUserId = $response->userId;
85 2
    }
86
87
88 2
    public function __call($name, $args)
89
    {
90 2
        if (!$this->allegroUserId || !$this->allegroSessionHandle) {
91 2
            $this->login();
92 2
        }
93
94
        //prefix with WebAPI "do" prefix
95 2
        $name = 'do' . ucfirst($name);
96
97 2
        $request = $this->getWebApiRequest(
98 2
            array_key_exists(0, $args) ? $args[0] : []
99 2
        );
100
101
        try {
102 2
            return $this->soapClient->{$name}($request);
103
        } catch (\Exception $e) {
104
            throw new Exception('WebAPI exception: ' . $e->getMessage() . "; trying to call: {$name}()");
105
        }
106
    }
107
}
108