Passed
Push — master ( 2b63db...3a2bd9 )
by Stefan
07:15
created

Rate::getRate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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