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.

TGifting::serializeOptionalXmlEncodedValue()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
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-2014 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 DOMXPath;
19
20
trait TGifting
21
{
22
    /** @var string */
23
    protected $giftItemId;
24
    /** @var IPriceGroup */
25
    protected $giftPricing;
26
    /** @var bool */
27
    protected $includeGiftWrapping;
28
    /** @var string */
29
    protected $giftMessageTo;
30
    /** @var string */
31
    protected $giftMessageFrom;
32
    /** @var string */
33
    protected $giftMessageContent;
34
    /** @var string */
35
    protected $giftCardTo;
36
    /** @var string */
37
    protected $giftCardFrom;
38
    /** @var string */
39
    protected $giftCardMessage;
40
    /** @var string */
41
    protected $packslipTo;
42
    /** @var string */
43
    protected $packslipFrom;
44
    /** @var string */
45
    protected $packslipMessage;
46
    /** @var string */
47
    protected $localizedFromLabel;
48
    /** @var string */
49
    protected $localizedToLabel;
50
51
    public function getEmptyGiftingPriceGroup()
52
    {
53
        return $this->buildPayloadForInterface(self::GIFTING_PRICE_GROUP_INTERFACE);
0 ignored issues
show
Bug introduced by
It seems like buildPayloadForInterface() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
    }
55
56
    public function getGiftItemId()
57
    {
58
        return $this->giftItemId;
59
    }
60
61
    public function setGiftItemId($giftItemId)
62
    {
63
        $this->giftItemId = $this->cleanString($giftItemId, 20);
0 ignored issues
show
Bug introduced by
It seems like cleanString() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
        return $this;
65
    }
66
67
    public function getGiftPricing()
68
    {
69
        return $this->giftPricing;
70
    }
71
72
    public function setGiftPricing(IPriceGroup $giftPricing)
73
    {
74
        $this->giftPricing = $giftPricing;
75
        return $this;
76
    }
77
78
    public function getIncludeGiftWrapping()
79
    {
80
        return $this->includeGiftWrapping;
81
    }
82
83
    public function setIncludeGiftWrapping($includeGiftWrapping)
84
    {
85
        $this->includeGiftWrapping = (bool) $includeGiftWrapping;
86
        return $this;
87
    }
88
89
    public function getGiftMessageTo()
90
    {
91
        return $this->giftMessageTo;
92
    }
93
94
    public function setGiftMessageTo($giftMessageTo)
95
    {
96
        $this->giftMessageTo = $giftMessageTo;
97
        return $this;
98
    }
99
100
    public function getGiftMessageFrom()
101
    {
102
        return $this->giftMessageFrom;
103
    }
104
105
    public function setGiftMessageFrom($giftMessageFrom)
106
    {
107
        $this->giftMessageFrom = $giftMessageFrom;
108
        return $this;
109
    }
110
111
    public function getGiftMessageContent()
112
    {
113
        return $this->giftMessageContent;
114
    }
115
116
    public function setGiftMessageContent($giftMessageContent)
117
    {
118
        $this->giftMessageContent = $this->normalizeWhitespace($giftMessageContent);
0 ignored issues
show
Bug introduced by
It seems like normalizeWhitespace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
        return $this;
120
    }
121
122
    public function getGiftCardTo()
123
    {
124
        return $this->giftCardTo;
125
    }
126
127
    public function setGiftCardTo($giftCardTo)
128
    {
129
        $this->giftCardTo = $giftCardTo;
130
        return $this;
131
    }
132
133
    public function getGiftCardFrom()
134
    {
135
        return $this->giftCardFrom;
136
    }
137
138
    public function setGiftCardFrom($giftCardFrom)
139
    {
140
        $this->giftCardFrom = $giftCardFrom;
141
        return $this;
142
    }
143
144
    public function getGiftCardMessage()
145
    {
146
        return $this->giftCardMessage;
147
    }
148
149
    public function setGiftCardMessage($giftCardMessage)
150
    {
151
        $this->giftCardMessage = $this->normalizeWhitespace($giftCardMessage);
0 ignored issues
show
Bug introduced by
It seems like normalizeWhitespace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
152
        return $this;
153
    }
154
155
    public function getPackslipTo()
156
    {
157
        return $this->packslipTo;
158
    }
159
160
    public function setPackslipTo($packslipTo)
161
    {
162
        $this->packslipTo = $packslipTo;
163
        return $this;
164
    }
165
166
    public function getPackslipFrom()
167
    {
168
        return $this->packslipFrom;
169
    }
170
171
    public function setPackslipFrom($packslipFrom)
172
    {
173
        $this->packslipFrom = $packslipFrom;
174
        return $this;
175
    }
176
177
    public function getPackslipMessage()
178
    {
179
        return $this->packslipMessage;
180
    }
181
182
    public function setPackslipMessage($packslipMessage)
183
    {
184
        $this->packslipMessage = $this->normalizeWhitespace($packslipMessage);
0 ignored issues
show
Bug introduced by
It seems like normalizeWhitespace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
185
        return $this;
186
    }
187
188
    public function getLocalizedFromLabel()
189
    {
190
        return $this->localizedFromLabel;
191
    }
192
193
    public function setLocalizedFromLabel($localizedFromLabel)
194
    {
195
        $this->localizedFromLabel = $localizedFromLabel;
196
        return $this;
197
    }
198
199
    public function getLocalizedToLabel()
200
    {
201
        return $this->localizedToLabel;
202
    }
203
204
    public function setLocalizedToLabel($localizedToLabel)
205
    {
206
        $this->localizedToLabel = $localizedToLabel;
207
        return $this;
208
    }
209
210
    /**
211
     * Serialize gifting data.
212
     *
213
     * @return string
214
     */
215
    protected function serializeGifting()
216
    {
217
        $serializedData = $this->serializeGift()
218
            . $this->serializeGiftingGiftCard()
219
            . $this->serializeGiftingPackslip();
220
        return $serializedData ? "<Gifting>$serializedData</Gifting>" : '';
221
    }
222
223
    /**
224
     * Serialize the "Gift" gifting data. Will only be included if there is
225
     * a gift item id set.
226
     *
227
     * @return string
228
     */
229
    protected function serializeGift()
230
    {
231
        if ($this->getGiftItemId()) {
232
            $pricing = $this->getGiftPricing();
233
            return '<Gift>'
234
                . "<ItemId>{$this->xmlEncode($this->getGiftItemId())}</ItemId>"
235
                . ($pricing ? $pricing->setRootNodeName('Pricing')->serialize() : '')
236
                . $this->serializeGiftingGiftMessage()
237
                . '</Gift>';
238
        }
239
        return '';
240
    }
241
242
    /**
243
     * Serialize the gift message data. Will only be included if there
244
     * are valid to and from fields.
245
     *
246
     * @return string
247
     */
248
    protected function serializeGiftingGiftMessage()
249
    {
250
        $messageSerialization = $this->serializeGiftingMessage(
251
            $this->getGiftMessageTo(),
252
            $this->getGiftMessageFrom(),
253
            $this->getGiftMessageContent()
254
        );
255
        return $messageSerialization ? $messageSerialization : '';
256
    }
257
258
    /**
259
     * Serialize the gift card message data. Will only be included if there
260
     * are valid to and from fields.
261
     *
262
     * @return string
263
     */
264
    protected function serializeGiftingGiftCard()
265
    {
266
        $messageSerialization = $this->serializeGiftingMessage(
267
            $this->getGiftCardTo(),
268
            $this->getGiftCardFrom(),
269
            $this->getGiftCardMessage()
270
        );
271
        // If the gifting includes gift wrapping, the gift card node
272
        // must always be included.
273
        return ($messageSerialization || $this->getIncludeGiftWrapping())
274
            ? "<GiftCard>$messageSerialization</GiftCard>"
275
            : '';
276
    }
277
278
    /**
279
     * Serialize the packslip message data. Will only be included if there
280
     * are valid to and from fields.
281
     *
282
     * @return string
283
     */
284
    protected function serializeGiftingPackslip()
285
    {
286
        $messageSerialization = $this->serializeGiftingMessage(
287
            $this->getPackslipTo(),
288
            $this->getPackslipFrom(),
289
            $this->getPackslipMessage()
290
        );
291
        return $messageSerialization ? "<Packslip>$messageSerialization</Packslip>" : '';
292
    }
293
294
    /**
295
     * Serialize a gifting message from, to and message. Will only return a
296
     * serialization if at least a from and to name are given.
297
     *
298
     * @param string
299
     * @param string
300
     * @param string
301
     * @return string
302
     */
303
    protected function serializeGiftingMessage($toName, $fromName, $messageText)
304
    {
305
        $localizedTo = $this->serializeOptionalAttribute(
0 ignored issues
show
Bug introduced by
It seems like serializeOptionalAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
306
            'localizedDisplayText',
307
            $this->xmlEncode($this->getLocalizedToLabel())
308
        );
309
        $localizedFrom = $this->serializeOptionalAttribute(
0 ignored issues
show
Bug introduced by
It seems like serializeOptionalAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
310
            'localizedDisplayText',
311
            $this->xmlEncode($this->getLocalizedFromLabel())
312
        );
313
        return $toName && $fromName
314
            ? '<Message>'
315
                . "<To $localizedTo>{$this->xmlEncode($toName)}</To>"
316
                . "<From $localizedFrom>{$this->xmlEncode($fromName)}</From>"
317
                . $this->serializeOptionalXmlEncodedValue('Message', $messageText)
318
                . '</Message>'
319
            : '';
320
    }
321
322
    /**
323
     * Extract gifting pricing from the payload in the DOMXPath and, if it is
324
     * included in the serialized data, deserialize it into a new price group.
325
     *
326
     * @return self
327
     */
328 View Code Duplication
    protected function deserializeGiftPricing(DOMXPath $xpath)
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...
329
    {
330
        $priceNode = $xpath->query('x:Gifting/x:Gift/x:Pricing')->item(0);
331
        if ($priceNode) {
332
            $this->setGiftPricing(
333
                $this->getEmptyGiftingPriceGroup()->deserialize($priceNode->C14N())
334
            );
335
        }
336
        return $this;
337
    }
338
339
    /**
340
     * Serialize an optional element containing a string. The value will be
341
     * xml-encoded if is not null.
342
     *
343
     * @param string
344
     * @param string
345
     * @return string
346
     */
347
    abstract protected function serializeOptionalXmlEncodedValue($name, $value);
348
349
    /**
350
     * encode the passed in string to be safe for xml if it is not null,
351
     * otherwise simply return the null parameter.
352
     *
353
     * @param string|null
354
     * @return string|null
355
     */
356
    abstract protected function xmlEncode($value = null);
357
}
358