Completed
Push — master ( aed522...7a9ddd )
by Stefan
03:33
created

Ups::compileEndpointUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ups;
4
5
use DOMDocument;
6
use Exception;
7
use Psr\Log\LoggerAwareInterface;
8
use Psr\Log\LoggerInterface;
9
use SimpleXMLElement;
10
use stdClass;
11
12
abstract class Ups implements LoggerAwareInterface
13
{
14
    const PRODUCTION_BASE_URL = 'https://onlinetools.ups.com/ups.app/xml';
15
    const INTEGRATION_BASE_URL = 'https://wwwcie.ups.com/ups.app/xml';
16
17
    /**
18
     * @var string
19
     */
20
    protected $accessKey;
21
22
    /**
23
     * @var string
24
     */
25
    protected $userId;
26
27
    /**
28
     * @var string
29
     */
30
    protected $password;
31
32
    /**
33
     * @var string
34
     *
35
     * @deprecated
36
     */
37
    protected $productionBaseUrl = 'https://onlinetools.ups.com/ups.app/xml';
38
39
    /**
40
     * @var string
41
     *
42
     * @deprecated
43
     */
44
    protected $integrationBaseUrl = 'https://wwwcie.ups.com/ups.app/xml';
45
46
    /**
47
     * @var bool
48
     */
49
    protected $useIntegration = false;
50
51
    /**
52
     * @var string
53
     */
54
    protected $context;
55
56
    /**
57
     * @deprecated
58
     */
59
    public $response;
60
61
    /**
62
     * @var LoggerInterface
63
     */
64
    protected $logger;
65
66
    /**
67
     * Constructor.
68
     *
69
     * @param string|null $accessKey UPS License Access Key
70
     * @param string|null $userId UPS User ID
71
     * @param string|null $password UPS User Password
72
     * @param bool $useIntegration Determine if we should use production or CIE URLs.
73
     * @param LoggerInterface|null $logger PSR3 compatible logger (optional)
74
     */
75 34
    public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false, LoggerInterface $logger = null)
76
    {
77 34
        $this->accessKey = $accessKey;
78 34
        $this->userId = $userId;
79 34
        $this->password = $password;
80 34
        $this->useIntegration = $useIntegration;
81 34
        $this->logger = $logger;
82 34
    }
83
84
    /**
85
     * Sets the transaction / context value.
86
     *
87
     * @param string $context The transaction "guidlikesubstance" value
88
     */
89 4
    public function setContext($context)
90
    {
91 4
        $this->context = $context;
92 4
    }
93
94
    /**
95
     * @param LoggerInterface $logger
96
     */
97
    public function setLogger(LoggerInterface $logger)
98
    {
99
        $this->logger = $logger;
100
    }
101
102
    /**
103
     * @return LoggerInterface
104
     */
105
    public function getLogger()
106
    {
107
        return $this->logger;
108
    }
109
110
    /**
111
     * Format a Unix timestamp or a date time with a Y-m-d H:i:s format into a YYYYMMDDHHmmss format required by UPS.
112
     *
113
     * @param string
114
     *
115
     * @return string
116
     */
117 4
    public function formatDateTime($timestamp)
118
    {
119 4
        if (!is_numeric($timestamp)) {
120
            $timestamp = strtotime($timestamp);
121
        }
122
123 4
        return date('YmdHis', $timestamp);
124
    }
125
126
    /**
127
     * Create the access request.
128
     *
129
     * @return string
130
     */
131 31
    protected function createAccess()
132
    {
133 31
        $xml = new DOMDocument();
134 31
        $xml->formatOutput = true;
135
136
        // Create the AccessRequest element
137 31
        $accessRequest = $xml->appendChild($xml->createElement('AccessRequest'));
138 31
        $accessRequest->setAttribute('xml:lang', 'en-US');
139
140 31
        $accessRequest->appendChild($xml->createElement('AccessLicenseNumber', $this->accessKey));
141 31
        $accessRequest->appendChild($xml->createElement('UserId', $this->userId));
142
        
143 31
        $p = $accessRequest->appendChild($xml->createElement('Password'));
144 31
        $p->appendChild($xml->createTextNode($this->password));
145
146 31
        return $xml->saveXML();
147
    }
148
149
    /**
150
     * Creates the TransactionReference node for a request.
151
     *
152
     * @return \DomNode
153
     */
154 31
    protected function createTransactionNode()
155
    {
156 31
        $xml = new DOMDocument();
157 31
        $xml->formatOutput = true;
158
159 31
        $trxRef = $xml->appendChild($xml->createElement('TransactionReference'));
160
161 31
        if (null !== $this->context) {
162 4
            $trxRef->appendChild($xml->createElement('CustomerContext', $this->context));
163 4
        }
164
165 31
        return $trxRef->cloneNode(true);
166
    }
167
168
    /**
169
     * Send request to UPS.
170
     *
171
     * @param string $access The access request xml
172
     * @param string $request The request xml
173
     * @param string $endpointurl The UPS API Endpoint URL
174
     *
175
     * @throws Exception
176
     *
177
     * @return SimpleXMLElement
178
     *
179
     * @deprecated Untestable
180
     */
181
    protected function request($access, $request, $endpointurl)
182
    {
183
        $requestInstance = new Request($this->logger);
184
        $response = $requestInstance->request($access, $request, $endpointurl);
185
        if ($response->getResponse() instanceof SimpleXMLElement) {
186
            $this->response = $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...
187
188
            return $response->getResponse();
189
        }
190
191
        throw new Exception('Failure: Response is invalid.');
192
    }
193
194
    /**
195
     * Convert XMLSimpleObject to stdClass object.
196
     *
197
     * @param SimpleXMLElement $xmlObject
198
     *
199
     * @return stdClass
200
     */
201 9
    protected function convertXmlObject(SimpleXMLElement $xmlObject)
202
    {
203 9
        return json_decode(json_encode($xmlObject));
204
    }
205
206
    /**
207
     * Compiles the final endpoint URL for the request.
208
     *
209
     * @param string $segment The URL segment to build in to the endpoint
210
     *
211
     * @return string
212
     */
213 31
    protected function compileEndpointUrl($segment)
214
    {
215 31
        $base = ($this->useIntegration ? $this->integrationBaseUrl : $this->productionBaseUrl);
0 ignored issues
show
Deprecated Code introduced by
The property Ups\Ups::$integrationBaseUrl 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...
Deprecated Code introduced by
The property Ups\Ups::$productionBaseUrl 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...
216
217 31
        return $base . $segment;
218
    }
219
}
220