CreatePaymentV2Request   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 40
eloc 87
c 2
b 0
f 0
dl 0
loc 260
rs 9.2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequired() 0 7 1
A setChannel() 0 17 5
A setBillingAddress() 0 17 5
A setPaymentData() 0 23 5
A setBirthdate() 0 7 2
A setShippingAddress() 0 17 5
A setCart() 0 21 5
C isValid() 0 16 12

How to fix   Complexity   

Complex Class

Complex classes like CreatePaymentV2Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CreatePaymentV2Request, and based on these observations, apply Extract Interface, too.

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\CustomerAddressEntity;
18
use Payever\ExternalIntegration\Payments\Http\MessageEntity\CartItemEntity;
19
use Payever\ExternalIntegration\Payments\Http\MessageEntity\ChannelEntity;
20
use Payever\ExternalIntegration\Payments\Http\MessageEntity\PaymentDataEntity;
21
22
23
/**
24
 * This class represents Create Payment RequestInterface Entity
25
 *
26
 * @method ChannelEntity          getChannel()
27
 * @method float                  getAmount()
28
 * @method float                  getFee()
29
 * @method string                 getOrderId()
30
 * @method string                 getCurrency()
31
 * @method CartItemEntity[]       getCart()
32
 * @method string                 getPaymentMethod()
33
 * @method string|null            getVariantId()
34
 * @method \DateTime|false        getBirthdate()
35
 * @method string                 getPhone()
36
 * @method string                 getEmail()
37
 * @method string                 getLocale()
38
 * @method string                 getShippingAddress()
39
 * @method string                 getBillingAddress()
40
 * @method string                 getSuccessUrl()
41
 * @method string                 getFailureUrl()
42
 * @method string                 getCancelUrl()
43
 * @method string                 getNoticeUrl()
44
 * @method string                 getPendingUrl()
45
 * @method string                 getCustomerRedirectUrl()
46
 * @method string                 getXFrameHost()
47
 * @method string                 getPluginVersion()
48
 * @method PaymentDataEntity|null getPaymentData()
49
 * @method self                   setAmount(float $amount)
50
 * @method self                   setFee(float $fee)
51
 * @method self                   setOrderId(string $id)
52
 * @method self                   setPaymentMethod(string $method)
53
 * @method self                   setVariantId(string|null $variantId)
54
 * @method self                   setCurrency(string $currency)
55
 * @method self                   setPhone(string $phone)
56
 * @method self                   setEmail(string $email)
57
 * @method self                   setLocale(string $locale)
58
 * @method self                   setSuccessUrl(string $url)
59
 * @method self                   setFailureUrl(string $url)
60
 * @method self                   setCancelUrl(string $url)
61
 * @method self                   setNoticeUrl(string $url)
62
 * @method self                   setPendingUrl(string $url)
63
 * @method self                   setCustomerRedirectUrl(string $url)
64
 * @method self                   setXFrameHost(string $host)
65
 * @method self                   setPluginVersion(string $version)
66
 *
67
 * @SuppressWarnings(PHPMD.TooManyFields)
68
 */
