Completed
Push — master ( a33463...978786 )
by Ashley
01:40
created

Invite::setForAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Clarkeash\Doorman\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Invite extends Model
9
{
10
    protected $dates = [ 'valid_until' ];
11
    
12 37
    public function __construct(array $attributes = [ ])
13
    {
14 37
        $this->table = config('doorman.invite_table_name');
15 37
        parent::__construct($attributes);
16 37
    }
17
18 18
    public function setForAttribute($for)
19
    {
20 18
        $this->attributes['for'] = strtolower($for);
21 18
    }
22
23
    /**
24
     * Has the invite expired.
25
     *
26
     * @return bool
27
     */
28 19
    public function hasExpired()
29
    {
30 19
        if (is_null($this->valid_until)) {
31 16
            return false;
32
        }
33
34 5
        return $this->valid_until->isPast();
35
    }
36
37
    /**
38
     * Is the invite full.
39
     *
40
     * @return bool
41
     */
42 21
    public function isFull()
43
    {
44 21
        if ($this->max == 0) {
45 4
            return false;
46
        }
47
48 19
        return $this->uses >= $this->max;
49
    }
50
51
    /**
52
     * Is the invite restricted to a user.
53
     *
54
     * @return bool
55
     */
56 16
    public function isRestricted()
57
    {
58 16
        return !is_null($this->for);
59
    }
60
61
62
    /**
63
     * Is the invite restricted for a particular user.
64
     * 
65
     * @param string $email
66
     *
67
     * @return bool
68
     */
69 10
    public function isRestrictedFor($email)
70
    {
71 10
        return strtolower($email) == $this->for;
72
    }
73
74
    /**
75
     * Can the invite be used anymore.
76
     *
77
     * @return bool
78
     */
79 1
    public function isUseless()
80
    {
81 1
        return $this->hasExpired() || $this->isFull();
82
    }
83
84
    /**
85
     * Scope a query to only include expired invites.
86
     *
87
     * @param \Illuminate\Database\Eloquent\Builder $query
88
     * @return \Illuminate\Database\Eloquent\Builder
89
     */
90 3
    public function scopeExpired($query)
91
    {
92 3
        return $query->where('valid_until', '<', Carbon::now(config('app.timezone')));
93
    }
94
95
    /**
96
     * Scope a query to only include full invites.
97
     *
98
     * @param \Illuminate\Database\Eloquent\Builder $query
99
     * @return \Illuminate\Database\Eloquent\Builder
100
     */
101 3
    public function scopeFull($query)
102
    {
103 3
        return $query->where('max', '!=', 0)->whereRaw('uses = max');
104
    }
105
106
    /**
107
     * Scope a query to only include useless invites.
108
     *
109
     * @param \Illuminate\Database\Eloquent\Builder $query
110
     * @return \Illuminate\Database\Eloquent\Builder
111
     */
112
    public function scopeUseless($query)
113
    {
114
        return $query
115 2
            ->where(function($q) {
116 2
                $this->scopeExpired($q);
117 2
            })
118 2
            ->orWhere(function($q) {
119 2
                $this->scopeFull($q);
120 2
            })
121
        ;
122
    }
123
}
124