Invite   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 4
cbo 3
dl 0
loc 114
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

9 Methods

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