Completed
Pull Request — master (#25)
by Bradley
05:48
created

Invite::expires()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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)) {
26
            return false;
27
        }
28
29
        return $this->valid_until->isPast();
30
    }
31
32
    /**
33
     * Get the expiry time.
34
     *
35
     * @return \DateTimeImmutable
36
     */
37
    public function expires()
38
    {
39
        return \DateTimeImmutable::createFromMutable($this->valid_until);
40
    }
41
42
    /**
43
     * Is the invite full.
44
     *
45
     * @return bool
46
     */
47
    public function isFull()
48
    {
49
        if ($this->max == 0) {
50
            return false;
51
        }
52
53
        return $this->uses >= $this->max;
54
    }
55
56
    /**
57
     * Is the invite restricted to a user.
58
     *
59
     * @return bool
60
     */
61
    public function isRestricted()
62
    {
63
        return !is_null($this->for);
64
    }
65
66
67
    /**
68
     * Is the invite restricted for a particular user.
69
     * 
70
     * @param string $email
71
     *
72
     * @return bool
73
     */
74
    public function isRestrictedFor($email)
75
    {
76
        return $email == $this->for;
77
    }
78
79
    /**
80
     * Can the invite be used anymore.
81
     *
82
     * @return bool
83
     */
84
    public function isUseless()
85
    {
86
        return $this->hasExpired() || $this->isFull();
87
    }
88
89
    /**
90
     * Scope a query to only include expired invites.
91
     *
92
     * @param \Illuminate\Database\Eloquent\Builder $query
93
     * @return \Illuminate\Database\Eloquent\Builder
94
     */
95
    public function scopeExpired($query)
96
    {
97
        return $query->where('valid_until', '<', Carbon::now(config('app.timezone')));
98
    }
99
100
    /**
101
     * Scope a query to only include full invites.
102
     *
103
     * @param \Illuminate\Database\Eloquent\Builder $query
104
     * @return \Illuminate\Database\Eloquent\Builder
105
     */
106
    public function scopeFull($query)
107
    {
108
        return $query->where('max', '!=', 0)->whereRaw('uses = max');
109
    }
110
111
    /**
112
     * Scope a query to only include useless invites.
113
     *
114
     * @param \Illuminate\Database\Eloquent\Builder $query
115
     * @return \Illuminate\Database\Eloquent\Builder
116
     */
117
    public function scopeUseless($query)
118
    {
119
        return $query
120
            ->where(function($q) {
121
                $this->scopeExpired($q);
122
            })
123
            ->orWhere(function($q) {
124
                $this->scopeFull($q);
125
            })
126
        ;
127
    }
128
}
129