Completed
Push — master ( 155f87...48c82e )
by Orkhan
03:25 queued 15s
created

PaymentQueryBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 32
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A whereSuccessful() 0 3 1
A wherePending() 0 10 1
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay\QueryBuilders;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
8
9
class PaymentQueryBuilder extends Builder
10
{
11
    /**
12
     * "whereSuccessful()" scope to filter only successful payments.
13
     * Successful payments are payments with "status" field value equal to STATUS_SUCCESSFUL constant value.
14
     *
15
     * @return PaymentQueryBuilder
16
     */
17
    public function whereSuccessful(): PaymentQueryBuilder
18
    {
19
        return $this->where('status', Payment::STATUS_SUCCESSFUL);
20
    }
21
22
    /**
23
     * "wherePending()" scope to filter only pending payments.
24
     * Pending payments are:
25
     * "status" field anything other than STATUS_SUCCESSFUL constant value,
26
     * PLUS
27
     * "checks" field less than MINIMUM_REQUIRED_CHECKS constant value OR "created_at" timestamp less than 30 minutes.
28
     *
29
     * @return PaymentQueryBuilder
30
     */
31
    public function wherePending(): PaymentQueryBuilder
32
    {
33
        return $this
34
            ->where(function (Builder $query) {
35
                $query->whereNull('status')
36
                    ->orWhere('status', '<>', Payment::STATUS_SUCCESSFUL);
37
            })
38
            ->where(function (Builder $query) {
39
                $query->where('checks', '<', Payment::MINIMUM_REQUIRED_CHECKS)
40
                    ->orWhere('created_at', '>=', Carbon::now()->subMinutes(30));
41
            });
42
    }
43
}
44