Request::buildXml()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 24
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 36
rs 8.8571
1
<?php namespace SimpleUPS\Rates;
2
3
use \SimpleUPS\Api\MissingParameterException;
4
5
use \SimpleUPS\UPS;
6
7
/**
8
 * @internal
9
 * @since 1.0
10
 */
11
class Request extends \SimpleUPS\Api\Request
12
{
13
    private
14
        /* @var string $pickupType */
15
        $pickupType = PickupType::DAILY_PICKUP,
16
        /* @var string $rateType */
17
        $rateType,
18
19
        /* @var Shipment $shipment */
20
        $shipment;
21
22
    public function __construct()
23
    {
24
        parent::__construct();
25
26
        $this->responseClass = '\SimpleUPS\Rates\Response';
27
    }
28
29
    /**
30
     * Determine which API call will be made
31
     * @internal
32
     * @return string
33
     */
34
    public function getUrl()
35
    {
36
        return $this->getDebug() ? 'https://wwwcie.ups.com/ups.app/xml/Rate' : 'https://onlinetools.ups.com/ups.app/xml/Rate';
37
    }
38
39
    /**
40
     * Build the validate address request
41
     * @internal
42
     * @return string
43
     * @throws \SimpleUPS\Api\MissingParameterException
44
     */
45
    public function buildXml()
46
    {
47
        if ($this->getShipment()->getDestination() == null) {
48
            throw new MissingParameterException('Shipment destination is missing');
49
        }
50
51
        $dom = new \DomDocument('1.0');
52
        $dom->formatOutput = $this->getDebug();
53
        $dom->appendChild($ratingRequest = $dom->createElement('RatingServiceSelectionRequest'));
54
        $addressRequestLang = $dom->createAttribute('xml:lang');
55
        $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...
56
        $ratingRequest->appendChild($request = $dom->createElement('Request'));
57
        $request->appendChild($transactionReference = $dom->createElement('TransactionReference'));
58
        $transactionReference->appendChild($dom->createElement('CustomerContext', $this->getCustomerContext()));
59
60
        $request->appendChild($dom->createElement('RequestAction', 'Rate'));
61
        $request->appendChild(
62
            $dom->createElement('RequestOption', 'Shop')
63
        ); //@todo test with "Rate" as setting to determine difference
64
65
        $ratingRequest->appendChild($pickupType = $dom->createElement('PickupType'));
66
        $pickupType->appendChild($shipmentType = $dom->createElement('Code', $this->getPickupType()));
67
68
        if ($this->getRateType() != null) {
69
            $ratingRequest->appendChild($customerClassification = $dom->createElement('CustomerClassification'));
70
            $customerClassification->appendChild($dom->createElement('Code', $this->getRateType()));
71
        }
72
        // Shipment
73
        $shipment = $this->getShipment();
74
        $shipment->setShipper(UPS::getShipper());
75
76
        $ratingRequest->appendChild($shipment->toXml($dom));
77
        $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...
78
79
        return $xml;
80
    }
81
82
    /**
83
     * How the shipment will be picked up
84
     * Default value is PickupType::DAILY_PICKUP
85
     * @see PickupType
86
     *
87
     * @param string $pickupType
88
     *
89
     * @throws \SimpleUPS\Api\InvalidParameterException
90
     * @return Request
91
     */
92 View Code Duplication
    public function setPickupType($pickupType)
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
        $reflectionClass = new \ReflectionClass('SimpleUPS\Rates\PickupType');
95
        if (!in_array($pickupType, $reflectionClass->getConstants())) {
96
            throw new \SimpleUPS\Api\InvalidParameterException(
97
                'PickupType is invalid, refer to SimpleUPS\Rates\PickupType for valid pickup types'
98
            );
99
        }
100
101
        $this->pickupType = $pickupType;
102
        return $this;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    private function getPickupType()
109
    {
110
        return $this->pickupType;
111
    }
112
113
    /**
114
     * How the shipment will be quoted
115
     * Defaults:
116
     * <ul>
117
     *  <li>RateType::DAILY_RATES when pickup type is PickupType::DAILY_PICKUP</li>
118
     *  <li>RateType::RETAIL_RATES when pickup type is PickupType::ONE_TIME_PICKUP, PickupType::ON_CALL_AIR, PickupType::LETTER_CENTER or PickupType::AIR_SERVICE_CENTER</li>
119
     * </ul>
120
     * @see PickupType
121
     * @see RateType
122
     *
123
     * @param string $rateType
124
     *
125
     * @throws \SimpleUPS\Api\InvalidParameterException
126
     * @return Request
127
     */
128 View Code Duplication
    public function setRateType($rateType)
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...
129
    {
130
        $reflectionClass = new \ReflectionClass('SimpleUPS\Rates\RateType');
131
        if (!in_array($rateType, $reflectionClass->getConstants())) {
132
            throw new \SimpleUPS\Api\InvalidParameterException(
133
                'RateType is invalid, refer to SimpleUPS\Rates\RateType for valid rate types'
134
            );
135
        }
136
137
        $this->rateType = $rateType;
138
        return $this;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    private function getRateType()
145
    {
146
        return $this->rateType;
147
    }
148
149
    /**
150
     * @param Shipment $shipment
151
     *
152
     * @return Request
153
     */
154
    public function setShipment(Shipment $shipment)
155
    {
156
        $this->shipment = $shipment;
157
        return $this;
158
    }
159
160
    /**
161
     * @return Shipment
162
     */
163
    public function getShipment()
164
    {
165
        return $this->shipment;
166
    }
167
}