Passed
Push — main ( 4b3ce9...6dd303 )
by Michael
03:57
created

Coupon::isDisposable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Couponables\Models;
6
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Model;
9
use MichaelRubel\Couponables\Models\Contracts\CouponContract;
10
use MichaelRubel\Couponables\Models\Traits\DefinesColumns;
11
12
class Coupon extends Model implements CouponContract
13
{
14
    use HasFactory, DefinesColumns;
15
16
    /**
17
     * The attributes that aren't mass assignable.
18
     *
19
     * @var array<string>|bool
20
     */
21
    protected $guarded = [];
22
23
    /**
24
     * The attributes that should be cast.
25
     *
26
     * @var array
27
     */
28
    protected $casts = [
29
        'code'     => 'string',
30
        'type'     => 'string',
31
        'data'     => 'collection',
32
        'quantity' => 'integer',
33
        'limit'    => 'integer',
34
        'datetime' => 'datetime',
35
    ];
36
37
    /**
38
     * @param array $attributes
39
     */
40 25
    public function __construct(array $attributes = [])
41
    {
42 25
        parent::__construct($attributes);
43
44 25
        $this->table = config('couponables.table', 'coupons');
45
    }
46
47
    /**
48
     * The only model allowed to redeem the code if assigned.
49
     *
50
     * @return Model|null
51
     */
52 2
    public function redeemer(): ?Model
53
    {
54 2
        return $this->isMorphColumnsFilled()
55 2
            ? $this->morphTo()->first()
56 2
            : null;
57
    }
58
59
    /**
60
     * Check if code is expired.
61
     *
62
     * @return bool
63
     */
64 17
    public function isExpired(): bool
65
    {
66 17
        $expires_at = $this->{call(CouponContract::class)->getExpiresAtColumn()};
67
68 17
        return $expires_at && now()->gte($expires_at);
69
    }
70
71
    /**
72
     * Check if code is not expired.
73
     *
74
     * @return bool
75
     */
76 1
    public function isNotExpired(): bool
77
    {
78 1
        return ! call($this)->isExpired();
79
    }
80
81
    /**
82
     * Check if code amount is over.
83
     *
84
     * @return bool
85
     */
86 14
    public function isOverQuantity(): bool
87
    {
88 14
        $quantity = $this->{call(CouponContract::class)->getQuantityColumn()};
89
90 14
        return ! is_null($quantity) && $quantity <= 0;
91
    }
92
93
    /**
94
     * Check if coupon is already redeemed by the model.
95
     *
96
     * @param Model $redeemer
97
     *
98
     * @return bool
99
     */
100 2
    public function isRedeemedBy(Model $redeemer): bool
101
    {
102 2
        $column = call(CouponContract::class)->getCodeColumn();
103 2
        $code   = $this->{$column};
104
105 2
        return ! is_null($code) && $redeemer
106 2
            ->coupons()
107 2
            ->where($column, $code)
108 2
            ->exists();
109
    }
110
111
    /**
112
     * Check if coupon is disposable.
113
     *
114
     * @return bool
115
     */
116 14
    public function isDisposable(): bool
117
    {
118 14
        $limit = $this->{call(CouponContract::class)->getLimitColumn()};
119
120 14
        return ! is_null($limit) && single($limit);
121
    }
122
123
    /**
124
     * Check if the code is reached its limit for the passed model.
125
     *
126
     * @param Model $redeemer
127
     *
128
     * @return bool
129
     */
130 14
    public function isOverLimitFor(Model $redeemer): bool
131
    {
132 14
        $column = call(CouponContract::class)->getCodeColumn();
133 14
        $limit  = $this->{call(CouponContract::class)->getLimitColumn()};
134
135 14
        return ! is_null($limit) && $limit < $redeemer
136 5
            ->coupons()
137 5
            ->where($column, $this->{$column})
138 14
            ->count();
139
    }
140
}
141