Completed
Pull Request — master (#88)
by
unknown
04:47
created

Tracking::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 6
crap 2
1
<?php
2
3
namespace Ups;
4
5
use DOMDocument;
6
use Exception;
7
use Psr\Log\LoggerInterface;
8
use SimpleXMLElement;
9
use stdClass;
10
11
/**
12
 * Tracking API Wrapper.
13
 */
14
class Tracking extends Ups
15
{
16
    const ENDPOINT = '/Track';
17
18
    /**
19
     * @var RequestInterface
20
     */
21
    private $request;
22
23
    /**
24
     * @var ResponseInterface
25
     *                        // todo make private
26
     */
27
    public $response;
28
29
    /**
30
     * @var string
31
     */
32
    private $trackingNumber;
33
34
    /**
35
     * @var string
36
     */
37
    private $referenceNumber;
38
39
    /**
40
     * @var string
41
     */
42
    private $requestOption;
43
44
    /**
45
     * @var string
46
     */
47
    private $shipperNumber;
48
49
    /**
50
     * @var string
51
     */
52 6
    private $beginDate;
53
54 6
    /**
55
     * @var string
56
     */
57 6
    private $endDate;
58 6
59
    /**
60
     * @param string|null $accessKey UPS License Access Key
61
     * @param string|null $userId UPS User ID
62
     * @param string|null $password UPS User Password
63
     * @param bool $useIntegration Determine if we should use production or CIE URLs.
64
     * @param RequestInterface $request
65
     * @param LoggerInterface PSR3 compatible logger (optional)
66
     */
67 View Code Duplication
    public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false, RequestInterface $request = null, LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        if (null !== $request) {
70 5
            $this->setRequest($request);
71
        }
72 5
        parent::__construct($accessKey, $userId, $password, $useIntegration, $logger);
73 5
    }
74
75 5
    /**
76 5
     * Get package tracking information.
77
     *
78 5
     * @param string $trackingNumber The package's tracking number.
79 5
     * @param string $requestOption Optional processing. For Mail Innovations the only valid options are Last Activity and All activity.
80
     *
81 5
     * @throws Exception
82 2
     *
83
     * @return stdClass
84
     */
85 3 View Code Duplication
    public function track($trackingNumber, $requestOption = 'activity')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $this->trackingNumber = $trackingNumber;
88
        $this->requestOption = $requestOption;
89
90
        $access = $this->createAccess();
91 3
        $request = $this->createRequest();
92
93
        $this->response = $this->getRequest()->request($access, $request, $this->compileEndpointUrl(self::ENDPOINT));
94
        $response = $this->response->getResponse();
95
96
        if (null === $response) {
97
            throw new Exception('Failure (0): Unknown error', 0);
98
        }
99
100
        if ($response instanceof SimpleXMLElement && $response->Response->ResponseStatusCode == 0) {
101
            throw new Exception(
102
                "Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}",
103
                (int)$response->Response->Error->ErrorCode
104
            );
105 1
        } else {
106
            return $this->formatResponse($response);
107 1
        }
108 1
    }
109
110 1
    /**
111 1
     * Set shipper number
112
     *
113 1
     * @param string $shipperNumber
114 1
     *
115
     */
116 1
    public function setShipperNumber($shipperNumber)
117 1
    {
118
        $this->shipperNumber = $shipperNumber;
119
    }
120
121
    /**
122
     * Set begin date
123
     *
124
     * @param string $beginDate
125
     *
126
     */
127
    public function setBeginDate($beginDate)
128
    {
129
        $this->beginDate = $beginDate;
130
    }
131
132
    /**
133
     * Set end date
134
     *
135 6
     * @param string $endDate
136
     *
137
     */
138
    public function setEndDate($endDate)
139
    {
140 6
        $this->endDate = $endDate;
141
    }
142
143 6
    /**
144 6
     * Get package tracking information.
145
     *
146
     * @param string $referenceNumber Reference numbers can be a purchase order number, job number, etc. Reference number can be added when creating a shipment.
147 6
     * @throws Exception
148 6
     *
149
     * @return stdClass
150
     */
