Test Failed
Pull Request — master (#13)
by Bernhard
02:34 queued 01:06
created

PaymentRequest::withCancelUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
use GloBee\PaymentApi\Exceptions\Validation\ValidationException;
6
7
class PaymentRequest extends Model
8
{
9
    protected $total;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
10
    protected $currency = 'USD';
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
11
    protected $customPaymentId;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
12
    protected $callbackData;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
13
    protected $customerName;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
14
    protected $customerEmail;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
15
    protected $successUrl;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
16
    protected $cancelUrl;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
17
    protected $ipnUrl;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
18
    protected $notificationEmail;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
19
    protected $confirmationSpeed = 'medium';
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
20
    protected $id;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
21
    protected $status;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
22
    protected $redirectUrl;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
23
    protected $expiresAt;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
24
    protected $createdAt;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
25
26 20
    public function __construct($total, $customerEmail)
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
27
    {
28 20
        $this->setTotal($total);
29 18
        $this->setCustomerEmail($customerEmail);
30 17
    }
31
32
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$total" missing
Loading history...
33
     * @param        $total
0 ignored issues
show
Coding Style introduced by
Missing parameter name
Loading history...
34
     * @param string $currency
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
35
     * @param string $customerEmail
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
36
     * @param string $clientName
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
37
     *
38
     * @return self
39
     */
40 12
    public static function create($total, $currency, $customerEmail, $clientName = '')
0 ignored issues
show
Coding Style introduced by
Type hint "total" missing for
Loading history...
Coding Style introduced by
Type hint "string" missing for $currency
Loading history...
Coding Style introduced by
Type hint "string" missing for $customerEmail
Loading history...
Coding Style introduced by
Type hint "string" missing for $clientName
Loading history...
41
    {
42 12
        $paymentRequest = new self($total, $customerEmail);
43
44 11
        $paymentRequest->setCurrency($currency);
45 9
        $paymentRequest->customerName = $clientName;
46
47 9
        return $paymentRequest;
48
    }
49
50
    /**
51
     * @param array $data
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
52
     *
53
     * @return PaymentRequest
54
     */
55 4
    public static function fromResponse(array $data)
56
    {
57 4
        $self = new self($data['total'], $data['customer']['email']);
58
59 4
        $callbackData = json_decode($data['callback_data'], true);
60 4
        if (json_last_error() !== JSON_ERROR_NONE) {
61 3
            $callbackData = $data['callback_data'];
62
        }
63 4
        $self->id = $data['id'];
64 4
        $self->status = $data['status'];
65 4
        $self->currency = $data['currency'];
66 4
        $self->customPaymentId = $data['custom_payment_id'];
67 4
        $self->callbackData = $callbackData;
68 4
        $self->customerName = $data['customer']['name'];
69 4
        $self->redirectUrl = $data['redirect_url'];
70 4
        $self->successUrl = $data['success_url'];
71 4
        $self->cancelUrl = $data['cancel_url'];
72 4
        $self->ipnUrl = $data['ipn_url'];
73 4
        $self->notificationEmail = $data['notification_email'];
74 4
        $self->confirmationSpeed = $data['confirmation_speed'];
75 4
        $self->expiresAt = $data['expires_at'];
76 4
        $self->createdAt = $data['created_at'];
77
78 4
        return $self;
79
    }
80
81
    /**
82
     * @param float $total
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
83
     *
84
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
85
     * @throws \GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
86
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
87 20
    protected function setTotal($total)
0 ignored issues
show
Coding Style introduced by
Type hint "float" missing for $total
Loading history...
88
    {
89 20
        Validator::validateNumberAboveMinimum('total', $total, 0);
90 18
        $this->total = $total;
91 18
    }
92
93
    /**
94
     * @param mixed $currency
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
95
     *
96
     * @throws \GloBee\PaymentApi\Exceptions\Validation\ValidationException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
97
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
98
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
99 11
    protected function setCurrency($currency)
100
    {
101 11
        Validator::validateStringLength('currency', $currency, 3);
102 9
        $this->currency = strtoupper($currency);
103 9
    }
104
105
    /**
106
     * @param mixed $customerEmail
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
107
     *
108
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
109
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
110
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
111 18
    protected function setCustomerEmail($customerEmail)
112
    {
113 18
        Validator::validateEmail('customer.email', $customerEmail);
114 17
        $this->customerEmail = $customerEmail;
115 17
    }
116
117
    /**
118
     * @param mixed $successUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
119
     *
120
     * @return self
121
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
122
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
123
     */
124 3
    public function withSuccessUrl($successUrl)
125
    {
126 3
        if ($successUrl !== null) {
127 3
            Validator::validateUrl('success_url', $successUrl);
128
        }
129 2
        $self = clone $this;
130 2
        $self->successUrl = $successUrl;
131
132 2
        return $self;
133
    }
134
135
    /**
136
     * @param mixed $cancelUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
137
     *
138
     * @return self
139
     */
140 3
    public function withCancelUrl($cancelUrl)
141
    {
142 3
        if ($cancelUrl !== null) {
143 3
            Validator::validateUrl('cancel_url', $cancelUrl);
144
        }
145 2
        $self = clone $this;
146 2
        $self->cancelUrl = $cancelUrl;
147
148 2
        return $self;
149
    }
150
151 3
    public function withCallbackData($callbackData)
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
152
    {
153 3
        $callbackDataString = $callbackData;
154
155 3
        if (!is_string($callbackData)) {
156 1
            $callbackDataString = json_encode($callbackData);
157
        }
158
159 3
        if (strlen($callbackDataString) > 150) {
160
            throw new ValidationException([], 'Callback Data must be less than 150 characters long.');
161
        }
162
163 3
        $self = clone $this;
164 3
        $self->callbackData = $callbackData;
165
166 3
        return $self;
167
    }
168
169
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$ipnUrl" missing
Loading history...
170
     * @param $ipnUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter name
Loading history...
171
     *
172
     * @return self
173
     */
174 3
    public function withIpnUrl($ipnUrl)
0 ignored issues
show
Coding Style introduced by
Type hint "ipnUrl" missing for
Loading history...
175
    {
176 3
        if ($ipnUrl !== null) {
177 3
            Validator::validateUrl('ipn_url', $ipnUrl);
178
        }
179 2
        $self = clone $this;
180 2
        $self->ipnUrl = $ipnUrl;
181
182 2
        return $self;
183
    }
184
185
    /**
186
     * @param mixed $customPaymentId
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
187
     *
188
     * @return self
189
     */
190 2
    public function withCustomPaymentId($customPaymentId)
191
    {
192 2
        $self = clone $this;
193 2
        $self->customPaymentId = $customPaymentId;
194
195 2
        return $self;
196
    }
197
198
    /**
199
     * @param mixed $notificationEmail
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
200
     *
201
     * @return self
202
     */
203 3
    public function withNotificationEmail($notificationEmail)
204
    {
205 3
        if ($notificationEmail !== null) {
206 3
            Validator::validateEmail('notification_email', $notificationEmail);
207
        }
208 2
        $self = clone $this;
209 2
        $self->notificationEmail = $notificationEmail;
210
211 2
        return $self;
212
    }
213
214
    /**
215
     * @return self
216
     */
217 1
    public function lowRiskConfirmation()
218
    {
219 1
        $self = clone $this;
220 1
        $self->confirmationSpeed = 'low';
221
222 1
        return $self;
223
    }
224
225
    /**
226
     * @return self
227
     */
228
    public function balancedConfirmation()
229
    {
230
        $self = clone $this;
231
        $self->confirmationSpeed = 'medium';
232
233
        return $self;
234
    }
235
236
    /**
237
     * @return self
238
     */
239 1
    public function quickConfirmation()
240
    {
241 1
        $self = clone $this;
242 1
        $self->confirmationSpeed = 'high';
243
244 1
        return $self;
245
    }
246
247 1
    public function confirmationSpeed($confirmationSpeed)
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
248
    {
249 1
        Validator::validateOptions('confirmation_speed', $confirmationSpeed, ['low', 'medium', 'high']);
250
251
        $self = clone $this;
252
        $self->confirmationSpeed = $confirmationSpeed;
253
254
        return $self;
255
    }
256
257
    /**
258
     * @return array
259
     */
260 2
    public function toArray()
261
    {
262 2
        $callbackData = $this->callbackData;
263 2
        if (is_array($callbackData)) {
264 1
            $callbackData = json_encode($callbackData);
265
        }
266
267
        return [
268 2
            'id' => $this->id,
269 2
            'status' => $this->status,
270 2
            'total' => $this->total,
271 2
            'currency' => $this->currency,
272 2
            'custom_payment_id' => $this->customPaymentId,
273 2
            'callback_data' => $callbackData,
274
            'customer' => [
275 2
                'name' => $this->customerName,
276 2
                'email' => $this->customerEmail,
277
            ],
278 2
            'redirect_url' => $this->redirectUrl,
279 2
            'success_url' => $this->successUrl,
280 2
            'cancel_url' => $this->cancelUrl,
281 2
            'ipn_url' => $this->ipnUrl,
282 2
            'notification_email' => $this->notificationEmail,
283 2
            'confirmation_speed' => $this->confirmationSpeed,
284 2
            'expires_at' => $this->expiresAt,
285 2
            'created_at' => $this->createdAt,
286
        ];
287
    }
288
}
289