Completed
Pull Request — master (#16)
by Bernhard
02:06
created

PaymentRequest::confirmationSpeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.216

Importance

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