Passed
Push — master ( 17c5f7...cb633b )
by Stefan
04:29 queued 02:00
created

Rate   C

Complexity

Total Complexity 25

Size/Duplication

Total Lines 240
Duplicated Lines 22.08 %

Coupling/Cohesion

Components 1
Dependencies 19

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 19
dl 53
loc 240
ccs 0
cts 102
cp 0
rs 6.875
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A shopRates() 12 12 2
A getRate() 12 12 2
A sendRequest() 20 20 3
F createRequest() 0 80 12
A formatResponse() 0 9 1
A getRequest() 8 8 2
A setRequest() 0 6 1
A getResponse() 0 4 1
A setResponse() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    protected $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 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...
66
    {
67
        if ($rateRequest instanceof Shipment) {
68
            $shipment = $rateRequest;
69
            $rateRequest = new RateRequest();
70
            $rateRequest->setShipment($shipment);
71
        }
72
73
        $this->requestOption = 'Rate';
74
75
        return $this->sendRequest($rateRequest);
76
    }
77
78
    /**
79
     * Creates and sends a request for the given shipment. This handles checking for
80
     * errors in the response back from UPS.
81
     *
82
     * @param RateRequest $rateRequest
83
     *
84
     * @throws Exception
85
     *
86
     * @return RateResponse
87
     */
88 View Code Duplication
    protected 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...
89
    {
90
        $request = $this->createRequest($rateRequest);
91
92
        $this->response = $this->getRequest()->request($this->createAccess(), $request, $this->compileEndpointUrl(self::ENDPOINT));
93
        $response = $this->response->getResponse();
94
95
        if (null === $response) {
96
            throw new Exception('Failure (0): Unknown error', 0);
97
        }
98
99
        if ($response->Response->ResponseStatusCode == 0) {
100
            throw new Exception(
101
                "Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}",
102
                (int)$response->Response->Error->ErrorCode
103
            );
104
        } else {
105
            return $this->formatResponse($response);
106
        }
107
    }
108
109
    /**
110
     * Create the Rate request.
111
     *
112
     * @param RateRequest $rateRequest The request details. Refer to the UPS documentation for available structure
113
     *
114
     * @return string
115
     */
116
    private function createRequest(RateRequest $rateRequest)
117
    {
118
        $shipment = $rateRequest->getShipment();
119
120
        $document = $xml = new DOMDocument();
121
        $xml->formatOutput = true;
122
123
        /** @var DOMElement $trackRequest */
124
        $trackRequest = $xml->appendChild($xml->createElement('RatingServiceSelectionRequest'));
125
        $trackRequest->setAttribute('xml:lang', 'en-US');
126
127
        $request = $trackRequest->appendChild($xml->createElement('Request'));
128
129
        $node = $xml->importNode($this->createTransactionNode(), true);
130
        $request->appendChild($node);
131
132
        $request->appendChild($xml->createElement('RequestAction', 'Rate'));
133
        $request->appendChild($xml->createElement('RequestOption', $this->requestOption));
134
135
        $trackRequest->appendChild($rateRequest->getPickupType()->toNode($document));
136
137
        $customerClassification = $rateRequest->getCustomerClassification();
138
        if (isset($customerClassification)) {
139
            $trackRequest->appendChild($customerClassification->toNode($document));
140
        }
141
142
        $shipmentNode = $trackRequest->appendChild($xml->createElement('Shipment'));
143
144
        // Support specifying an individual service
145
        $service = $shipment->getService();
146
        if (isset($service)) {
147
            $shipmentNode->appendChild($service->toNode($document));
148
        }
149
150
        $shipper = $shipment->getShipper();
151
        if (isset($shipper)) {
152
            $shipmentNode->appendChild($shipper->toNode($document));
153
        }
154
155
        $shipFrom = $shipment->getShipFrom();
156
        if (isset($shipFrom)) {
157
            $shipmentNode->appendChild($shipFrom->toNode($document));
158
        }
159
160
        $shipTo = $shipment->getShipTo();
161
        if (isset($shipTo)) {
162
            $shipmentNode->appendChild($shipTo->toNode($document));
163
        }
164
165
        $alternateDeliveryAddress = $shipment->getAlternateDeliveryAddress();
166
        if (isset($alternateDeliveryAddress)) {
167
            $shipmentNode->appendChild($alternateDeliveryAddress->toNode($document));
168
        }
169
170
        $rateInformation = $shipment->getRateInformation();
171
        if ($rateInformation !== null) {
172
            $shipmentNode->appendChild($rateInformation->toNode($document));
173
        }
174
175
        $shipmentIndicationType = $shipment->getShipmentIndicationType();
176
        if (isset($shipmentIndicationType)) {
177
            $shipmentNode->appendChild($shipmentIndicationType->toNode($document));
178
        }
179
180
        foreach ($shipment->getPackages() as $package) {
181
            $shipmentNode->appendChild($package->toNode($document));
182
        }
183
184
        $shipmentServiceOptions = $shipment->getShipmentServiceOptions();
185
        if (isset($shipmentServiceOptions)) {
186
            $shipmentNode->appendChild($shipmentServiceOptions->toNode($xml));
187
        }
188
189
        $deliveryTimeInformation = $shipment->getDeliveryTimeInformation();
190
        if (isset($deliveryTimeInformation)) {
191
            $shipmentNode->appendChild($deliveryTimeInformation->toNode($xml));
192
        }
193
194
        return $xml->saveXML();
195
    }
196
197
    /**
198
     * Format the response.
199
     *
200
     * @param SimpleXMLElement $response
201
     *
202
     * @return RateResponse
203
     */
204
    private function formatResponse(SimpleXMLElement $response)
205
    {
206
        // We don't need to return data regarding the response to the user
207
        unset($response->Response);
208
209
        $result = $this->convertXmlObject($response);
210
211
        return new RateResponse($result);
212
    }
213
214
    /**
215
     * @return RequestInterface
216
     */
217 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...
218
    {
219
        if (null === $this->request) {
220
            $this->request = new Request($this->logger);
221
        }
222
223
        return $this->request;
224
    }
225
226
    /**
227
     * @param RequestInterface $request
228
     *
229
     * @return $this
230
     */
231
    public function setRequest(RequestInterface $request)
232
    {
233
        $this->request = $request;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @return ResponseInterface
240
     */
241
    public function getResponse()
242
    {
243
        return $this->response;
244
    }
245
246
    /**
247
     * @param ResponseInterface $response
248
     *
249
     * @return $this
250
     */
251
    public function setResponse(ResponseInterface $response)
252
    {
253
        $this->response = $response;
254
255
        return $this;
256
    }
257
}
258