Completed
Push — master ( c73332...997785 )
by Jhao
04:01 queued 01:33
created

SingleAccessClient::callSoapMethod()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 9.9332
1
<?php
2
3
/**
4
 * This file is part of RussianPost SDK package.
5
 *
6
 * © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Appwilio\RussianPostSDK\Tracking;
15
16
use Psr\Log\NullLogger;
17
use Psr\Log\LoggerAwareTrait;
18
use Psr\Log\LoggerAwareInterface;
19
use Appwilio\RussianPostSDK\Tracking\Single\TrackingResponse;
20
use Appwilio\RussianPostSDK\Tracking\Single\CashOnDeliveryResponse;
21
use Appwilio\RussianPostSDK\Tracking\Single\CashOnDeliveryEventsInput;
22
use Appwilio\RussianPostSDK\Tracking\Exceptions\SingleAccessException;
23
24
class SingleAccessClient implements LoggerAwareInterface
25
{
26
    use LoggerAwareTrait;
27
28
    public const LANG_ENG = 'ENG';
29
    public const LANG_RUS = 'RUS';
30
31
    public const HISTORY_OPERATIONS    = 0;
32
    public const HISTORY_NOTIFICATIONS = 1;
33
34
    protected const LINK_URL = 'https://www.pochta.ru/tracking';
35
    protected const WSDL_URL = 'https://tracking.pochta.ru/tracking-web-static/rtm34_wsdl.xml';
36
37
    protected const XML_NS_DATA     = 'http://russianpost.org/operationhistory/data';
38
    protected const XML_NS_COD_DATA = 'http://www.russianpost.org/RTM/DataExchangeESPP/Data';
39
40
    /** @var \SoapClient */
41
    protected $client;
42
43
    /** @var string */
44
    private $login;
45
46
    /** @var string */
47
    private $password;
48
49
    private $options = [
50
        'soap_version' => SOAP_1_2,
51
        'trace'        => 1,
52
        'classmap'     => [
53
            'Rtm02Parameter'                   => Single\Parameter::class,
54
55
            'getOperationHistoryResponse'      => Single\TrackingResponse::class,
56
            'OperationHistoryRecord'           => Single\TrackingOperation::class,
57
            'Address'                          => Single\TrackingOperationAddress::class,
58
            'Country'                          => Single\TrackingOperationCountry::class,
59
            'OperationHistoryData'             => Single\TrackingOperationsWrapper::class,
60
            'OperationParameters'              => Single\TrackingOperationParameters::class,
61
            'UserParameters'                   => Single\TrackingOperationUserParameters::class,
62
            'ItemParameters'                   => Single\TrackingOperationItemParameters::class,
63
            'AddressParameters'                => Single\TrackingOperationAddressParameters::class,
64
            'FinanceParameters'                => Single\TrackingOperationFinanceParameters::class,
65
66
            'PostalOrderEvent'                 => Single\CashOnDeliveryEvent::class,
67
            'PostalOrderEventsForMailResponse' => Single\CashOnDeliveryResponse::class,
68
            'PostalOrderEventsForMailInput'    => Single\CashOnDeliveryEventsInput::class,
69
            // Mai – не опечатка
70
            'PostalOrderEventsForMaiOutput'    => Single\CashOnDeliveryEventsWrapper::class,
71
        ],
72
    ];
73
74 6
    public function __construct(string $login, string $password)
75
    {
76 6
        $this->logger = new NullLogger();
77
78 6
        $this->login = $login;
79 6
        $this->password = $password;
80 6
    }
81
82 1
    public function getTrackingUrl(string $number): string
83
    {
84 1
        return self::LINK_URL."#{$number}";
85
    }
86
87 3
    public function getTrackingEvents(
88
        string $track,
89
        string $language = self::LANG_RUS,
90
        int $type = self::HISTORY_OPERATIONS
91
    ): TrackingResponse {
92 3
        return $this->callSoapMethod(
93 3
            'getOperationHistory',
94 3
            $this->assembleTrackingRequestArguments($track, $language, $type)
95
        );
96
    }
97
98 1
    public function getCashOnDeliveryEvents(
99
        string $track,
100
        string $language = self::LANG_RUS
101
    ): CashOnDeliveryResponse {
102 1
        return $this->callSoapMethod(
103 1
            'PostalOrderEventsForMail',
104 1
            $this->assembleCashOnDeliveryRequestArguments($track, $language)
105
        );
106
    }
107
108 4
    protected function getClient(): \SoapClient
109
    {
110 4
        if (! $this->client) {
111
            $this->client = new \SoapClient(self::WSDL_URL, $this->options);
112
        }
113
114 4
        return $this->client;
115
    }
116
117 4
    private function callSoapMethod(string $method, \SoapVar $arguments)
118
    {
119
        try {
120 4
            return $this->getClient()->__soapCall($method, [$arguments]);
121 2
        } catch (\SoapFault $e) {
122 2
            if (\property_exists($e, 'detail')) {
123 1
                $detail = \get_object_vars($e->{'detail'});
124
125 1
                throw new SingleAccessException(\key($detail).': '.\current($detail), $e->getCode(), $e);
126
            }
127
128 1
            throw new SingleAccessException($e->getMessage(), $e->getCode(), $e);
129
        } finally {
130 4
            $this->logger->info("pochta.ru Single Tracking request: {$this->getClient()->__getLastRequest()}");
131 4
            $this->logger->info("pochta.ru Single Tracking response: {$this->getClient()->__getLastResponse()}");
132
        }
133
    }
134
135 3
    private function assembleTrackingRequestArguments(string $track, string $language, int $type): \SoapVar
136
    {
137 3
        return new \SoapVar([
138 3
            new \SoapVar([
139 3
                new \SoapVar($track, \XSD_STRING, '', '', 'Barcode', self::XML_NS_DATA),
140 3
                new \SoapVar($type, \XSD_STRING, '', '', 'MessageType', self::XML_NS_DATA),
141 3
                new \SoapVar($language, \XSD_STRING, '', '', 'Language', self::XML_NS_DATA),
142 3
            ], \SOAP_ENC_OBJECT, '', '', 'OperationHistoryRequest', self::XML_NS_DATA),
143 3
            $this->assembleAuthorizationHeader(),
144 3
        ], \SOAP_ENC_OBJECT);
145
    }
146
147 1
    private function assembleCashOnDeliveryRequestArguments(string $code, string $language): \SoapVar
148
    {
149 1
        $input = new CashOnDeliveryEventsInput($code, $language);
150
151 1
        return new \SoapVar([
152 1
            new \SoapVar($input, \SOAP_ENC_OBJECT, '', '', 'PostalOrderEventsForMailInput', self::XML_NS_COD_DATA),
153 1
            $this->assembleAuthorizationHeader(),
154 1
        ], \SOAP_ENC_OBJECT);
155
    }
156
157 4
    private function assembleAuthorizationHeader(): \SoapVar
158
    {
159 4
        return new \SoapVar([
160 4
            new \SoapVar($this->login, \XSD_STRING, '', '', 'login', self::XML_NS_DATA),
161 4
            new \SoapVar($this->password, \XSD_STRING, '', '', 'password', self::XML_NS_DATA),
162 4
        ], \SOAP_ENC_OBJECT, '', '', 'AuthorizationHeader', self::XML_NS_DATA);
163
    }
164
}
165