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

CreatePaymentRequest::isValid()   C

Complexity

Conditions 14
Paths 79

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 17
rs 6.2666
cc 14
nc 79
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Payever\ExternalIntegration\Payments\Http\MessageEntity\CartItemEntity;
18
use Payever\ExternalIntegration\Payments\Http\MessageEntity\ShippingAddressEntity;
19
20
/**
21
 * This class represents Create Payment RequestInterface Entity
22
 *
23
 * @method string                 getChannel()
24
 * @method integer                getChannelSetId()
25
 * @method float                  getAmount()
26
 * @method float                  getFee()
27
 * @method string                 getOrderId()
28
 * @method string                 getCurrency()
29
 * @method CartItemEntity[]       getCart()
30
 * @method string                 getSalutation()
31
 * @method string                 getPaymentMethod()
32
 * @method string|null            getVariantId()
33
 * @method string                 getFirstName()
34
 * @method string                 getLastName()
35
 * @method string                 getStreet()
36
 * @method string                 getStreetNumber()
37
 * @method string                 getZip()
38
 * @method string                 getCity()
39
 * @method string                 getCountry()
40
 * @method string                 getSocialSecurityNumber()
41
 * @method \DateTime|false        getBirthdate()
42
 * @method string                 getPhone()
43
 * @method string                 getEmail()
44
 * @method string                 getShippingAddress()
45
 * @method string                 getSuccessUrl()
46
 * @method string                 getFailureUrl()
47
 * @method string                 getCancelUrl()
48
 * @method string                 getNoticeUrl()
49
 * @method string                 getPendingUrl()
50
 * @method string                 getXFrameHost()
51
 * @method string                 getPluginVersion()
52
 * @method self                   setChannel(string $channel)
53
 * @method self                   setChannelSetId(int $id)
54
 * @method self                   setAmount(float $amount)
55
 * @method self                   setFee(float $fee)
56
 * @method self                   setOrderId(string $id)
57
 * @method self                   setPaymentMethod(string $method)
58
 * @method self                   setVariantId(string|null $variantId)
59
 * @method self                   setCurrency(string $currency)
60
 * @method self                   setSalutation(string $salutation)
61
 * @method self                   setFirstName(string $name)
62
 * @method self                   setLastName(string $name)
63
 * @method self                   setStreet(string $street)
64
 * @method self                   setStreetNumber(string $streetNumber)
65
 * @method self                   setZip(string $zip)
66
 * @method self                   setCity(string $city)
67
 * @method self                   setCountry(string $country)
68
 * @method self                   setSocialSecurityNumber(string $ssn)
69
 * @method self                   setPhone(string $phone)
70
 * @method self                   setEmail(string $email)
71
 * @method self                   setSuccessUrl(string $url)
72
 * @method self                   setFailureUrl(string $url)
73
 * @method self                   setCancelUrl(string $url)
74
 * @method self                   setNoticeUrl(string $url)
75
 * @method self                   setPendingUrl(string $url)
76
 * @method self                   setXFrameHost(string $host)
77
 * @method self                   setPluginVersion(string $version)
78
 *
79
 * @SuppressWarnings(PHPMD.TooManyFields)
80
 */
