Completed
Push — master ( 2792e5...4b31fe )
by Stefan
03:32
created

Rate::createRequest()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 75
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 75
ccs 0
cts 53
cp 0
rs 3.375
cc 11
eloc 43
nc 1024
nop 1
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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));
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
        $customerClassification = $rateRequest->getCustomerClassification();
140
        if (isset($customerClassification)) {
141
            $trackRequest->appendChild($customerClassification->toNode($document));
142
        }
143
144
        $shipmentNode = $trackRequest->appendChild($xml->createElement('Shipment'));
145
146
        // Support specifying an individual service
147
        $service = $shipment->getService();
148
        if (isset($service)) {
149
            $shipmentNode->appendChild($service->toNode($document));
150
        }
151
152
        $shipper = $shipment->getShipper();
153
        if (isset($shipper)) {
154
            $shipmentNode->appendChild($shipper->toNode($document));
155
        }
156
157
        $shipFrom = $shipment->getShipFrom();
158
        if (isset($shipFrom)) {
159
            $shipmentNode->appendChild($shipFrom->toNode($document));
160
        }
161
162
        $shipTo = $shipment->getShipTo();
163
        if (isset($shipTo)) {
164
            $shipmentNode->appendChild($shipTo->toNode($document));
165
        }
166
167
        $alternateDeliveryAddress = $shipment->getAlternateDeliveryAddress();
168
        if (isset($alternateDeliveryAddress)) {
169
            $shipmentNode->appendChild($alternateDeliveryAddress->toNode($document));
170
        }
171
172
        $rateInformation = $shipment->getRateInformation();
173
        if ($rateInformation !== null) {
174
            $shipmentNode->appendChild($rateInformation->toNode($document));
175
        }
176
177
        $shipmentIndicationType = $shipment->getShipmentIndicationType();
178
        if (isset($shipmentIndicationType)) {
179
            $shipmentNode->appendChild($shipmentIndicationType->toNode($document));
180
        }
181
182
        foreach ($shipment->getPackages() as $package) {
183
            $shipmentNode->appendChild($package->toNode($document));
184
        }
185
186
        $shipmentServiceOptions = $shipment->getShipmentServiceOptions();
187
        if (isset($shipmentServiceOptions)) {
188
            $shipmentNode->appendChild($shipmentServiceOptions->toNode($xml));
189
        }
190
191
        return $xml->saveXML();
192
    }
193
194
    /**
195
     * Format the response.
196
     *
197
     * @param SimpleXMLElement $response
198
     *
199
     * @return stdClass
200
     */
201
    private function formatResponse(SimpleXMLElement $response)
202
    {
203
        // We don't need to return data regarding the response to the user
204
        unset($response->Response);
205
206
        $result = $this->convertXmlObject($response);
207
208
        return new RateResponse($result);
209
    }
210
211
    /**
212
     * @return RequestInterface
213
     */
214 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...
215
    {
216
        if (null === $this->request) {
217
            $this->request = new Request($this->logger);
218
        }
219
220
        return $this->request;
221
    }
222
223
    /**
224
     * @param RequestInterface $request
225
     *
226
     * @return $this
227
     */
228
    public function setRequest(RequestInterface $request)
229
    {
230
        $this->request = $request;
231
232
        return $this;
233
    }
234
235
    /**
236
     * @return ResponseInterface
237
     */
238
    public function getResponse()
239
    {
240
        return $this->response;
241
    }
242
243
    /**
244
     * @param ResponseInterface $response
245
     *
246
     * @return $this
247
     */
248
    public function setResponse(ResponseInterface $response)
249
    {
250
        $this->response = $response;
251
252
        return $this;
253
    }
254
}
255