Passed
Push — master ( c06059...bfa84d )
by payever
03:42
created

CreatePaymentV2Request::setBillingAddress()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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