UPS   D
last analyzed

Complexity

Total Complexity 56

Size/Duplication

Total Lines 597
Duplicated Lines 3.69 %

Coupling/Cohesion

Components 7
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 597
rs 4.2857
wmc 56
lcom 7
cbo 7

26 Methods

Rating   Name   Duplication   Size   Complexity  
A getRates() 0 17 2
A isValidRegion() 0 4 1
A getSuggestedRegions() 0 4 1
A trackByTrackingNumber() 0 11 2
A isValidAddress() 0 4 1
A getCorrectedAddress() 0 4 1
A getSuggestedAddresses() 0 4 1
A _performAddressValidation() 11 11 3
A _performRegionValidation() 11 11 3
A setAuthentication() 0 6 1
A setAccountNumber() 0 4 1
A getAccountNumber() 0 8 3
A setAccessLicenseNumber() 0 4 1
A getAccessLicenseNumber() 0 8 3
A setPassword() 0 4 1
A getPassword() 0 8 3
A setUserId() 0 4 1
A getUserId() 0 8 3
A setShipper() 0 4 1
F getShipper() 0 49 12
A setCurrencyCode() 0 4 1
A getCurrencyCode() 0 8 2
A setDebug() 0 4 1
A getDebug() 0 12 3
A getDebugOutput() 0 8 2
A load() 0 20 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like UPS often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UPS, and based on these observations, apply Extract Interface, too.

1
<?php namespace SimpleUPS;
2
3
/**
4
 * This is the SimpleUPS PHP library
5
 * The SimpleUPS PHP Library is designed to simplify the UPS API.  It is intended
6
 * for Small Businesses and includes support for Address/Region Validation & Correction and
7
 * Small Package Tracking & Rates.+
8
 * @author      BenKuhl <[email protected]>
9
 * @license     http://www.simpleups.io/license
10
 * @link        https://www.ups.com/upsdeveloperkit UPS Developer Kit
11
 * @since       1.0
12
 */
