Completed
Push — master ( b1c4b6...a05019 )
by Şəhriyar
19s
created

Activates::expires()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace App\Traits\Users;
2
3
use App\Models\User;
4
use App\Models\UserActivation;
5
use Carbon\Carbon;
6
7
trait Activates
8
{
9
    /**
10
     * @param \App\Models\User $user
11
     *
12
     * @return \App\Models\UserActivation
13
     */
14 1
    protected function create(User $user)
15
    {
16 1
        $activation = new UserActivation();
17 1
        $code = $this->generateActivationCode();
18 1
        $activation->fill(compact('code'));
19 1
        $activation->user_id = $user->id;
20 1
        $activation->save();
21
22 1
        return $activation;
23
    }
24
25
    /**
26
     * @param \App\Models\User $user
27
     * @param string|null      $code
28
     *
29
     * @return bool
30
     */
31 2
    protected function exists(User $user, $code = null)
32
    {
33 2
        $expires = $this->expires();
34 2
        $activation = UserActivation::where('user_id', $user->id)->where('completed', false)->where('created_at', '>', $expires);
35 2
        if ($code) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $code of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
36
            $activation->where('code', $code);
37
        }
38
39 2
        return $activation->first() ?: false;
40
    }
41
42
    /**
43
     * Returns the expiration date.
44
     *
45
     * @return \Carbon\Carbon
46
     */
47 3
    protected function expires()
48
    {
49 3
        $expires = config('auth.activations.expires', 259200);
50
51 3
        return Carbon::now()->subSeconds($expires);
52
    }
53
54
    /**
55
     * @param \App\Models\User $user
56
     *
57
     * @return bool
58
     */
59 2
    protected function completed(User $user)
60
    {
61 2
        $activation = UserActivation::where('user_id', $user->id)->where('completed', true)->first();
62
63 2
        return $activation ?: false;
64
    }
65
66
    /**
67
     * @param \App\Models\User $user
68
     * @param string           $code
69
     *
70
     * @return bool
71
     */
72 1
    public function complete(User $user, $code)
73
    {
74 1
        $expires = $this->expires();
75 1
        $activation = UserActivation::where('user_id', $user->id)->where('code', $code)->where('completed', false)->where('created_at', '>', $expires)->first();
76 1
        if ($activation === null) {
77
            return false;
78
        }
79 1
        $activation->fill([
80 1
            'completed' => true,
81 1
            'completed_at' => Carbon::now(),
82 1
        ]);
83 1
        $activation->save();
84
85 1
        return true;
86
    }
87
88
    /**
89
     * Return a random string for an activation code.
90
     *
91
     * @return string
92
     */
93 1
    protected function generateActivationCode()
94
    {
95 1
        return str_random(32);
96
    }
97
98
    /**
99
     * Get the e-mail subject line to be used for the reset link email.
100
     *
101
     * @return string
102
     */
103 2
    protected function getActivationEmailSubject()
104
    {
105 2
        return trans('auth.activation_email_subject');
106
    }
107
}
108