Completed
Pull Request — master (#145)
by
unknown
06:36
created

Rate::sendRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Ups;
4
5
use DOMDocument;
6
use DOMElement;
7
use Exception;
8
use SimpleXMLElement;
9
use Ups\Entity\RateRequest;
10
use Ups\Entity\RateResponse;
11
use Ups\Entity\Shipment;
12
13
/**
14
 * Rate API Wrapper.
15
 *
16
 * @author Michael Williams <[email protected]>
17
 */
18
class Rate extends Ups
19
{
20
    const ENDPOINT = '/Rate';
21
22
    /**
23
     * @var RequestInterface
24
     */
25
    private $request;
26
27
    /**
28
     * @var ResponseInterface
29
     *                        todo: make private
30
     */
31
    public $response;
32
33
    /**
34
     * @var string
35
     */
36
    private $requestOption;
37
38
    /**
39
     * @param $rateRequest
40
     *
41
     * @throws Exception
42
     *
43
     * @return RateResponse
44
     */
45 View Code Duplication
    public function shopRates($rateRequest)
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...
46
    {
47
        if ($rateRequest instanceof Shipment) {
48
            $shipment = $rateRequest;
49
            $rateRequest = new RateRequest();
50
            $rateRequest->setShipment($shipment);
51
        }
52
53
        $this->requestOption = 'Shop';
54
55
        return $this->sendRequest($rateRequest);
56
    }
57
58
    /**
59
     * @param $rateRequest
60
     *
61
     * @throws Exception
62
     *
63
     * @return RateResponse
64
     */
65 View Code Duplication
    public function shopRatesTimeInTransit($rateRequest)
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...
66
    {
67
        if ($rateRequest instanceof Shipment) {
68
            $shipment = $rateRequest;
69
            $rateRequest = new RateRequest();
70
            $rateRequest->setShipment($shipment);
71
        }
72
73
        $this->requestOption = 'Shoptimeintransit';
74
75
        return $this->sendRequest($rateRequest);
76
    }
77
78
    /**
79
     * @param $rateRequest
80
     *
81
     * @throws Exception
82
     *
83
     * @return RateResponse
84
     */
85 View Code Duplication
    public function getRate($rateRequest)
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
        if ($rateRequest instanceof Shipment) {
88
            $shipment = $rateRequest;
89
            $rateRequest = new RateRequest();
90
            $rateRequest->setShipment($shipment);
91
        }
92
93
        $this->requestOption = 'Rate';
94
95
        return $this->sendRequest($rateRequest);
96
    }
97
98
    /**
99
     * @param $rateRequest
100
     *
101
     * @throws Exception
102
     *
103
     * @return RateResponse
104
     */
105 View Code Duplication
    public function getRateTimeInTransit($rateRequest)
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...
106
    {
107
        if ($rateRequest instanceof Shipment) {
108
            $shipment = $rateRequest;
109
            $rateRequest = new RateRequest();
110
            $rateRequest->setShipment($shipment);
111
        }
112
113
        $this->requestOption = 'Ratetimeintransit';
114
115
        return $this->sendRequest($rateRequest);
116
    }
117
118
    /**
119
     * Creates and sends a request for the given shipment. This handles checking for
120
     * errors in the response back from UPS.
121
     *
122
     * @param RateRequest $rateRequest
123
     *
124
     * @throws Exception
125
     *
126
     * @return RateResponse
127
     */
128 View Code Duplication
    private function sendRequest(RateRequest $rateRequest)
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...
129
    {
130
        $request = $this->createRequest($rateRequest);
131
132
        $this->response = $this->getRequest()->request($this->createAccess(), $request, $this->compileEndpointUrl(self::ENDPOINT));
133
        $response = $this->response->getResponse();
134
135
        if (null === $response) {
136
            throw new Exception('Failure (0): Unknown error', 0);
137
        }
138
139
        if ($response->Response->ResponseStatusCode == 0) {
140
            throw new Exception(
141
                "Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}",
142
                (int)$response->Response->Error->ErrorCode
143
            );
144
        } else {
145
            return $this->formatResponse($response);
146
        }
147
    }
148
149
    /**
150
     * Create the Rate request.
151
     *
152
     * @param RateRequest $rateRequest The request details. Refer to the UPS documentation for available structure
153
     *
154
     * @return string
155
     */
156
    private function createRequest(RateRequest $rateRequest)
157
    {
158
        $shipment = $rateRequest->getShipment();
159
160
        $document = $xml = new DOMDocument();
161
        $xml->formatOutput = true;
162
163
        /** @var DOMElement $trackRequest */
164
        $trackRequest = $xml->appendChild($xml->createElement('RatingServiceSelectionRequest'));
165
        $trackRequest->setAttribute('xml:lang', 'en-US');
166
167
        $request = $trackRequest->appendChild($xml->createElement('Request'));
168
169
        $node = $xml->importNode($this->createTransactionNode(), true);
170
        $request->appendChild($node);
171
172
        $request->appendChild($xml->createElement('RequestAction', 'Rate'));
173
        $request->appendChild($xml->createElement('RequestOption', $this->requestOption));
174
175
        $trackRequest->appendChild($rateRequest->getPickupType()->toNode($document));
176
177
        $customerClassification = $rateRequest->getCustomerClassification();
178
        if (isset($customerClassification)) {
179
            $trackRequest->appendChild($customerClassification->toNode($document));
180
        }
181
182
        $shipmentNode = $trackRequest->appendChild($xml->createElement('Shipment'));
183
184
        // Support specifying an individual service
185
        $service = $shipment->getService();
186
        if (isset($service)) {
187
            $shipmentNode->appendChild($service->toNode($document));
188
        }
189
190
        $shipper = $shipment->getShipper();
191
        if (isset($shipper)) {
192
            $shipmentNode->appendChild($shipper->toNode($document));
193
        }
194
195
        $shipFrom = $shipment->getShipFrom();
196
        if (isset($shipFrom)) {
197
            $shipmentNode->appendChild($shipFrom->toNode($document));
198
        }
199
200
        $shipTo = $shipment->getShipTo();
201
        if (isset($shipTo)) {
202
            $shipmentNode->appendChild($shipTo->toNode($document));
203
        }
204
205
        $alternateDeliveryAddress = $shipment->getAlternateDeliveryAddress();
206
        if (isset($alternateDeliveryAddress)) {
207
            $shipmentNode->appendChild($alternateDeliveryAddress->toNode($document));
208
        }
209
210
        $rateInformation = $shipment->getRateInformation();
211
        if ($rateInformation !== null) {
212
            $shipmentNode->appendChild($rateInformation->toNode($document));
213
        }
214
215
        $shipmentIndicationType = $shipment->getShipmentIndicationType();
216
        if (isset($shipmentIndicationType)) {
217
            $shipmentNode->appendChild($shipmentIndicationType->toNode($document));
218
        }
219
220
        foreach ($shipment->getPackages() as $package) {
221
            $shipmentNode->appendChild($package->toNode($document));
222
        }
223
224
        $shipmentServiceOptions = $shipment->getShipmentServiceOptions();
225
        if (isset($shipmentServiceOptions)) {
226
            $shipmentNode->appendChild($shipmentServiceOptions->toNode($xml));
227
        }
228
229
        $deliveryTimeInformation = $shipment->getDeliveryTimeInformation();
230
        if (isset($deliveryTimeInformation)) {
231
            $shipmentNode->appendChild($deliveryTimeInformation->toNode($xml));
232
        }
233
234
        return $xml->saveXML();
235
    }
236
237
    /**
238
     * Format the response.
239
     *
240
     * @param SimpleXMLElement $response
241
     *
242
     * @return RateResponse
243
     */
244
    private function formatResponse(SimpleXMLElement $response)
245
    {
246
        // We don't need to return data regarding the response to the user
247
        unset($response->Response);
248
249
        $result = $this->convertXmlObject($response);
250
251
        return new RateResponse($result);
252
    }
253
254
    /**
255
     * @return RequestInterface
256
     */
257 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...
258
    {
259
        if (null === $this->request) {
260
            $this->request = new Request($this->logger);
261
        }
262
263
        return $this->request;
264
    }
265
266
    /**
267
     * @param RequestInterface $request
268
     *
269
     * @return $this
270
     */
271
    public function setRequest(RequestInterface $request)
272
    {
273
        $this->request = $request;
274
275
        return $this;
276
    }
277
278
    /**
279
     * @return ResponseInterface
280
     */
281
    public function getResponse()
282
    {
283
        return $this->response;
284
    }
285
286
    /**
287
     * @param ResponseInterface $response
288
     *
289
     * @return $this
290
     */
291
    public function setResponse(ResponseInterface $response)
292
    {
293
        $this->response = $response;
294
295
        return $this;
296
    }
297
}
298