Completed
Push — master ( e7b516...6a1685 )
by Zura
10:42
created

Promocode   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 122
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A users() 0 5 1
A scopeByCode() 0 4 1
A scopeIsDisposable() 0 4 1
A scopeIsNotDisposable() 0 4 1
A scopeExpired() 0 4 1
A isDisposable() 0 4 1
A isExpired() 0 4 2
1
<?php
2
3
namespace Gabievi\Promocodes\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Promocode extends Model
9
{
10
    /**
11
     * Indicates if the model should be timestamped.
12
     *
13
     * @var bool
14
     */
15
    public $timestamps = false;
16
17
    /**
18
     * The attributes that are mass assignable.
19
     *
20
     * @var array
21
     */
22
    protected $fillable = ['code', 'reward', 'is_disposable', 'expires_at'];
23
24
    /**
25
     * The attributes that should be cast to native types.
26
     *
27
     * @var array
28
     */
29
    protected $casts = [
30
        'is_disposable' => 'boolean',
31
        'data' => 'array',
32
    ];
33
34
    /**
35
     * The attributes that should be mutated to dates.
36
     *
37
     * @var array
38
     */
39
    protected $dates = ['expires_at'];
40
41
    /**
42
     * Promocode constructor.
43
     *
44
     * @param array $attributes
45
     */
46
    public function __construct(array $attributes = [])
47
    {
48
        parent::__construct($attributes);
49
50
        $this->table = config('promocodes.table', 'promocodes');
51
    }
52
53
    /**
54
     * Get the users who is related promocode.
55
     *
56
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
57
     */
58
    public function users()
59
    {
60
        return $this->belongsToMany(config('promocodes.user_model'), config('promocodes.relation_table'))
61
            ->withPivot('used_at');
62
    }
63
64
    /**
65
     * Query builder to find promocode using code.
66
     *
67
     * @param $query
68
     * @param $code
69
     *
70
     * @return mixed
71
     */
72
    public function scopeByCode($query, $code)
73
    {
74
        return $query->where('code', $code);
75
    }
76
77
    /**
78
     * Query builder to get disposable codes.
79
     *
80
     * @param $query
81
     * @return mixed
82
     */
83
    public function scopeIsDisposable($query)
84
    {
85
        return $query->where('is_disposable', true);
86
    }
87
88
    /**
89
     * Query builder to get non-disposable codes.
90
     *
91
     * @param $query
92
     * @return mixed
93
     */
94
    public function scopeIsNotDisposable($query)
95
    {
96
        return $query->where('is_disposable', false);
97
    }
98
99
    /**
100
     * Query builder to get expired promotion codes.
101
     *
102
     * @param $query
103
     * @return mixed
104
     */
105
    public function scopeExpired($query)
106
    {
107
        return $query->whereNotNull('expires_at')->whereDate('expires_at', '<=', Carbon::now());
108
    }
109
110
    /**
111
     * Check if code is disposable (ont-time).
112
     *
113
     * @return bool
114
     */
115
    public function isDisposable()
116
    {
117
        return $this->is_disposable;
118
    }
119
120
    /**
121
     * Check if code is expired.
122
     *
123
     * @return bool
124
     */
125
    public function isExpired()
126
    {
127
        return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false;
128
    }
129
}
130