Completed
Push — master ( baa05d...28c5af )
by Ashley
02:55
created

Invite::scopeUseless()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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