Completed
Push — revert-7-master-add-line2-to-a... ( 8e332e )
by Ben
02:26
created

Request::buildXml()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 28
c 3
b 0
f 0
nc 10
nop 0
dl 0
loc 46
rs 8.4751
1
<?php namespace SimpleUPS\AddressValidate;
2
3
use \SimpleUPS\Api\MissingParameterException;
4
5
use \SimpleUPS\UPS;
6
7
/**
8
 * A request/response handler for validating a street address
9
 * @internal
10
 * @link https://www.ups.com/upsdeveloperkit/downloadresource?loc=en_US
11
 */
12
class Request extends \SimpleUPS\Api\Request
13
{
14
    private
15
        /* @var Address $address */
16
        $address;
17
18
    /**
19
     * @param null $debug
20
     */
21
    public function __construct($debug = null)
22
    {
23
        parent::__construct($debug);
24
25
        $this->responseClass = false;
26
    }
27
28
    /**
29
     * Determine which API call will be made
30
     * @return string
31
     */
32
    public function getUrl()
33
    {
34
        return $this->getDebug() ? 'https://wwwcie.ups.com/ups.app/xml/XAV' : 'https://onlinetools.ups.com/ups.app/xml/XAV';
35
    }
36
37
    /**
38
     * Build the validate address request
39
     * @return string
40
     * @throws \SimpleUPS\Api\MissingParameterException
41
     */
42
    public function buildXml()
43
    {
44
        if (serialize($this->getAddress()) == serialize(new Address())) {
45
            throw new MissingParameterException(
46
                'Address requires a Country code and a City, a State\Province code or a Postal code'
47
            );
48
        }
49
50
        if ($this->getAddress()->getCountryCode() === null) {
51
            throw new MissingParameterException('Address requires a Country code');
52
        }
53
54
        $dom = new \DomDocument('1.0');
55
        $dom->formatOutput = $this->getDebug();
56
        $dom->appendChild($addressRequest = $dom->createElement('AddressValidationRequest'));
57
        $addressRequestLang = $dom->createAttribute('xml:lang');
58
        $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...
59
        $addressRequest->appendChild($request = $dom->createElement('Request'));
60
        $request->appendChild($transactionReference = $dom->createElement('TransactionReference'));
61
        $transactionReference->appendChild($dom->createElement('CustomerContext', $this->getCustomerContext()));
62
63
        $request->appendChild($dom->createElement('RequestAction', 'XAV'));
64
        $request->appendChild($dom->createElement('RequestOption', '3'));
65
66
        $addressRequest->appendChild($address = $dom->createElement('AddressKeyFormat'));
67
68
        $address->appendChild($dom->createElement('AddressLine', $this->getAddress()->getStreet()));
69
        if ($this->getAddress()->getCity() != null) {
70
            $address->appendChild($dom->createElement('PoliticalDivision2', $this->getAddress()->getCity()));
71
        }
72
73
        if ($this->getAddress()->getStateProvinceCode() != null) {
74
            $address->appendChild(
75
                $dom->createElement('PoliticalDivision1', $this->getAddress()->getStateProvinceCode())
76
            );
77
        }
78
79
        if ($this->getAddress()->getPostalCode() != null) {
80
            $address->appendChild($dom->createElement('PostcodePrimaryLow', $this->getAddress()->getPostalCode()));
81
        }
82
83
        $address->appendChild($dom->createElement('CountryCode', $this->getAddress()->getCountryCode()));
84
        $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...
85
86
        return $xml;
87
    }
88
89
    /**
90
     * @return Response|\SimpleUPS\Api\Response|\SimpleXMLElement
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\AddressValidate\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\AddressValidate\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 Address $address
104
     *
105
     * @return Request
106
     */
107
    public function setAddress(Address $address)
108
    {
109
        $this->address = $address;
110
        return $this;
111
    }
112
113
    /**
114
     * @return Address
115
     */
116
    public function getAddress()
117
    {
118
        return $this->address;
119
    }
120
}