Passed
Push — master ( ea2827...0aa822 )
by Radosław
13:02
created

Client::__call()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

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