Passed
Push — master ( 302660...3652f8 )
by payever
04:19 queued 01:32
created

ShippingGoodsPaymentRequest::isValid()   B

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 string                getReason()
24
 * @method self                  setReason(string $reason)
25
 * @method array                 getPaymentItems()
26
 * @method ShippingDetailsEntity getShippingDetails()
27
 */
28
class ShippingGoodsPaymentRequest extends RequestEntity
29
{
30
    /** @var string $reason */
31
    protected $reason;
32
33
    /** @var float $amount */
34
    protected $amount;
35
36
    /** @var PaymentItemEntity[] $paymentItems */
37
    protected $paymentItems;
38
39
    /** @var ShippingDetailsEntity $shippingDetails */
40
    protected $shippingDetails;
41
42
    /**
43
     * Sets Payment Items
44
     *
45
     * @param array|string $paymentItems
46
     *
47
     * @return $this
48
     */
49
    public function setPaymentItems($paymentItems)
50
    {
51
        if (!$paymentItems) {
52
            return $this;
53
        }
54
55
        if (is_string($paymentItems)) {
56
            $paymentItems = json_decode($paymentItems);
57
        }
58
59
        if (!is_array($paymentItems)) {
60
            return $this;
61
        }
62
63
        $this->paymentItems = [];
64
65
        foreach ($paymentItems as $item) {
66
            $this->paymentItems[] = new PaymentItemEntity($item);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Sets shipping details
74
     *
75
     * @param ShippingDetailsEntity|string $shippingDetails
76
     *
77
     * @return $this
78
     */
79
    public function setShippingDetails($shippingDetails)
80
    {
81
        if (!$shippingDetails) {
82
            return $this;
83
        }
84
85
        if (is_string($shippingDetails)) {
86
            $shippingDetails = json_decode($shippingDetails);
87
        }
88
89
        if (!is_array($shippingDetails) && !is_object($shippingDetails)) {
90
            return $this;
91
        }
92
93
        $this->shippingDetails = new ShippingDetailsEntity($shippingDetails);
94
95
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function isValid()
102
    {
103
        if (is_array($this->paymentItems)) {
0 ignored issues
show
introduced by
The condition is_array($this->paymentItems) is always true.
Loading history...
104
            foreach ($this->paymentItems as $item) {
105
                if (!$item instanceof PaymentItemEntity || !$item->isValid()) {
106
                    return false;
107
                }
108
            }
109
        }
110
111
        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...
112
    }
113
}
114