Voucher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A users() 0 4 1
A model() 0 4 1
A isExpired() 0 4 2
A isNotExpired() 0 4 1
1
<?php
2
3
namespace BeyondCode\Vouchers\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Voucher extends Model
9
{
10
    /**
11
     * The attributes that are mass assignable.
12
     *
13
     * @var array
14
     */
15
    protected $fillable = [
16
        'model_id',
17
        'model_type',
18
        'code',
19
        'data',
20
        'expires_at'
21
    ];
22
23
    /**
24
     * The attributes that should be mutated to dates.
25
     *
26
     * @var array
27
     */
28
    protected $dates = [
29
        'expires_at'
30
    ];
31
32
    /**
33
     * The attributes that should be cast to native types.
34
     *
35
     * @var array
36
     */
37
    protected $casts = [
38
        'data' => 'collection'
39
    ];
40
41
    public function __construct(array $attributes = [])
42
    {
43
        parent::__construct($attributes);
44
45
        $this->table = config('vouchers.table', 'vouchers');
46
    }
47
48
    /**
49
     * Get the users who redeemed this voucher.
50
     *
51
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
52
     */
53
    public function users()
54
    {
55
        return $this->belongsToMany(config('vouchers.user_model'), config('vouchers.relation_table'))->withPivot('redeemed_at');
56
    }
57
58
    /**
59
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
60
     */
61
    public function model()
62
    {
63
        return $this->morphTo();
64
    }
65
66
    /**
67
     * Check if code is expired.
68
     *
69
     * @return bool
70
     */
71
    public function isExpired()
72
    {
73
        return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false;
74
    }
75
76
    /**
77
     * Check if code is not expired.
78
     *
79
     * @return bool
80
     */
81
    public function isNotExpired()
82
    {
83
        return ! $this->isExpired();
84
    }
85
}
86