Passed
Pull Request — master (#223)
by
unknown
02:52
created

Tradeability   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 159
Duplicated Lines 8.18 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 13
loc 159
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A formatResponse() 0 4 1
A __construct() 13 13 2
A getLandedCosts() 0 14 2
A createRequestLandedCost() 0 21 2
A sendRequest() 0 14 2
A getRequest() 0 8 2
A setRequest() 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 Exception;
7
use Psr\Log\LoggerInterface;
8
use SimpleXMLElement;
9
use Ups\Entity\Tradeability\LandedCostRequest;
10
11
/**
12
 * Tradeability API Wrapper.
13
 *
14
 * @author Stefan Doorn <[email protected]>
15
 */
16
class Tradeability extends Ups
17
{
18
19
    /**
20
     *
21
     */
22
    const ENDPOINT_LANDEDCOST = '/LandedCost';
23
    /**
24
     * @var string
25
     *
26
     * @deprecated
27
     */
28
    protected $productionBaseUrl = 'https://www.ups.com/webservices';
29
30
    /**
31
     * @var string
32
     *
33
     * @deprecated
34
     */
35
    protected $integrationBaseUrl = 'https://wwwcie.ups.com/webservices';
36
    /**
37
     * @var
38
     */
39
    private $request;
40
41
    /**
42
     * @param string|null $accessKey UPS License Access Key
43
     * @param string|null $userId UPS User ID
44
     * @param string|null $password UPS User Password
45
     * @param bool $useIntegration Determine if we should use production or CIE URLs.
46
     * @param RequestInterface|null $request
47
     * @param LoggerInterface|null $logger PSR3 compatible logger (optional)
48
     */
49 View Code Duplication
    public function __construct(
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...
50
        $accessKey = null,
51
        $userId = null,
52
        $password = null,
53
        $useIntegration = false,
54
        RequestInterface $request = null,
55
        LoggerInterface $logger = null
56
    ) {
57
        if (null !== $request) {
58
            $this->setRequest($request);
59
        }
60
        parent::__construct($accessKey, $userId, $password, $useIntegration, $logger);
61
    }
62
63
    /**
64
     * @param LandedCostRequest $request
65
     * @return SimpleXmlElement
66
     * @throws Exception
67
     */
68
    public function getLandedCosts(LandedCostRequest $request)
69
    {
70
        $request = $this->createRequestLandedCost($request);
71
        $response = $this->sendRequest(
72
            $request, self::ENDPOINT_LANDEDCOST,
73
            'ProcessLCRequest', 'LandedCost'
74
        );
75
76
        if (isset($response->LandedCostResponse->QueryResponse)) {
77
            return $response->LandedCostResponse->QueryResponse;
78
        }
79
80
        return $response->LandedCostResponse->EstimateResponse;
81
    }
82
83
    /**
84
     * Create the LandedCostRequest request.
85
     *
86
     * @param LandedCostRequest $landedCostRequest The request details. Refer to the UPS documentation for available structure
87
     *
88
     * @return string
89
     */
90
    private function createRequestLandedCost(LandedCostRequest $landedCostRequest)
91
    {
92
        $xml = new DOMDocument();
93
        $xml->formatOutput = true;
94
95
        $tradeabilityRequest = $xml->appendChild($xml->createElement('LandedCostRequest'));
96
        $tradeabilityRequest->setAttribute('xml:lang', 'en-US');
97
98
        $request = $tradeabilityRequest->appendChild($xml->createElement('Request'));
99
100
        $node = $xml->importNode($this->createTransactionNode(), true);
101
        $request->appendChild($node);
102
103
        $request->appendChild($xml->createElement('RequestAction', 'LandedCost'));
104
105
        if ($landedCostRequest->getQueryRequest() !== null) {
106
            $tradeabilityRequest->appendChild($landedCostRequest->getQueryRequest()->toNode($xml));
107
        }
108
109
        return $xml->saveXML();
110
    }
111
112
    /**
113
     * Creates and sends a request for the given data. Most errors are handled in SoapRequest
114
     *
115
     * @param string $request
116
     * @param string $endpoint
117
     * @param string $operation
118
     * @param string $wsdl
119
     *
120
     * @throws Exception
121
     *
122
     * @return \stdClass
123
     */
124
    private function sendRequest($request, $endpoint, $operation, $wsdl)
125
    {
126
        $endpointurl = $this->compileEndpointUrl($endpoint);
127
        $this->response = $this->getRequest()->request(
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...
128
            $this->createAccess(), $request, $endpointurl, $operation, $wsdl
0 ignored issues
show
Unused Code introduced by
The call to RequestInterface::request() has too many arguments starting with $operation.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
        );
130
        $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...
131
132
        if (null === $response) {
133
            throw new Exception('Failure (0): Unknown error', 0);
134
        }
135
136
        return $this->formatResponse($response);
137
    }
138
139
    /**
140
     * @return RequestInterface
141
     */
142
    public function getRequest()
143
    {
144
        if (null === $this->request) {
145
            $this->request = new SoapRequest($this->logger);
146
        }
147
148
        return $this->request;
149
    }
150
151
    /**
152
     * @param RequestInterface $request
153
     *
154
     * @return $this
155
     */
156
    public function setRequest(RequestInterface $request)
157
    {
158
        $this->request = $request;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Format the response.
165
     *
166
     * @param SimpleXMLElement $response
167
     *
168
     * @return \stdClass
169
     */
170
    private function formatResponse(SimpleXMLElement $response)
171
    {
172
        return $this->convertXmlObject($response->Body);
173
    }
174
}
175