Passed
Push — master ( 480a58...1d93b8 )
by Jhao
02:09
created

SingleAccessClient::callSoapMethod()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 1
b 0
f 0
ccs 7
cts 8
cp 0.875
cc 3
nc 6
nop 2
crap 3.0175
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;
0 ignored issues
show
Bug introduced by
The type Psr\Log\NullLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Psr\Log\LoggerAwareTrait;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerAwareTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Psr\Log\LoggerAwareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerAwareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
use Appwilio\RussianPostSDK\Tracking\Single\PostalOrderEventsForMailInput;
0 ignored issues
show
Bug introduced by
The type Appwilio\RussianPostSDK\...OrderEventsForMailInput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class SingleAccessClient implements LoggerAwareInterface
26
{
27
    use LoggerAwareTrait;
28
29
    public const LANG_ENG = 'ENG';
30
    public const LANG_RUS = 'RUS';
31
32
    public const HISTORY_OPERATIONS    = 0;
33
    public const HISTORY_NOTIFICATIONS = 1;
34
35
    protected const LINK_URL = 'https://www.pochta.ru/tracking';
36
    protected const WSDL_URL = 'https://tracking.pochta.ru/tracking-web-static/rtm34_wsdl.xml';
37
38
    protected const XML_NS_DATA        = 'http://russianpost.org/operationhistory/data';
39
    protected const XML_NS_COD_HISTORY = 'http://www.russianpost.org/RTM/DataExchangeESPP/Data';
40
41
    /** @var \SoapClient */
42
    protected $client;
43
44
    /** @var string */
45
    private $login;
46
47
    /** @var string */
48
    private $password;
49
50
    private $options = [
51
        'soap_version' => SOAP_1_2,
52
        'trace'        => 1,
53
        'classmap'     => [
54
            'Rtm02Parameter'                   => Single\Parameter::class,
55
56
            'getOperationHistoryResponse'      => Single\TrackingResponse::class,
57
            'OperationHistoryRecord'           => Single\TrackingOperation::class,
58
            'Address'                          => Single\TrackingOperationAddress::class,
59
            'Country'                          => Single\TrackingOperationCountry::class,
60
            'OperationHistoryData'             => Single\TrackingOperationsWrapper::class,
61
            'OperationParameters'              => Single\TrackingOperationParameters::class,
62
            'UserParameters'                   => Single\TrackingOperationUserParameters::class,
63
            'ItemParameters'                   => Single\TrackingOperationItemParameters::class,
64
            'AddressParameters'                => Single\TrackingOperationAddressParameters::class,
65 4
            'FinanceParameters'                => Single\TrackingOperationFinanceParameters::class,
66
67 4
            'PostalOrderEvent'                 => Single\CashOnDeliveryEvent::class,
68 4
            'PostalOrderEventsForMailResponse' => Single\CashOnDeliveryResponse::class,
69
            'PostalOrderEventsForMailInput'    => Single\CashOnDeliveryEventsInput::class,
70 1
            // Mai – не опечатка
71
            'PostalOrderEventsForMaiOutput'    => Single\CashOnDeliveryEventsWrapper::class,
72 1
        ],
73
    ];
74
75 1
    public function __construct(string $login, string $password)
76
    {
77
        $this->logger = new NullLogger();
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
79
        $this->login = $login;
80 1
        $this->password = $password;
81
    }
82
83 1
    public function getTrackingUrl(string $number): string
84
    {
85
        return self::LINK_URL."#{$number}";
86
    }
87
88
    public function getTrackingEvents(
89
        string $track,
90
        string $language = self::LANG_RUS,
91
        int $type = self::HISTORY_OPERATIONS
92
    ): TrackingResponse {
93
        return $this->callSoapMethod(
94
            'getOperationHistory',
95 1
            $this->assembleTrackingRequestArguments($track, $language, $type)
96
        );
97 1
    }
98
99 1
    public function getCashOnDeliveryEvents(
100
        string $track,
101
        string $language = self::LANG_RUS
102 1
    ): CashOnDeliveryResponse {
103
        return $this->callSoapMethod(
104 1
            'PostalOrderEventsForMail',
105 1
            $this->assembleCashOnDeliveryRequestArguments($track, $language)
106 1
        );
107 1
    }
108 1
109 1
    protected function getClient(): \SoapClient
110 1
    {
111 1
        if (! $this->client) {
112
            $this->client = new \SoapClient(self::WSDL_URL, $this->options);
113
        }
114 1
115
        return $this->client;
116 1
    }
117
118 1
    private function callSoapMethod(string $method, \SoapVar $arguments)
119 1
    {
120 1
        try {
121 1
            return $this->getClient()->__soapCall($method, [$arguments]);
122
        } catch (\SoapFault $e) {
123
            if (\property_exists($e, 'detail')) {
124 2
                $detail = \get_object_vars($e->{'detail'});
125
126 2
                throw new SingleAccessException(\key($detail).': '.\current($detail), $e->getCode(), $e);
127
            }
128
129
            throw new SingleAccessException($e->getMessage(), $e->getCode(), $e);
130 2
        } finally {
131
            $this->logger->info("pochta.ru Single Tracking request: {$this->getClient()->__getLastRequest()}");
132
            $this->logger->info("pochta.ru Single Tracking response: {$this->getClient()->__getLastResponse()}");
133
        }
134
    }
135
136
    private function assembleTrackingRequestArguments(string $track, string $language, int $type): \SoapVar
137
    {
138
        return new \SoapVar([
139
            new \SoapVar([
140
                new \SoapVar($track, \XSD_STRING, '', '', 'Barcode', self::XML_NS_DATA),
141
                new \SoapVar($type, \XSD_STRING, '', '', 'MessageType', self::XML_NS_DATA),
142
                new \SoapVar($language, \XSD_STRING, '', '', 'Language', self::XML_NS_DATA),
143
            ], \SOAP_ENC_OBJECT, '', '', 'OperationHistoryRequest', self::XML_NS_DATA),
144
            new \SoapVar([
145
                new \SoapVar($this->login, XSD_STRING, '', '', 'login', self::XML_NS_DATA),
146
                new \SoapVar($this->password, XSD_STRING, '', '', 'password', self::XML_NS_DATA),
147
            ], SOAP_ENC_OBJECT, '', '', 'AuthorizationHeader', self::XML_NS_DATA),
148
        ], \SOAP_ENC_OBJECT);
149
    }
150
151
    private function assembleCashOnDeliveryRequestArguments(string $code, string $language): \SoapVar
152
    {
153
        $input = new CashOnDeliveryEventsInput($code, $language);
154
155
        return new \SoapVar([
156
            new \SoapVar($input, \SOAP_ENC_OBJECT, '', '', 'PostalOrderEventsForMailInput', self::XML_NS_COD_HISTORY),
157
            new \SoapVar([
158
                new \SoapVar($this->login, XSD_STRING, '', '', 'login', self::XML_NS_DATA),
159
                new \SoapVar($this->password, XSD_STRING, '', '', 'password', self::XML_NS_DATA),
160
            ], SOAP_ENC_OBJECT, '', '', 'AuthorizationHeader', self::XML_NS_DATA),
161
        ], \SOAP_ENC_OBJECT);
162
    }
163
}
164