GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Customization::findCustomizedItem()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 6.7272
cc 7
eloc 12
nc 5
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2013-2014 eBay Enterprise, Inc.
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the Open Software License (OSL 3.0)
8
 * that is bundled with this package in the file LICENSE.md.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * @copyright   Copyright (c) 2013-2015 eBay Enterprise, Inc. (http://www.ebayenterprise.com/)
13
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
14
 */
15
16
namespace eBayEnterprise\RetailOrderManagement\Payload\Order;
17
18
use eBayEnterprise\RetailOrderManagement\Payload\IPayload;
19
use eBayEnterprise\RetailOrderManagement\Payload\IPayloadMap;
20
use eBayEnterprise\RetailOrderManagement\Payload\ISchemaValidator;
21
use eBayEnterprise\RetailOrderManagement\Payload\IValidatorIterator;
22
use eBayEnterprise\RetailOrderManagement\Payload\PayloadFactory;
23
use eBayEnterprise\RetailOrderManagement\Payload\TPayload;
24
use Psr\Log\LoggerInterface;
25
use Psr\Log\NullLogger;
26
27
class Customization implements ICustomization
28
{
29
    use TPayload, TCustomizationInstructionContainer;
30
31
    const ROOT_NODE = 'Customization';
32
33
    /** @var int */
34
    protected $customizationId;
35
    /** @var IPriceGroup */
36
    protected $extendedPrice;
37
    /** @var string */
38
    protected $itemId;
39
    /** @var string */
40
    protected $customizedItemId;
41
    /** @var IOrderItem */
42
    protected $customizedItem;
43
44
    /**
45
     * @param IValidatorIterator
46
     * @param ISchemaValidator
47
     * @param IPayloadMap
48
     * @param LoggerInterface
49
     * @param IPayload
50
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
51
     */
52
    public function __construct(
53
        IValidatorIterator $validators,
54
        ISchemaValidator $schemaValidator,
0 ignored issues
show
Unused Code introduced by
The parameter $schemaValidator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
        IPayloadMap $payloadMap,
56
        LoggerInterface $logger,
57
        IPayload $parentPayload = null
58
    ) {
59
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger of type object<Psr\Log\LoggerInterface> is incompatible with the declared type object<eBayEnterprise\Re...ayload\LoggerInterface> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
        $this->validators = $validators;
61
        $this->payloadMap = $payloadMap;
62
        $this->parentPayload = $parentPayload;
63
        $this->payloadFactory = new PayloadFactory;
64
65
        $this->extractionPaths = [
66
            'itemId' => 'string(x:ItemId)',
67
        ];
68
        $this->optionalExtractionPaths = [
69
            'customizationId' => 'x:CustomizationId',
70
            'customizedItemId' => 'x:Item/@ref',
71
        ];
72
        $this->subpayloadExtractionPaths = [
73
            'customizationInstructions' => 'x:Instructions',
74
        ];
75
76
        $this->customizationInstructions = $this->buildPayloadForInterface(
77
            self::CUSTOMIZATION_INSTRUCTION_ITERABLE_INTERFACE
78
        );
79
    }
80
81
    public function getEmptyExtendedPrice()
82
    {
83
        return $this->buildPayloadForInterface(self::PRICE_GROUP_INTERFACE);
84
    }
85
86
    public function getCustomizationId()
87
    {
88
        return $this->customizationId;
89
    }
90
91
    public function setCustomizationId($customizationId)
92
    {
93
        $this->customizationId = $customizationId;
94
        return $this;
95
    }
96
97
    public function getExtendedPrice()
98
    {
99
        return $this->extendedPrice;
100
    }
101
102
    public function setExtendedPrice(IPriceGroup $extendedPrice)
103
    {
104
        $this->extendedPrice = $extendedPrice;
105
        return $this;
106
    }
107
108
    public function getItemId()
109
    {
110
        return $this->itemId;
111
    }
112
113
    public function setItemId($itemId)
114
    {
115
        $this->itemId = $this->cleanString($itemId, 20);
116
        return $this;
117
    }
118
119
    public function getCustomizedItem()
120
    {
121
        if (!$this->customizedItem) {
122
            $this->customizedItem = $this->findCustomizedItem();
123
        }
124
        return $this->customizedItem;
125
    }
126
127
    public function setCustomizedItem(IOrderItem $customizedItem)
128
    {
129
        $this->customizedItem = $customizedItem;
130
        $orderItemContainer = $this->getOrderItemContainer();
131
        if ($orderItemContainer) {
132
            $orderItemContainer->getOrderItems()->offsetSet($customizedItem);
133
        }
134
        return $this;
135
    }
136
137
    public function getCustomizedItemId()
138
    {
139
        $item = $this->getCustomizedItem();
140
        return $item ? $item->getId() : $this->customizedItemId;
141
    }
142
143
    protected function getParentOrderItem()
144
    {
145
        return $this->getAncestorPayloadOfType('\eBayEnterprise\RetailOrderManagement\Payload\Order\IOrderItem');
146
    }
147
148
    protected function getOrderItemContainer()
149
    {
150
        return $this->getAncestorPayloadOfType('\eBayEnterprise\RetailOrderManagement\Payload\Order\IOrderItemContainer');
151
    }
152
153
    /**
154
     * Search through related payloads for the item the customization applies
155
     * to. Will attempt to dereference  an item id if one exists. If there is
156
     * not an id to dereference, will default to the first order item ancestor.
157
     * If no suitable item can be found, will return null.
158
     *
159
     * This will perform a fresh search on every invocation. As payloads related
160
     * to this one may mutate around this payload, it is not guaranteed to be
161
     * idempotent so results are not cached.
162
     *
163
     * @return IOrderItem|null
164
     */
165
    protected function findCustomizedItem()
166
    {
167
        // Get the parent item if there is one, may be null if there isn't.
168
        $parentItem = $this->getParentOrderItem();
169
        // If there isn't an id to dereference, accept the parent, whether
170
        // there is one or not, as the item being referenced. If there is
171
        // an id to dereference and it matches the parent, no need to search
172
        // further, so it can be returned.
173
        if (is_null($this->customizedItemId) ||
174
            ($parentItem && $parentItem->getId() === $this->customizedItemId)
175
        ) {
176
            return $parentItem;
177
        }
178
179
        // If an item hasn't been found yet, look though all items that could
180
        // be referenced (items in a related IOrderItemContainer) by the item id
181
        // for a match - know there is an id as if there weren't, the parent
182
        // item would have already been returned.
183
        $itemContainer = $this->getOrderItemContainer();
184
        if ($itemContainer) {
185
            foreach ($itemContainer->getOrderItems() as $orderItem) {
186
                if ($orderItem->getId() === $this->customizedItemId) {
187
                    $this->customizedItem = $orderItem;
188
                    break;
189
                }
190
            }
191
        }
192
        // If still no match, one can't be found/doesn't exist so return null.
193
        return null;
194
    }
195
196 View Code Duplication
    protected function serializeContents()
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...
197
    {
198
        return $this->serializeOptionalXmlEncodedValue('CustomizationId', $this->getCustomizationId())
199
            . $this->getCustomizationInstructions()->serialize()
200
            . (!is_null($this->getExtendedPrice()) ? $this->getExtendedPrice()->setRootNodeName('ExtendedPrice')->serialize() : '')
201
            . "<ItemId>{$this->xmlEncode($this->getItemId())}</ItemId>"
202
            . $this->serializeCustomizedItem();
203
    }
204
205
    /**
206
     * When a customized item id has been provided, return a serialization
207
     * of the reference - "Item" node with "ref" attribute set to the item id.
208
     * When no customized item id is set, returns an empty string.
209
     *
210
     * @return string
211
     */
212
    protected function serializeCustomizedItem()
213
    {
214
        $itemId = $this->getCustomizedItemId();
215
        return $itemId ? "<Item ref='{$this->xmlEncode($itemId)}'/>" : '';
216
    }
217
218 View Code Duplication
    protected function deserializeExtra($serializedPayload)
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...
219
    {
220
        $xpath = $this->getPayloadAsXPath($serializedPayload);
221
        $priceNode = $xpath->query('x:ExtendedPrice')->item(0);
222
        if ($priceNode) {
223
            $this->extendedPrice = $this->getEmptyExtendedPrice()->deserialize($priceNode->C14N());
224
        }
225
    }
226
227
    protected function getRootNodeName()
228
    {
229
        return static::ROOT_NODE;
230
    }
231
232
    protected function getXmlNamespace()
233
    {
234
        return self::XML_NS;
235
    }
236
}
237