Passed
Push — master ( 00ab5d...b6c1c8 )
by Radosław
02:21
created

Client::getWebApiRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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