ShippingGoodsPaymentRequest::isValid()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 8.8333
cc 7
nc 7
nop 0
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  RequestEntity
7
 * @package   Payever\Payments
8
 * @author    payever GmbH <[email protected]>
9
 * @copyright 2017-2021 payever GmbH
10
 * @license   MIT <https://opensource.org/licenses/MIT>
11
 * @link      https://docs.payever.org/shopsystems/api/getting-started
12
 */
13
14
namespace Payever\ExternalIntegration\Payments\Http\RequestEntity;
15
16
use Payever\ExternalIntegration\Core\Http\RequestEntity;
17
18
/**
19
 * This class represents Shipping Goods Payment RequestInterface Entity
20
 *
21
 * @method float                 getAmount()
22
 * @method self                  setAmount(float $amount)
23
 * @method float                 getDeliveryFee()
24
 * @method self                  setDeliveryFee(float $deliveryFee)
25
 * @method string                getReason()
26
 * @method self                  setReason(string $reason)
27
 * @method array                 getPaymentItems()
28
 * @method ShippingDetailsEntity getShippingDetails()
29
 * @method string                getInvoiceId()
30
 * @method self                  setInvoiceId(string $invoiceId)
31
 * @method string                getInvoiceUrl()
32
 * @method self                  setInvoiceUrl(string $invoiceUrl)
33
 * @method string                getPoNumber()
34
 * @method self                  setPoNumber(string $poNumber)
35
 */
36
class ShippingGoodsPaymentRequest extends RequestEntity
37
{
38
    /** @var string $reason */
39
    protected $reason;
40
41
    /** @var float $amount */
42
    protected $amount;
43
44
    /** @var float $deliveryFee */
45
    protected $deliveryFee;
46
47
    /** @var PaymentItemEntity[] $paymentItems */
48
    protected $paymentItems;
49
50
    /** @var ShippingDetailsEntity $shippingDetails */
51
    protected $shippingDetails;
52
53
    /** @var string $invoiceId */
54
    protected $invoiceId;
55
56
    /** @var string $invoiceUrl */
57
    protected $invoiceUrl;
58
59
    /** @var string $poNumber */
60
    protected $poNumber;
61
62
    /**
63
     * Sets Payment Items
64
     *
65
     * @param array|string $paymentItems
66
     *
67
     * @return $this
68
     */
69
    public function setPaymentItems($paymentItems)
70
    {
71
        if (!$paymentItems) {
72
            return $this;
73
        }
74
75
        if (is_string($paymentItems)) {
76
            $paymentItems = json_decode($paymentItems);
77
        }
78
79
        if (!is_array($paymentItems)) {
80
            return $this;
81
        }
82
83
        $this->paymentItems = [];
84
85
        foreach ($paymentItems as $item) {
86
            $this->paymentItems[] = new PaymentItemEntity($item);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * Sets shipping details
94
     *
95
     * @param ShippingDetailsEntity|string $shippingDetails
96
     *
97
     * @return $this
98
     */
99
    public function setShippingDetails($shippingDetails)
100
    {
101
        if (!$shippingDetails) {
102
            return $this;
103
        }
104
105
        if (is_string($shippingDetails)) {
106
            $shippingDetails = json_decode($shippingDetails);
107
        }
108
109
        if (!is_array($shippingDetails) && !is_object($shippingDetails)) {
110
            return $this;
111
        }
112
113
        $this->shippingDetails = new ShippingDetailsEntity($shippingDetails);
114
115
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function isValid()
122
    {
123
        if (is_array($this->paymentItems)) {
0 ignored issues
show
introduced by
The condition is_array($this->paymentItems) is always true.
Loading history...
124
            foreach ($this->paymentItems as $item) {
125
                if (!$item instanceof PaymentItemEntity || !$item->isValid()) {
126
                    return false;
127
                }
128
            }
129
        }
130
131
        return parent::isValid() && (!$this->paymentItems || is_array($this->paymentItems));
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paymentItems of type Payever\ExternalIntegrat...ity\PaymentItemEntity[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132
    }
133
}
134