151 6 View Code Duplication
    public function trackByReference($referenceNumber, $requestOption = 'activity')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
        $this->referenceNumber = $referenceNumber;
154 6
        $this->requestOption = $requestOption;
155
156
        $access = $this->createAccess();
157 6
        $request = $this->createRequest();
158 6
159
        $this->response = $this->getRequest()->request($access, $request, $this->compileEndpointUrl(self::ENDPOINT));
160
        $response = $this->response->getResponse();
161 6
162
        if (null === $response) {
163
            throw new Exception('Failure (0): Unknown error', 0);
164 6
        }
165 6
166
        if ($response instanceof SimpleXMLElement && $response->Response->ResponseStatusCode == 0) {
167
            throw new Exception(
168 6
                "Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}",
169 6
                (int)$response->Response->Error->ErrorCode
170
            );
171
        } else {
172 6
            return $this->formatResponse($response);
173 6
        }
174
    }
175
176 6
    /**
177
     * Create the Tracking request.
178 6
     *
179
     * @return string
180 6
     */
181 6
    private function createRequest()
182 1
    {
183
        $xml = new DOMDocument();
184 6
        $xml->formatOutput = true;
185
186 5
        $trackRequest = $xml->appendChild($xml->createElement('TrackRequest'));
187
        $trackRequest->setAttribute('xml:lang', 'en-US');
188
189
        $request = $trackRequest->appendChild($xml->createElement('Request'));
190
191
        $node = $xml->importNode($this->createTransactionNode(), true);
192
        $request->appendChild($node);
193
194 6
        $request->appendChild($xml->createElement('RequestAction', 'Track'));
195
196 6
        if (null !== $this->requestOption) {
197 6
            $request->appendChild($xml->createElement('RequestOption', $this->requestOption));
198
        }
199 6
200 6
        if (null !== $this->trackingNumber) {
201
            $trackRequest->appendChild($xml->createElement('TrackingNumber', $this->trackingNumber));
202 6
        }
203
204 6
        if (null !== $this->referenceNumber) {
205 6
            $trackRequest->appendChild($xml->createElement('ReferenceNumber'))->appendChild($xml->createElement('Value', $this->referenceNumber));
206
        }
207 6
208
        if(null !== $this->shipperNumber) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
209 6
            $trackRequest->appendChild($xml->createElement('ShipperNumber', $this->shipperNumber));
210 6
        }
211 6
212
        if(null !== $this->beginDate) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
213 6
            $trackRequest->appendChild($xml->createElement('BeginDate', $this->beginDate));
214 5
        }
215 5
216
        if(null !== $this->endDate) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
217 6
            $trackRequest->appendChild($xml->createElement('EndDate', $this->endDate));
218 1
        }
219 1
220
        return $xml->saveXML();
221 6
    }
222 1
223 1
    /**
224 6
     * Format the response.
225
     *
226
     * @param SimpleXMLElement $response
227
     *
228
     * @return stdClass
229
     */
230
    private function formatResponse(SimpleXMLElement $response)
231
    {
232
        return $this->convertXmlObject($response->Shipment);
233
    }
234 3
235
    /**
236 3
     * @return RequestInterface
237
     */
238 View Code Duplication
    public function getRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
    {
240
        if (null === $this->request) {
241
            $this->request = new Request($this->logger);
242 6
        }
243
244 6
        return $this->request;
245
    }
246
247
    /**
248 6
     * @param RequestInterface $request
249
     *
250
     * @return $this
251
     */
252
    public function setRequest(RequestInterface $request)
253
    {
254
        $this->request = $request;
255
256 6
        return $this;
257
    }
258 6
259
    /**
260 6
     * @return ResponseInterface
261
     */
262
    public function getResponse()
263
    {
264
        return $this->response;
265
    }
266 2
267
    /**
268 2
     * @param ResponseInterface $response
269
     *
270
     * @return $this
271
     */
272
    public function setResponse(ResponseInterface $response)
273
    {
274
        $this->response = $response;
275
276
        return $this;
277
    }
278
}
279