Request::buildXml()   C
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 29
c 1
b 0
f 0
nc 11
nop 0
dl 0
loc 48
rs 6.7272
1
<?php namespace SimpleUPS\RegionValidate;
2
3
use \SimpleUPS\Api\MissingParameterException;
4
5
use \SimpleUPS\Address;
6
7
use \SimpleUPS\UPS;
8
9
/**
10
 * @internal
11
 */
12
class Request extends \SimpleUPS\Api\Request
13
{
14
    private
15
        /* @var Address $address */
16
        $address;
17
18
    public function __construct($debug = null)
19
    {
20
        parent::__construct($debug);
21
22
        $this->responseClass = false;
23
    }
24
25
    /**
26
     * Determine which API call will be made
27
     * @return string
28
     */
29
    public function getUrl()
30
    {
31
        return $this->getDebug() ? 'https://wwwcie.ups.com/ups.app/xml/AV' : 'https://onlinetools.ups.com/ups.app/xml/AV';
32
    }
33
34
    /**
35
     * Build the validate address request
36
     * @return string
37
     * @throws \SimpleUPS\Api\MissingParameterException
38
     */
39
    public function buildXml()
40
    {
41
        if (serialize($this->getAddress()) == serialize(new Address())) {
42
            throw new MissingParameterException(
43
                'Address requires a Country code, Postal code, and either a City or State\Province code'
44
            );
45
        }
46
47
        if ($this->getAddress()->getCountryCode() === null) {
48
            throw new MissingParameterException('Address requires a Country code');
49
        }
50
51
        if ($this->getAddress()->getPostalCode() === null) {
52
            throw new MissingParameterException('Address requires a Postal code');
53
        }
54
55
        $dom = new \DomDocument('1.0');
56
        $dom->formatOutput = $this->getDebug();
57
        $dom->appendChild($addressRequest = $dom->createElement('AddressValidationRequest'));
58
        $addressRequestLang = $dom->createAttribute('xml:lang');
59
        $addressRequestLang->value = parent::getXmlLang();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getXmlLang() instead of buildXml()). Are you sure this is correct? If so, you might want to change this to $this->getXmlLang().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
60
        $addressRequest->appendChild($request = $dom->createElement('Request'));
61
        $request->appendChild($transactionReference = $dom->createElement('TransactionReference'));
62
        $transactionReference->appendChild($dom->createElement('CustomerContext', $this->getCustomerContext()));
63
        $transactionReference->appendChild($dom->createElement('XpciVersion', $this->getXpciVersion()));
64
65
        $request->appendChild($dom->createElement('RequestAction', 'AV'));
66
        $addressRequest->appendChild($address = $dom->createElement('Address'));
67
68
        if ($this->getAddress()->getCity() != null) {
69
            $address->appendChild($dom->createElement('City', $this->getAddress()->getCity()));
70
        }
71
72
        if ($this->getAddress()->getStateProvinceCode() != null) {
73
            $address->appendChild(
74
                $dom->createElement('StateProvinceCode', $this->getAddress()->getStateProvinceCode())
75
            );
76
        }
77
78
        if ($this->getAddress()->getPostalCode() != null) {
79
            $address->appendChild($dom->createElement('PostalCode', $this->getAddress()->getPostalCode()));
80
        }
81
        $address->appendChild($dom->createElement('CountryCode', $this->getAddress()->getCountryCode()));
82
83
        $xml = parent::buildAuthenticationXml() . $dom->saveXML();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (buildAuthenticationXml() instead of buildXml()). Are you sure this is correct? If so, you might want to change this to $this->buildAuthenticationXml().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
84
85
        return $xml;
86
    }
87
88
    /**
89
     * Send a request to the API and get region rankings for the provided address
90
     * @return Response
91
     */
92 View Code Duplication
    public function sendRequest()
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...
93
    {
94
        $xml = parent::sendRequest();
95
96
        $responseClass = new \SimpleUPS\RegionValidate\Response($this->getAddress());
97
        $response = $responseClass->fromXml($xml);
0 ignored issues
show
Bug introduced by
It seems like $xml defined by parent::sendRequest() on line 94 can also be of type object<SimpleUPS\Api\Response>; however, SimpleUPS\RegionValidate\Response::fromXml() does only seem to accept object<SimpleXMLElement>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
98
99
        return $response;
100
    }
101
102
    /**
103
     * @param \SimpleUPS\Address $address
104
     */
105
    public function setAddress(Address $address)
106
    {
107
        $this->address = $address;
108
    }
109
110
    /**
111
     * @return Address
112
     */
113
    public function getAddress()
114
    {
115
        return $this->address;
116
    }
117
}