Passed
Pull Request — master (#157)
by Rudger
03:17
created

Locator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 162
Duplicated Lines 20.99 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 67.27%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 34
loc 162
ccs 37
cts 55
cp 0.6727
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 2
A getLocations() 0 4 1
A sendRequest() 19 19 4
A formatResponse() 0 6 1
A getRequest() 8 8 2
A setRequest() 0 6 1
A getResponse() 0 4 1
A setResponse() 0 6 1
B createRequest() 0 34 3

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 Exception;
7
use Psr\Log\LoggerInterface;
8
use SimpleXMLElement;
9
use Ups\Entity\LocatorRequest;
10
11
/**
12
 * Locator API Wrapper.
13
 *
14
 * @author Stefan Doorn <[email protected]>
15
 */
16
class Locator extends Ups
17
{
18
    private $request;
19
20
    const ENDPOINT = '/Locator';
21
22
    const OPTION_DROP_LOCATIONS_AND_WILL_CALL_LOCATIONS = 1;
23
    const OPTION_ALL_AVAILABLE_ADDITIONAL_SERVICES = 8;
24
    const OPTION_ALL_AVAILABLE_PROGRAM_TYPES = 16;
25
    const OPTION_ALL_AVAILABLE_ADDITIONAL_SERVICES_AND_PROGRAM_TYPES = 24;
26
    const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS = 32;
27
    const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_ADDITIONAL_SERVICES = 40;
28
    const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_PROGRAM_TYPES = 48;
29
    const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_ADDITIONAL_SERVICES_AND_PROGRAM_TYPES = 56;
30
    const OPTION_UPS_ACCESS_POINT_LOCATIONS = 64;
31
32
    /**
33
     * @param string|null $accessKey UPS License Access Key
34
     * @param string|null $userId UPS User ID
35
     * @param string|null $password UPS User Password
36
     * @param bool $useIntegration Determine if we should use production or CIE URLs.
37
     * @param RequestInterface $request
38
     * @param LoggerInterface $logger PSR3 compatible logger (optional)
39
     */
40 3 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...
41
    {
42 3
        if (null !== $request) {
43
            $this->setRequest($request);
44
        }
45 3
        parent::__construct($accessKey, $userId, $password, $useIntegration, $logger);
46 3
    }
47
48 3
    public function getLocations(LocatorRequest $request, $requestOption = self::OPTION_UPS_ACCESS_POINT_LOCATIONS)
49
    {
50 3
        return $this->sendRequest($request, $requestOption);
51
    }
52
53
    /**
54
     * Creates and sends a request for the given shipment. This handles checking for
55
     * errors in the response back from UPS.
56
     *
57
     * @param LocatorRequest $request
58
     * @param int $requestOption
59
     *
60
     * @throws Exception
61
     *
62
     * @return \stdClass
63
     */
64 3 View Code Duplication
    private function sendRequest(LocatorRequest $request, $requestOption)
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...
65
    {
66 3
        $request = $this->createRequest($request, $requestOption);
67 3
        $this->response = $this->getRequest()->request($this->createAccess(), $request, $this->compileEndpointUrl(self::ENDPOINT));
0 ignored issues
show
Deprecated Code introduced by
The property Ups\Ups::$response has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
68 3
        $response = $this->response->getResponse();
0 ignored issues
show
Deprecated Code introduced by
The property Ups\Ups::$response has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
69
70 3
        if (null === $response) {
71 3
            throw new Exception('Failure (0): Unknown error', 0);
72
        }
73
74
        if ($response instanceof SimpleXMLElement && $response->Response->ResponseStatusCode == 0) {
75
            throw new Exception(
76
                "Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}",
77
                (int)$response->Response->Error->ErrorCode
78
            );
79
        } else {
80
            return $this->formatResponse($response);
81
        }
82
    }
83
84
    /**
85
     * Create the TimeInTransit request.
86
     *
87
     * @param LocatorRequest $locatorRequest The request details. Refer to the UPS documentation for available structure
88
     * @param $requestOption
89
     *
90
     * @return string
91
     */
92 3
    private function createRequest(LocatorRequest $locatorRequest, $requestOption)
93
    {
94 3
        $xml = new DOMDocument();
95 3
        $xml->formatOutput = true;
96
97 3
        $trackRequest = $xml->appendChild($xml->createElement('LocatorRequest'));
98 3
        $trackRequest->setAttribute('xml:lang', 'en-US');
99
100 3
        $request = $trackRequest->appendChild($xml->createElement('Request'));
101
102 3
        $node = $xml->importNode($this->createTransactionNode(), true);
103 3
        $request->appendChild($node);
104
105 3
        $request->appendChild($xml->createElement('RequestAction', 'Locator'));
106 3
        $request->appendChild($xml->createElement('RequestOption', $requestOption));
107
108
        // Origin Address
109 3
        $trackRequest->appendChild($locatorRequest->getOriginAddress()->toNode($xml));
110
111
        // Translate
112 3
        $trackRequest->appendChild($locatorRequest->getTranslate()->toNode($xml));
113
114
        // Unit of measurement
115 3
        if ($locatorRequest->getUnitOfMeasurement()) {
116 3
            $trackRequest->appendChild($locatorRequest->getUnitOfMeasurement()->toNode($xml));
117 3
        }
118
119
        // LocationSearchCriteria
120 3
        if ($locatorRequest->getLocationSearchCriteria()) {
121 2
            $trackRequest->appendChild($locatorRequest->getLocationSearchCriteria()->toNode($xml));
122 2
        }
123
124 3
        return $xml->saveXML();
125
    }
126
127
    private function formatResponse(SimpleXMLElement $response)
128
    {
129
        unset($response->Response);
130
131
        return $this->convertXmlObject($response);
132
    }
133
134
    /**
135
     * @return RequestInterface
136
     */
137 3 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...
138
    {
139 3
        if (null === $this->request) {
140
            $this->request = new Request($this->logger);
141
        }
142
143 3
        return $this->request;
144
    }
145
146
    /**
147
     * @param RequestInterface $request
148
     *
149
     * @return $this
150
     */
151 3
    public function setRequest(RequestInterface $request)
152
    {
153 3
        $this->request = $request;
154
155 3
        return $this;
156
    }
157
158
    /**
159
     * @return ResponseInterface
160
     */
161
    public function getResponse()
162
    {
163
        return $this->response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->response; of type Ups\ResponseInterface|SimpleXMLElement adds the type SimpleXMLElement to the return on line 163 which is incompatible with the return type documented by Ups\Locator::getResponse of type Ups\ResponseInterface.
Loading history...
Deprecated Code introduced by
The property Ups\Ups::$response has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
164
    }
165
166
    /**
167
     * @param ResponseInterface $response
168
     *
169
     * @return $this
170
     */
171
    public function setResponse(ResponseInterface $response)
172
    {
173
        $this->response = $response;
0 ignored issues
show
Deprecated Code introduced by
The property Ups\Ups::$response has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
174
175
        return $this;
176
    }
177
}
178