Completed
Pull Request — master (#80)
by
unknown
03:13
created

AddressValidationResponse::noCandidates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 7
loc 7
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php namespace Ups\Entity;
2
3
use Ups\AddressValidation;
4
use Ups\Entity\AddressValidation\AVAddress;
5
use Ups\Entity\AddressValidation\AddressClassification;
6
7
class AddressValidationResponse
8
{
9
    protected $response;
10
    protected $requestAction;
11
12
    /**
13
     * AddressValidationResponse constructor.
14
     * @param \SimpleXMLElement $xmlDocument
15
     * @param $requestAction
16
     */
17 14
    public function __construct(\SimpleXMLElement $xmlDocument, $requestAction)
18
    {
19 14
        $this->response = $xmlDocument;
20 14
        $this->requestAction = $requestAction;
21 14
    }
22
23
    /**
24
     * Tells whether or not the NoCandidatesIndicator is present on the XML document.
25
     * This indicator is returned if the address is so badly formed that UPS is
26
     * unable to even offer any suggested alternatives
27
     *
28
     * @return bool
29
     */
30 5 View Code Duplication
    public function noCandidates()
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...
31 2
    {
32 5
        if (AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) {
33 3
            throw new \BadMethodCallException(__METHOD__ . ' should not be called on Address Classification only requests.');
34 2
        }
35 2
        return isset($this->response->NoCandidatesIndicator);
36 2
    }
37
38
    /**
39
     * Tells whether or not the ValidAddressIndicator is present on the XML document.
40
     * This indicator is present if provided address is valid and represents a
41
     * single, unique address in the UPS Address Validation system.
42
     * @return bool
43
     */
44 3
    public function isValid()
45
    {
46 3
        if (AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) {
47 1
            return $this->response->AddressClassification->Code > 0;
48
        }
49 2
        return isset($this->response->ValidAddressIndicator);
50
    }
51
52
    /**
53
     * Tells whether or not the AmbiguousAddressIndicator is present on the XML document.
54
     * This indicator is present when the address provided is not specific enough to
55
     * be unique to one physical location, but provides enough
56
     *
57
     * @throws \BadMethodCallException
58
     * @return bool
59
     */
60 3 View Code Duplication
    public function isAmbiguous()
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...
61
    {
62 3
        if (AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) {
63 1
            throw new \BadMethodCallException(__METHOD__ . ' should not be called on Address Classification only requests.');
64
        }
65 2
        return isset($this->response->AmbiguousAddressIndicator);
66
    }
67
68
    /**
69
     * @throws \BadMethodCallException
70
     * @return AddressClassification
71
     */
72 2
    public function getAddressClassification()
73
    {
74 2
        if ($this->requestAction < AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION) {
75 1
            throw new \BadMethodCallException('Address Classification was not requested.');
76
        }
77 1
        return new AddressClassification($this->response->AddressClassification);
78
    }
79
80
    /**
81
     * @return \Ups\Entity\AddressValidation\Address[]
82
     */
83 1
    public function getCandidateAddressList()
84
    {
85 1
        if (!isset($this->response->AddressKeyFormat)) {
86
            return [];
87
        }
88 1
        $candidates = [];
89 1
        foreach ($this->response->AddressKeyFormat as $address) {
90 1
            $candidates[] = new AVAddress($address);
91 1
        }
92 1
        return $candidates;
93
    }
94
95 1
    public function getValidatedAddress()
96
    {
97 1
        if ($this->requestAction == AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION) {
98
            throw new \BadMethodCallException('Only Address Classification was requested. There is no address.');
99
        }
100
101 1
        return new AVAddress($this->response->AddressKeyFormat);
102
    }
103
}
104