Completed
Push — master ( 399dd2...21b117 )
by ARCANEDEV
11s
created

Confirmable::scopeConfirmed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models\Traits;
2
3
use Arcanedev\LaravelAuth\Exceptions\UserConfirmationException;
4
use Arcanedev\LaravelAuth\Services\UserConfirmator;
5
use Illuminate\Database\Eloquent\Builder;
6
7
/**
8
 * Trait     Confirmable
9
 *
10
 * @package  Arcanedev\LaravelAuth\Models\Traits
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  string|null          confirmation_code  (Optional)
14
 * @property  \Carbon\Carbon|null  confirmed_at       (Optional)
15
 * @property  bool                 is_confirmed       (Optional)
16
 *
17
 * @method  static  \Illuminate\Database\Eloquent\Builder  confirmed()
18
 * @method  static  \Illuminate\Database\Eloquent\Builder  unconfirmed(string $code = null)
19
 */
20
trait Confirmable
21
{
22
    /* -----------------------------------------------------------------
23
     |  Scopes
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * Scope confirmed users.
29
     *
30
     * @param  \Illuminate\Database\Eloquent\Builder  $query
31
     *
32
     * @return \Illuminate\Database\Eloquent\Builder
33
     */
34 3
    public function scopeConfirmed(Builder $query)
35
    {
36 3
        return $query->whereNull('confirmation_code')
37 3
                     ->whereNotNull('confirmed_at');
38
    }
39
40
    /**
41
     * Scope unconfirmed users by code.
42
     *
43
     * @param  \Illuminate\Database\Eloquent\Builder  $query
44
     * @param  string                                 $code
45
     *
46
     * @return \Illuminate\Database\Eloquent\Builder
47
     */
48 15
    public function scopeUnconfirmed(Builder $query, $code = null)
49
    {
50 15
        return $query->whereNotNull('confirmation_code')
51
                     ->unless(is_null($code), function (Builder $q) use ($code) {
52 9
                         return $q->where('confirmation_code', $code);
53 15
                     })
54 15
                     ->whereNull('confirmed_at');
55
    }
56
57
    /* -----------------------------------------------------------------
58
     |  Getters & Setters
59
     | -----------------------------------------------------------------
60
     */
61
62
    /**
63
     * Get the `is_confirmed` attribute.
64
     *
65
     * @return bool
66
     */
67 9
    public function getIsConfirmedAttribute()
68
    {
69 9
        return $this->isConfirmed();
70
    }
71
72
    /* -----------------------------------------------------------------
73
     |  Main Methods
74
     | -----------------------------------------------------------------
75
     */
76
77
    /**
78
     * Confirm the unconfirmed user account by confirmation code.
79
     *
80
     * @param  string  $code
81
     *
82
     * @return \Arcanesoft\Contracts\Auth\Models\User
83
     *
84
     * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException
85
     */
86 15
    public static function findUnconfirmed($code)
87
    {
88
        /** @var  \Arcanesoft\Contracts\Auth\Models\User|null  $unconfirmed */
89 15
        $unconfirmed = static::unconfirmed($code)->first();
90
91 15
        if ( ! $unconfirmed instanceof self)
92 3
            throw (new UserConfirmationException)->setModel(static::class);
93
94 12
        return $unconfirmed;
95
    }
96
97
    /**
98
     * Confirm the new user account.
99
     *
100
     * @param  \Arcanesoft\Contracts\Auth\Models\User|string  $code
101
     *
102
     * @return \Arcanesoft\Contracts\Auth\Models\User
103
     */
104 9
    public static function confirm($code)
105
    {
106 9
        if ($code instanceof self)
107 6
            $code = $code->confirmation_code;
108
109 9
        return (new UserConfirmator)->confirm(
110 9
            static::findUnconfirmed($code)
111
        );
112
    }
113
114
    /* -----------------------------------------------------------------
115
     |  Check Methods
116
     | -----------------------------------------------------------------
117
     */
118
119
    /**
120
     * Check if user has a confirmed account.
121
     *
122
     * @return bool
123
     */
124 9
    public function isConfirmed()
125
    {
126 9
        return ! is_null($this->confirmed_at);
127
    }
128
}
129