81
class CreatePaymentRequest extends RequestEntity
82
{
83
    /** @var string $channel */
84
    protected $channel;
85
86
    /** @var integer $channelSetId */
87
    protected $channelSetId;
88
89
    /** @var string $paymentMethod */
90
    protected $paymentMethod;
91
92
    /** @var string|null */
93
    protected $variantId;
94
95
    /** @var float $amount */
96
    protected $amount;
97
98
    /** @var float $fee */
99
    protected $fee;
100
101
    /** @var string $orderId */
102
    protected $orderId;
103
104
    /** @var string $currency */
105
    protected $currency;
106
107
    /** @var CartItemEntity[] $cart */
108
    protected $cart;
109
110
    /** @var string $salutation */
111
    protected $salutation;
112
113
    /** @var string $firstName */
114
    protected $firstName;
115
116
    /** @var string $lastName */
117
    protected $lastName;
118
119
    /** @var string $street */
120
    protected $street;
121
122
    /** @var string $streetNumber */
123
    protected $streetNumber;
124
125
    /** @var string $zip */
126
    protected $zip;
127
128
    /** @var string $city */
129
    protected $city;
130
131
    /** @var string $country */
132
    protected $country;
133
134
    /** @var string $socialSecurityNumber */
135
    protected $socialSecurityNumber;
136
137
    /** @var \DateTime|bool $birthdate */
138
    protected $birthdate;
139
140
    /** @var string $phone */
141
    protected $phone;
142
143
    /** @var string $email */
144
    protected $email;
145
146
    /** @var string $successUrl */
147
    protected $successUrl;
148
149
    /** @var string $failureUrl */
150
    protected $failureUrl;
151
152
    /** @var string $cancelUrl */
153
    protected $cancelUrl;
154
155
    /** @var string $noticeUrl */
156
    protected $noticeUrl;
157
158
    /** @var string $pendingUrl */
159
    protected $pendingUrl;
160
161
    /** @var string $xFrameHost */
162
    protected $xFrameHost;
163
164
    /** @var string $pluginVersion */
165
    protected $pluginVersion;
166
167
    /** @var ShippingAddressEntity $shippingAddress */
168
    protected $shippingAddress;
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getRequired()
174
    {
175
        return [
176
            'channel',
177
            'amount',
178
            'order_id',
179
            'currency',
180
        ];
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
186
     */
187
    public function isValid()
188
    {
189
        if (is_array($this->cart)) {
0 ignored issues
show
introduced by
The condition is_array($this->cart) is always true.
Loading history...
190
            foreach ($this->cart as $item) {
191
                if (!$item instanceof CartItemEntity || !$item->isValid()) {
192
                    return false;
193
                }
194
            }
195
        }
196
197
        return parent::isValid() &&
198
            is_numeric($this->amount) &&
199
            is_array($this->cart) &&
200
            !empty($this->cart) &&
201
            (!$this->channelSetId || is_integer($this->channelSetId)) &&
202
            (!$this->fee || is_numeric($this->fee)) &&
203
            (!$this->birthdate || $this->birthdate instanceof \DateTime);
204
    }
205
206
    /**
207
     * Sets Cart
208
     *
209
     * @param array|string $cart
210
     *
211
     * @return $this
212
     */
213
    public function setCart($cart)
214
    {
215
        if (!$cart) {
216
            return $this;
217
        }
218
219
        if (is_string($cart)) {
220
            $cart = json_decode($cart);
221
        }
222
223
        if (!is_array($cart)) {
224
            return $this;
225
        }
226
227
        $this->cart = [];
228
229
        foreach ($cart as $item) {
230
            $this->cart[] = new CartItemEntity($item);
231
        }
232
233
        return $this;
234
    }
235
236
    /**
237
     * Sets shipping address
238
     *
239
     * @param ShippingAddressEntity|string $shippingAddress
240
     *
241
     * @return $this
242
     */
243
    public function setShippingAddress($shippingAddress)
244
    {
245
        if (!$shippingAddress) {
246
            return $this;
247
        }
248
249
        if (is_string($shippingAddress)) {
250
            $shippingAddress = json_decode($shippingAddress);
251
        }
252
253
        if (!is_array($shippingAddress) && !is_object($shippingAddress)) {
254
            return $this;
255
        }
256
257
        $this->shippingAddress = new ShippingAddressEntity($shippingAddress);
258
259
        return $this;
260
    }
261
262
    /**
263
     * Sets Birthdate
264
     *
265
     * @param string $birthdate
266
     *
267
     * @return $this
268
     */
269
    public function setBirthdate($birthdate)
270
    {
271
        $this->birthdate = date_create($birthdate);
272
273
        return $this;
274
    }
275
}
276