69
class CreatePaymentV2Request extends RequestEntity
70
{
71
    /** @var ChannelEntity $channel */
72
    protected $channel;
73
74
    /** @var string $paymentMethod */
75
    protected $paymentMethod;
76
77
    /** @var string|null */
78
    protected $variantId;
79
80
    /** @var float $amount */
81
    protected $amount;
82
83
    /** @var float $fee */
84
    protected $fee;
85
86
    /** @var string $orderId */
87
    protected $orderId;
88
89
    /** @var string $currency */
90
    protected $currency;
91
92
    /** @var CartItemEntity[] $cart */
93
    protected $cart;
94
95
    /** @var \DateTime|bool $birthdate */
96
    protected $birthdate;
97
98
    /** @var string $phone */
99
    protected $phone;
100
101
    /** @var string $email */
102
    protected $email;
103
104
    /** @var string $locale */
105
    protected $locale;
106
107
    /** @var string $successUrl */
108
    protected $successUrl;
109
110
    /** @var string $failureUrl */
111
    protected $failureUrl;
112
113
    /** @var string $cancelUrl */
114
    protected $cancelUrl;
115
116
    /** @var string $noticeUrl */
117
    protected $noticeUrl;
118
119
    /** @var string $pendingUrl */
120
    protected $pendingUrl;
121
122
    /** @var string $customerRedirectUrl */
123
    protected $customerRedirectUrl;
124
125
    /** @var string $xFrameHost */
126
    protected $xFrameHost;
127
128
    /** @var string $pluginVersion */
129
    protected $pluginVersion;
130
131
    /** @var CustomerAddressEntity $shippingAddress */
132
    protected $shippingAddress;
133
134
    /** @var CustomerAddressEntity $billingAddress */
135
    protected $billingAddress;
136
137
    /** @var PaymentDataEntity $paymentData */
138
    protected $paymentData;
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getRequired()
144
    {
145
        return [
146
            'channel',
147
            'amount',
148
            'order_id',
149
            'currency',
150
        ];
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
156
     */
157
    public function isValid()
158
    {
159
        if (is_array($this->cart)) {
0 ignored issues
show
introduced by
The condition is_array($this->cart) is always true.
Loading history...
160
            foreach ($this->cart as $item) {
161
                if (!$item instanceof CartItemEntity || !$item->isValid()) {
162
                    return false;
163
                }
164
            }
165
        }
166
167
        return parent::isValid() &&
168
            is_numeric($this->amount) &&
169
            is_array($this->cart) &&
170
            !empty($this->cart) &&
171
            (!$this->fee || is_numeric($this->fee)) &&
172
            (!$this->birthdate || $this->birthdate instanceof \DateTime);
173
    }
174
175
    /**
176
     * Sets Cart
177
     *
178
     * @param array|string $cart
179
     *
180
     * @return $this
181
     */
182
    public function setCart($cart)
183
    {
184
        if (!$cart) {
185
            return $this;
186
        }
187
188
        if (is_string($cart)) {
189
            $cart = json_decode($cart);
190
        }
191
192
        if (!is_array($cart)) {
193
            return $this;
194
        }
195
196
        $this->cart = [];
197
198
        foreach ($cart as $item) {
199
            $this->cart[] = new CartItemEntity($item);
200
        }
201
202
        return $this;
203
    }
204
205
    /**
206
     * Sets shipping address
207
     *
208
     * @param CustomerAddressEntity|string $shippingAddress
209
     *
210
     * @return $this
211
     */
212
    public function setShippingAddress($shippingAddress)
213
    {
214
        if (!$shippingAddress) {
215
            return $this;
216
        }
217
218
        if (is_string($shippingAddress)) {
219
            $shippingAddress = json_decode($shippingAddress);
220
        }
221
222
        if (!is_array($shippingAddress) && !is_object($shippingAddress)) {
223
            return $this;
224
        }
225
226
        $this->shippingAddress = new CustomerAddressEntity($shippingAddress);
227
228
        return $this;
229
    }
230
231
    /**
232
     * Sets billing address
233
     *
234
     * @param CustomerAddressEntity|string $billingAddress
235
     *
236
     * @return $this
237
     */
238
    public function setBillingAddress($billingAddress)
239
    {
240
        if (!$billingAddress) {
241
            return $this;
242
        }
243
244
        if (is_string($billingAddress)) {
245
            $billingAddress = json_decode($billingAddress);
246
        }
247
248
        if (!is_array($billingAddress) && !is_object($billingAddress)) {
249
            return $this;
250
        }
251
252
        $this->billingAddress = new CustomerAddressEntity($billingAddress);
253
254
        return $this;
255
    }
256
257
    /**
258
     * Sets Birthdate
259
     *
260
     * @param string $birthdate
261
     *
262
     * @return $this
263
     */
264
    public function setBirthdate($birthdate)
265
    {
266
        if ($birthdate) {
267
            $this->birthdate = date_create($birthdate);
268
        }
269
270
        return $this;
271
    }
272
273
    /**
274
     * Sets payment data
275
     *
276
     * @param PaymentDataEntity|array|string $paymentData
277
     *
278
     * @return $this
279
     */
280
    public function setPaymentData($paymentData)
281
    {
282
        if (!$paymentData) {
283
            return $this;
284
        }
285
286
        if ($paymentData instanceof PaymentDataEntity) {
287
            $this->paymentData = $paymentData;
288
289
            return $this;
290
        }
291
292
        if (is_string($paymentData)) {
293
            $paymentData = json_decode($paymentData, true);
294
        }
295
296
        if (!is_array($paymentData)) {
297
            return $this;
298
        }
299
300
        $this->paymentData = new PaymentDataEntity($paymentData);
301
302
        return $this;
303
    }
304
305
    /**
306
     * Sets Channel
307
     *
308
     * @param ChannelEntity|string $channel
309
     *
310
     * @return $this
311
     */
312
    public function setChannel($channel)
313
    {
314
        if (!$channel) {
315
            return $this;
316
        }
317
318
        if (is_string($channel)) {
319
            $channel = json_decode($channel);
320
        }
321
322
        if (!is_array($channel) && !is_object($channel)) {
323
            return $this;
324
        }
325
326
        $this->channel = new ChannelEntity($channel);
327
328
        return $this;
329
    }
330
}
331