13
class UPS
14
{
15
16
    private static
17
18
        /* @var null $accountNumber */
19
        $accountNumber,
20
21
        /* @var null $accessLicenseNumber */
22
        $accessLicenseNumber,
23
24
        /* @var null $userId */
25
        $userId,
26
27
        /* @var null $shipperNumber */
28
        $shipperNumber,
29
30
        /* @var null $password */
31
        $password,
32
33
        /* @var null $debug */
34
        $debug,
35
36
        /* @var \SimpleUPS\AddressValidate\Response $addressValidationResponse */
37
        $addressValidationResponse,
38
39
        /* @var \SimpleUPS\RegionValidate\Response $regionValidationResponse */
40
        $regionValidationResponse,
41
42
        /* @var Shipper $shipper */
43
        $shipper,
44
45
        /* @var string $currencyCode */
46
        $currencyCode = 'USD';
47
48
    public static
49
        /* @var Object UPS::$request The last request object */
50
        $request,
51
52
        /* @var string UPS::$response The last response xml */
53
        $response,
54
55
        /* @var string UPS::$libraryName The name of this library */
56
        $libraryName = 'SimpleUPS';
57
58
    /**
59
     * @api
60
     * @since 1.0
61
     * @throws \SimpleUPS\Api\InvalidResponseException
62
     * @throws \SimpleUPS\Api\RequestFailedException
63
     * @throws \SimpleUPS\Api\ResponseErrorException
64
     * @throws \SimpleUPS\Api\MissingParameterException
65
     * @throws \SimpleUPS\Api\InvalidParameterException
66
     */
67
    public static function getRates(
68
        Rates\Shipment $shipment,
69
        $pickupType = Rates\PickupType::DAILY_PICKUP,
70
        $rateType = null
71
    ) {
72
        $request = new Rates\Request();
73
        $request->setShipment($shipment);
74
        $request->setPickupType($pickupType);
75
76
        if ($rateType != null) {
77
            $request->setRateType($rateType);
78
        }
79
80
        $response = $request->sendRequest();
81
82
        return $response->getShippingMethods();
83
    }
84
85
    /**
86
     * Determine if various combinations of city, state/province, postal code and country code are valid.
87
     * If an address is invalid while the region is, then the invalid portion of the address is the street.  Also
88
     * assists with determining if city is misspelled or state doesn't match the postal code.
89
     * Address fields used include:
90
     * <ul>
91
     *  <li>city</li>
92
     *  <li>stateProvinceCode</li>
93
     *  <li>postalCode - REQUIRED</li>
94
     *  <li>countryCode - REQUIRED</li>
95
     * </ul>
96
     * @note    If a postal code is missing, the region is not accurate enough and thus will always return false.
97
     * @see     \SimpleUPS\UPS::isValidAddress()
98
     * @see     \SimpleUPS\UPS::getSuggestedRegions()
99
     *
100
     * @param Address $address
101
     *
102
     * @return bool
103
     * @example "http://ups.local/examples/SimpleUPS.isValidRegion()_valid.phps" A valid region
104
     * @example "http://ups.local/examples/SimpleUPS.isValidRegion()_invalid.phps" An invalid region
105
     * @example "http://ups.local/examples/SimpleUPS.isValidRegion()_with_isValidAddress().phps" An invalid region
106
     * @api
107
     * @since   1.0
108
     * @throws \SimpleUPS\Api\InvalidResponseException
109
     * @throws \SimpleUPS\Api\RequestFailedException
110
     * @throws \SimpleUPS\Api\ResponseErrorException
111
     * @throws \SimpleUPS\Api\MissingParameterException
112
     */
113
    public static function isValidRegion(Address $address)
114
    {
115
        return self::_performRegionValidation($address)->isRegionValid();
0 ignored issues
show
Bug introduced by
The method isRegionValid() does not seem to exist on object<SimpleUPS\AddressValidate\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
    }
117
118
    /**
119
     * Get the suggested regions available for an invalid region.
120
     * Suggested regions are ordered by a "rank" that UPS provides based on estimated
121
     * accuracy.
122
     * A suggested region may be flagged as "corrected" and have a quality rating of 1
123
     * if there are minor mistakes such as a misspelled city or missing information that
124
     * UPS is able to correct.
125
     * @see   \SimpleUPS\UPS::isValidRegion()
126
     *
127
     * @param Address $address
128
     *
129
     * @return \SimpleUPS\RegionValidate\RegionSuggestion[]|null
130
     * @api
131
     * @since 1.0
132
     * @throws \SimpleUPS\Api\InvalidResponseException
133
     * @throws \SimpleUPS\Api\RequestFailedException
134
     * @throws \SimpleUPS\Api\ResponseErrorException
135
     * @throws \SimpleUPS\Api\MissingParameterException
136
     */
137
    public static function getSuggestedRegions(Address $address)
138
    {
139
        return self::_performRegionValidation($address)->getSuggestedRegions();
0 ignored issues
show
Bug introduced by
The method getSuggestedRegions() does not seem to exist on object<SimpleUPS\AddressValidate\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
    }
141
142
    /**
143
     * Track a small package by tracking number
144
     *
145
     * @param string $trackingNumber
146
     *
147
     * @return \SimpleUPS\Track\SmallPackage\Shipment[]|null
148
     * @api
149
     * @since 1.0
150
     * @throws \SimpleUPS\Api\InvalidResponseException
151
     * @throws \SimpleUPS\Api\RequestFailedException
152
     * @throws \SimpleUPS\Api\ResponseErrorException
153
     * @throws \SimpleUPS\Api\MissingParameterException
154
     * @throws \SimpleUPS\Api\NoTrackingInformationException
155
     * @throws \SimpleUPS\Track\TrackingNumberNotFoundException
156
     */
157
    public static function trackByTrackingNumber($trackingNumber)
158
    {
159
        if (!is_numeric($trackingNumber{0})) {
160
            throw new \SimpleUPS\Api\MissingParameterException('Tracking number is invalid, is it a reference number?');
161
        }
162
163
        $request = new Track\SmallPackage\Request();
164
        $request->setTrackingNumber($trackingNumber);
165
166
        return $request->sendRequest()->getShipments();
167
    }
168
169
    /**
170
     * Determine if provided street address is valid.
171
     * If an address is invalid while the region is, then the invalid portion of the address is the street.  Also
172
     * assists with determining if city is misspelled or state doesn't match the postal code.
173
     * UPS will auto correct certain things about an address.  For example, if the state is "XY"
174
     * and the zip code is correct, UPS will ignore the state completely.  However, UPS will provide
175
     * a fully corrected address.  It's advisable you use this address.
176
     * @see   getCorrectedAddress
177
     * @see   getSuggestedAddresses
178
     * @see   isValidRegion
179
     *
180
     * @param Address $address
181
     *
182
     * @return bool
183
     * @api
184
     * @since 1.0
185
     * @throws \SimpleUPS\Api\InvalidResponseException
186
     * @throws \SimpleUPS\Api\RequestFailedException
187
     * @throws \SimpleUPS\Api\ResponseErrorException
188
     * @throws \SimpleUPS\Api\MissingParameterException
189
     */
190
    public static function isValidAddress(Address $address)
191
    {
192
        return self::_performAddressValidation($address)->isAddressValid();
0 ignored issues
show
Bug introduced by
The method isAddressValid() does not seem to exist on object<SimpleUPS\AddressValidate\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
193
    }
194
195
    /**
196
     * Take an address and provide a corrected, fully valid address
197
     * UPS will correct a slightly invalid address for you if it is able to determine the real
198
     * address.  For example, if the state is "XY" and the zip code is correct, UPS will provide
199
     * a correct address where the state matches the zip code.  It's advisable under all circumstances
200
     * you use this address when it's provided.
201
     * It is recommended that you always check the validity of an address before obtaining it's
202
     * corrected version.
203
     * @see   isValidAddress
204
     * @see   getSuggestedAddresses
205
     *
206
     * @param Address $address
207
     *
208
     * @return \SimpleUPS\AddressValidate\Address|null
209
     * @api
210
     * @since 1.0
211
     * @throws \SimpleUPS\Api\InvalidResponseException
212
     * @throws \SimpleUPS\Api\RequestFailedException
213
     * @throws \SimpleUPS\Api\ResponseErrorException
214
     * @throws \SimpleUPS\Api\MissingParameterException
215
     */
216
    public static function getCorrectedAddress(Address $address)
217
    {
218
        return self::_performAddressValidation($address)->getCorrectedAddress();
0 ignored issues
show
Bug introduced by
The method getCorrectedAddress() does not seem to exist on object<SimpleUPS\AddressValidate\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
219
    }
220
221
    /**
222
     * If a address is invalid, UPS may provide suggestions for what the correct address is.
223
     * If there are errors in a street address such as the street number not matching an actual
224
     * address UPS may provide some suggestions.
225
     * @note  Suggested addresses can sometimes be grouped together.  For instance if you enter a street number that is significantly off from what's available on the street, the suggested addresses will be grouped by the hundred. (ie: 10900-10998 MY STREET DR)
226
     * @see   isValidAddress
227
     * @see   getCorrectedAddress
228
     *
229
     * @param Address $address
230
     *
231
     * @return \SimpleUPS\AddressValidate\Address[]|null
232
     * @api
233
     * @since 1.0
234
     * @throws \SimpleUPS\Api\InvalidResponseException
235
     * @throws \SimpleUPS\Api\RequestFailedException
236
     * @throws \SimpleUPS\Api\ResponseErrorException
237
     * @throws \SimpleUPS\Api\MissingParameterException
238
     */
239
    public static function getSuggestedAddresses(Address $address)
240
    {
241
        return self::_performAddressValidation($address)->getSuggestedAddresses();
0 ignored issues
show
Bug introduced by
The method getSuggestedAddresses() does not seem to exist on object<SimpleUPS\AddressValidate\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
242
    }
243
244
    /**
245
     * Perform validation on the provided address if not already performed
246
     *
247
     * @param Address $address
248
     *
249
     * @internal
250
     * @return \SimpleUPS\AddressValidate\Request
251
     * @throws \SimpleUPS\Api\InvalidResponseException
252
     * @throws \SimpleUPS\Api\RequestFailedException
253
     * @throws \SimpleUPS\Api\ResponseErrorException
254
     * @throws \SimpleUPS\Api\MissingParameterException
255
     */
256 View Code Duplication
    private static function _performAddressValidation(Address $address)
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...
257
    {
258
        if (self::$addressValidationResponse === null || self::$addressValidationResponse->getAddress() != $address) {
259
            $request = new AddressValidate\Request();
260
            $request->setAddress($address);
0 ignored issues
show
Compatibility introduced by
$address of type object<SimpleUPS\Address> is not a sub-type of object<SimpleUPS\AddressValidate\Address>. It seems like you assume a child class of the class SimpleUPS\Address to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
261
262
            self::$addressValidationResponse = $request->sendRequest();
263
        }
264
265
        return self::$addressValidationResponse;
266
    }
267
268
    /**
269
     * Perform validation on the provided region of the address if not already performed
270
     *
271
     * @param Address $address
272
     *
273
     * @internal
274
     * @return \SimpleUPS\AddressValidate\Request
275
     * @throws \SimpleUPS\Api\InvalidResponseException
276
     * @throws \SimpleUPS\Api\RequestFailedException
277
     * @throws \SimpleUPS\Api\ResponseErrorException
278
     * @throws \SimpleUPS\Api\MissingParameterException
279
     */
280 View Code Duplication
    private static function _performRegionValidation(Address $address)
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...
281
    {
282
        if (self::$regionValidationResponse === null || self::$regionValidationResponse->getAddress() != $address) {
283
            $request = new RegionValidate\Request();
284
            $request->setAddress($address);
285
286
            self::$regionValidationResponse = $request->sendRequest();
287
        }
288
289
        return self::$regionValidationResponse;
290
    }
291
292
    /**
293
     * Set the UPS Access License Number, User ID, Password and Account Number.
294
     * Can also be set with the constants UPS_ACCESSLICENSENUMBER, UPS_USERID, UPS_PASSWORD
295
     * @see   \SimpleUPS\UPS::getAccountNumber()
296
     * @see   \SimpleUPS\UPS::getAccessLicenseNumber()
297
     * @see   \SimpleUPS\UPS::getUserId()
298
     * @see   \SimpleUPS\UPS::getPassword()
299
     * @api
300
     * @since 1.0
301
     *
302
     * @param string $accessLicenseNumber
303
     * @param string $userId
304
     * @param string $password
305
     *
306
     * @throws \Exception
307
     */
308
    public static function setAuthentication($accessLicenseNumber, $userId, $password)
309
    {
310
        self::setAccessLicenseNumber($accessLicenseNumber);
311
        self::setUserId($userId);
312
        self::setPassword($password);
313
    }
314
315
    /**
316
     * @internal
317
     *
318
     * @param $accountNumber
319
     */
320
    private static function setAccountNumber($accountNumber)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
321
    {
322
        self::$accountNumber = $accountNumber;
323
    }
324
325
    /**
326
     * Get the account number to be used in API requests
327
     * Can also be set with the constant UPS_ACCOUNTNUMBER
328
     * @see   \SimpleUPS\UPS::setAuthentication()
329
     * @see   \SimpleUPS\UPS::getAccessLicenseNumber()
330
     * @see   \SimpleUPS\UPS::getUserId()
331
     * @see   \SimpleUPS\UPS::getPassword()
332
     * @api
333
     * @since 1.0
334
     * @return string|null
335
     */
336
    public static function getAccountNumber()
337
    {
338
        if (self::$accountNumber == null && defined('UPS_ACCOUNTNUMBER')) {
339
            self::$accountNumber = UPS_ACCOUNTNUMBER;
340
        }
341
342
        return self::$accountNumber;
343
    }
344
345
    /**
346
     * @param $accessLicenseNumber
347
     *
348
     * @internal
349
     */
350
    private static function setAccessLicenseNumber($accessLicenseNumber)
351
    {
352
        self::$accessLicenseNumber = $accessLicenseNumber;
353
    }
354
355
    /**
356
     * Get the license number to be used in API requests
357
     * Can also be set with the constant UPS_ACCESSLICENSENUMBER
358
     * @see   \SimpleUPS\UPS::setAuthentication()
359
     * @see   \SimpleUPS\UPS::getAccountNumber()
360
     * @see   \SimpleUPS\UPS::getUserId()
361
     * @see   \SimpleUPS\UPS::getPassword()
362
     * @api
363
     * @since 1.0
364
     * @return string|null
365
     */
366
    public static function getAccessLicenseNumber()
367
    {
368
        if (self::$accessLicenseNumber == null && defined('UPS_ACCESSLICENSENUMBER')) {
369
            self::$accessLicenseNumber = UPS_ACCESSLICENSENUMBER;
370
        }
371
372
        return self::$accessLicenseNumber;
373
    }
374
375
    /**
376
     * @param $password
377
     *
378
     * @internal
379
     */
380
    private static function setPassword($password)
381
    {
382
        self::$password = $password;
383
    }
384
385
    /**
386
     * Get the password to be used in API requests
387
     * Can also be set with the constant UPS_PASSWORD
388
     * @see   \SimpleUPS\UPS::setAuthentication()
389
     * @see   \SimpleUPS\UPS::getAccountNumber()
390
     * @see   \SimpleUPS\UPS::getAccessLicenseNumber()
391
     * @api
392
     * @since 1.0
393
     * @return string|null
394
     */
395
    public static function getPassword()
396
    {
397
        if (self::$password == null && defined('UPS_PASSWORD')) {
398
            self::$password = UPS_PASSWORD;
399
        }
400
401
        return self::$password;
402
    }
403
404
    /**
405
     * @param $userId
406
     *
407
     * @internal
408
     */
409
    private static function setUserId($userId)
410
    {
411
        self::$userId = $userId;
412
    }
413
414
    /**
415
     * Get the user id to be used in API requests
416
     * Can also be set with the constant UPS_USERID
417
     * @see   \SimpleUPS\UPS::setAuthentication()
418
     * @see   \SimpleUPS\UPS::getAccountNumber()
419
     * @see   \SimpleUPS\UPS::getAccessLicenseNumber()
420
     * @see   \SimpleUPS\UPS::getPassword()
421
     * @api
422
     * @since 1.0
423
     * @return string|null
424
     */
425
    public static function getUserId()
426
    {
427
        if (self::$userId == null && defined('UPS_USERID')) {
428
            self::$userId = UPS_USERID;
429
        }
430
431
        return self::$userId;
432
    }
433
434
    /**
435
     * Number to use in rate requests
436
     * Can also be set with the constants UPS_SHIPPERNUMBER
437
     * @api
438
     *
439
     * @param Shipper $shipper
440
     */
441
    public static function setShipper(Shipper $shipper)
442
    {
443
        self::$shipper = $shipper;
444
    }
445
446
    /**
447
     * Shipper information used in rate requests
448
     * @todo  document how to set this via objects & constants
449
     * @api
450
     * @since 1.0
451
     * @return Shipper
452
     */
453
    public static function getShipper()
454
    {
455
        if (self::$shipper == null) {
456
            self::$shipper = new Shipper();
457
458
            if (self::$shipperNumber == null && defined('UPS_SHIPPER_NUMBER')) {
459
                self::$shipper->setNumber(UPS_SHIPPER_NUMBER);
460
            }
461
462
463
            $shipperAddress = new InstructionalAddress();
464
465
            if (defined('UPS_SHIPPER_ADDRESSEE')) {
466
                $shipperAddress->setAddressee(UPS_SHIPPER_ADDRESSEE);
467
            }
468
469
            if (defined('UPS_SHIPPER_STREET')) {
470
                $shipperAddress->setStreet(UPS_SHIPPER_STREET);
471
            }
472
473
            if (defined('UPS_SHIPPER_ADDRESS_LINE2')) {
474
                $shipperAddress->setAddressLine2(UPS_SHIPPER_ADDRESS_LINE2);
475
            }
476
477
            if (defined('UPS_SHIPPER_ADDRESS_LINE3')) {
478
                $shipperAddress->setAddressLine3(UPS_SHIPPER_ADDRESS_LINE3);
479
            }
480
481
            if (defined('UPS_SHIPPER_CITY')) {
482
                $shipperAddress->setCity(UPS_SHIPPER_CITY);
483
            }
484
485
            if (defined('UPS_SHIPPER_STATEPROVINCE_CODE')) {
486
                $shipperAddress->setState(UPS_SHIPPER_STATEPROVINCE_CODE);
0 ignored issues
show
Bug introduced by
The method setState() does not exist on SimpleUPS\InstructionalAddress. Did you maybe mean setStateProvinceCode()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
487
            }
488
489
            if (defined('UPS_SHIPPER_POSTAL_CODE')) {
490
                $shipperAddress->setPostalCode(UPS_SHIPPER_POSTAL_CODE);
491
            }
492
493
            if (defined('UPS_SHIPPER_COUNTRY_CODE')) {
494
                $shipperAddress->setCountryCode(UPS_SHIPPER_COUNTRY_CODE);
495
            }
496
497
            self::$shipper->setAddress($shipperAddress);
498
        }
499
500
        return self::$shipper;
501
    }
502
503
    /**
504
     * Set the currency code to be used
505
     * @api
506
     *
507
     * @param string $currencyCode
508
     */
509
    public static function setCurrencyCode($currencyCode)
510
    {
511
        self::$currencyCode = $currencyCode;
512
    }
513
514
    /**
515
     * Get the currency code
516
     * @since 1.0
517
     * @return string
518
     */
519
    public static function getCurrencyCode()
520
    {
521
        if (defined('UPS_CURRENCY_CODE')) {
522
            self::setCountryCode(UPS_CURRENCY_CODE);
0 ignored issues
show
Bug introduced by
The method setCountryCode() does not seem to exist on object<SimpleUPS\UPS>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
523
        }
524
525
        return self::$currencyCode;
526
    }
527
528
    /**
529
     * Define debug mode
530
     * Can also be set with the constant UPS_DEBUG
531
     * @note  While the library is in debug mode, it uses more memory storing additional information about each request.
532
     * @see   \SimpleUPS\UPS::getDebugOutput()
533
     * @api
534
     * @since 1.0
535
     *
536
     * @param bool $debug
537
     */
538
    public static function setDebug($debug)
539
    {
540
        self::$debug = $debug;
541
    }
542
543
    /**
544
     * Determine if the library is in debug mode
545
     * @see   \SimpleUPS\UPS::getDebugOutput()
546
     * @see   \SimpleUPS\UPS::getDebug()
547
     * @see   \SimpleUPS\UPS::setDebug()
548
     * @since 1.0
549
     * @return bool
550
     */
551
    public static function getDebug()
552
    {
553
        if (self::$debug == null) {
554
            self::$debug = false;
555
556
            if (defined('UPS_DEBUG')) {
557
                self::$debug = UPS_DEBUG;
558
            }
559
        }
560
561
        return !!self::$debug;
562
    }
563
564
    //@todo Add logging method here that could be overridden for frameworks like Yii
565
566
    /**
567
     * Prints debug output
568
     * @see   \SimpleUPS\UPS::getDebug()
569
     * @see   \SimpleUPS\UPS::setDebug()
570
     * @api
571
     * @since 1.0
572
     */
573
    public static function getDebugOutput()
574
    {
575
        if (UPS::getDebug()) {
576
            require_once 'debug_output.inc';
577
        } else {
578
            throw new \Exception('Debug mode must be enabled');
579
        }
580
    }
581
582
    /**
583
     * Used as the autoloader to load classes as they're needed
584
     *
585
     * @param $class
586
     *
587
     * @internal
588
     */
589
    public static function load($class)
590
    {
591
        $path = realpath(
592
            __DIR__ . str_replace(
593
                array(
594
                    __NAMESPACE__,
595
                    "\\"
596
                ),
597
                array(
598
                    '',
599
                    DIRECTORY_SEPARATOR
600
                ),
601
                $class
602
            ) . '.php'
603
        );
604
605
        if ($path !== false) {
606
            require_once $path;
607
        }
608
    }
609
}
610
611
spl_autoload_register(__NAMESPACE__ . '\UPS::load');