Passed
Push — master ( fcefb5...9291e6 )
by Jhao
02:07
created

PacketAccessClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
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
namespace Appwilio\RussianPostSDK\Tracking;
13
14
use Appwilio\RussianPostSDK\Tracking\Packet\TicketResponse;
15
use Appwilio\RussianPostSDK\Tracking\Packet\TrackingResponse;
16
use Appwilio\RussianPostSDK\Tracking\Exceptions\PacketAccessException;
17
18
class PacketAccessClient
19
{
20
    public const LANG_ENG = 'ENG';
21
    public const LANG_RUS = 'RUS';
22
23
    public const HISTORY_OPERATIONS = 0;
24
    public const HISTORY_NOTIFICATIONS = 1;
25
26
    public const ERROR_NOT_READY = 6;
27
    public const ERROR_INTERNAL = 16;
28
29
    protected const WSDL_URL = 'https://tracking.russianpost.ru/fc?wsdl';
30
31
    /** @var string */
32
    protected $login;
33
34
    /** @var string */
35
    protected $password;
36
37
    /** @var \SoapClient */
38
    protected $client;
39
40
    protected $options = [
41
        'soap_version' => SOAP_1_1,
42
        'trace'        => 1,
43
        'classmap'     => [
44
            'item'                   => Packet\Event::class,     // Item согласно wsdl-описанию называется item
45
            'file'                   => Packet\Value::class,     // value согласно wsdl-описанию называется file
46
            'error'                  => Packet\Error::class,     // корневая ошибка
47
            'Error'                  => Packet\Error::class,     // ошибка конкретного трека
48
            'operation'              => Packet\Operation::class, // Operation согласно wsdl-описанию называется operation
49
            'ticketResponse'         => TicketResponse::class,
50
            'answerByTicketResponse' => TrackingResponse::class,
51
        ],
52
    ];
53
54
    public function __construct(string $login, string $password)
55
    {
56
        $this->login = $login;
57
        $this->password = $password;
58
    }
59
60
    public function getTicket(iterable $tracks, string $language = self::LANG_RUS): TicketResponse
61
    {
62
        if (is_array($tracks)) {
63
            $tracks = new \ArrayIterator($tracks);
64
        }
65
66
        if (iterator_count($tracks) > 3000) {
67
            throw PacketAccessException::tracksNumberLimitExceeded();
68
        }
69
70
        $arguments = $this->assembleTicketRequestArguments($tracks, $language);
0 ignored issues
show
Bug introduced by
It seems like $tracks can also be of type ArrayIterator; however, parameter $tracks of Appwilio\RussianPostSDK\...icketRequestArguments() does only seem to accept iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $arguments = $this->assembleTicketRequestArguments(/** @scrutinizer ignore-type */ $tracks, $language);
Loading history...
71
72
        /** @var TicketResponse $response */
73
        $response = $this->getClient()->{'getTicket'}($arguments);
74
75
        if ($response->hasError()) {
76
            throw new PacketAccessException($response->getError()->getMessage(), $response->getError()->getCode());
77
        }
78
79
        return $response;
80
    }
81
82
    public function getTrackingEvents(string $ticket): TrackingResponse
83
    {
84
        $arguments = $this->assembleTrackingRequestArgument($ticket);
85
86
        /** @var TrackingResponse $response */
87
        $response = $this->getClient()->{'getResponseByTicket'}($arguments);
88
89
        if ($response->hasError()) {
90
            throw new PacketAccessException($response->getError()->getMessage(), $response->getError()->getCode());
91
        }
92
93
        return $response;
94
    }
95
96
    private function assembleTicketRequestArguments(iterable $tracks, string $language): \SoapVar
97
    {
98
        $items = new \ArrayObject();
99
100
        foreach ($tracks as $track) {
101
            $items->append(new \SoapVar("<Item Barcode=\"{$track}\" />", XSD_ANYXML));
102
        }
103
104
        return new \SoapVar([
105
            new \SoapVar($items, SOAP_ENC_OBJECT, null, null, 'request'),
106
            new \SoapVar($this->login, XSD_STRING, null, null, 'login'),
107
            new \SoapVar($this->password, XSD_STRING, null, null, 'password'),
108
            new \SoapVar($language, XSD_STRING, null, null, 'language'),
109
        ], SOAP_ENC_OBJECT);
110
    }
111
112
    private function assembleTrackingRequestArgument(string $ticket): \SoapVar
113
    {
114
        return new \SoapVar([
115
            new \SoapVar($ticket, XSD_STRING, null, null, 'ticket'),
116
            new \SoapVar($this->login, XSD_STRING, null, null, 'login'),
117
            new \SoapVar($this->password, XSD_STRING, null, null, 'password'),
118
        ], SOAP_ENC_OBJECT);
119
    }
120
121
    public function getClient(): \SoapClient
122
    {
123
        if (! $this->client) {
124
            $this->client = new \SoapClient(self::WSDL_URL, $this->options);
125
        }
126
127
        return $this->client;
128
    }
129
}
130