Passed
Push — master ( 997785...de53d8 )
by Jhao
02:17
created

PacketAccessClient::callSoapMethod()   A

Complexity

Conditions 3
Paths 14

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 14
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\Packet\TicketResponse;
20
use Appwilio\RussianPostSDK\Tracking\Packet\TrackingResponse;
21
use Appwilio\RussianPostSDK\Tracking\Exceptions\PacketAccessException;
22
23
class PacketAccessClient implements LoggerAwareInterface
24
{
25
    use LoggerAwareTrait;
26
27
    public const LANG_ENG = 'ENG';
28
    public const LANG_RUS = 'RUS';
29
30
    public const HISTORY_OPERATIONS    = 0;
31
    public const HISTORY_NOTIFICATIONS = 1;
32
33
    public const ERROR_NOT_READY = 6;
34
    public const ERROR_INTERNAL  = 16;
35
36
    protected const WSDL_URL = 'https://tracking.pochta.ru/tracking-web-static/fc_wsdl.xml';
37
38
    /** @var string */
39
    protected $login;
40
41
    /** @var string */
42
    protected $password;
43
44
    /** @var \SoapClient */
45
    protected $client;
46
47
    protected $options = [
48
        'soap_version' => \SOAP_1_1,
49
        'trace'        => 1,
50
        'classmap'     => [
51
            'item'                   => Packet\Item::class,      // Item согласно wsdl-описанию называется item
52
            'error'                  => Packet\Error::class,     // корневая ошибка
53
            'Error'                  => Packet\Error::class,     // ошибка конкретного трека
54
            'file'                   => Packet\Wrapper::class,   // value согласно wsdl-описанию называется file
55
            'operation'              => Packet\Operation::class, // Operation согласно wsdl-описанию называется operation
56
57
            'ticketResponse'         => TicketResponse::class,
58
            'answerByTicketResponse' => TrackingResponse::class,
59
        ],
60
    ];
61
62 6
    public function __construct(string $login, string $password)
63
    {
64 6
        $this->logger = new NullLogger();
65
66 6
        $this->login = $login;
67 6
        $this->password = $password;
68 6
    }
69
70 2
    public function getTicket(iterable $tracks, string $language = self::LANG_RUS): TicketResponse
71
    {
72 2
        if (\is_array($tracks)) {
73 1
            $tracks = new \ArrayIterator($tracks);
74
        }
75
76 2
        if (\iterator_count($tracks) > 3000) {
77 1
            throw PacketAccessException::trackNumberLimitExceeded();
78
        }
79
80 1
        return $this->callSoapMethod(
81 1
            'getTicket',
82 1
            $this->assembleTicketRequestArguments($tracks, $language)
83
        );
84
    }
85
86 3
    public function getTrackingEvents(string $ticket): TrackingResponse
87
    {
88 3
        return $this->callSoapMethod(
89 3
            'getResponseByTicket',
90 3
            $this->assembleTrackingRequestArgument($ticket)
91
        );
92
    }
93
94 4
    protected function getClient(): \SoapClient
95
    {
96 4
        if (! $this->client) {
97
            $this->client = new \SoapClient(self::WSDL_URL, $this->options);
98
        }
99
100 4
        return $this->client;
101
    }
102
103 4
    private function callSoapMethod(string $method, \SoapVar $arguments)
104
    {
105
        try {
106 4
            $response = $this->getClient()->__soapCall($method, [$arguments]);
107
108 3
            if ($response->hasError()) {
109 1
                throw new PacketAccessException($response->getError()->getMessage(), $response->getError()->getCode());
110
            }
111
112 2
            return $response;
113 2
        } catch (\SoapFault $e) {
114 1
            throw new PacketAccessException($e->getMessage(), $e->getCode(), $e);
115
        } finally {
116 4
            $this->logger->info("pochta.ru Packet Tracking request: {$this->getClient()->__getLastRequest()}");
117 4
            $this->logger->info("pochta.ru Packet Tracking response: {$this->getClient()->__getLastResponse()}");
118
        }
119
    }
120
121 1
    private function assembleTicketRequestArguments(iterable $tracks, string $language): \SoapVar
122
    {
123 1
        $items = new \ArrayObject();
124
125 1
        foreach ($tracks as $track) {
126 1
            $items->append(new \SoapVar("<Item Barcode=\"{$track}\" />", \XSD_ANYXML));
127
        }
128
129 1
        return new \SoapVar([
130 1
            new \SoapVar($items, \SOAP_ENC_OBJECT, '', '', 'request'),
131 1
            new \SoapVar($this->login, \XSD_STRING, '', '', 'login'),
132 1
            new \SoapVar($this->password, \XSD_STRING, '', '', 'password'),
133 1
            new \SoapVar($language, \XSD_STRING, '', '', 'language'),
134 1
        ], \SOAP_ENC_OBJECT);
135
    }
136
137 3
    private function assembleTrackingRequestArgument(string $ticket): \SoapVar
138
    {
139 3
        return new \SoapVar([
140 3
            new \SoapVar($ticket, \XSD_STRING, '', '', 'ticket'),
141 3
            new \SoapVar($this->login, \XSD_STRING, '', '', 'login'),
142 3
            new \SoapVar($this->password, \XSD_STRING, '', '', 'password'),
143 3
        ], \SOAP_ENC_OBJECT);
144
    }
145
}
146