Passed
Push — master ( 7a2ae7...77adf5 )
by Curtis
12:45 queued 06:15
created

ActivationRepository::createTokenAndSendEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 8
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 22
rs 10
1
<?php
2
3
namespace App\Logic\Activation;
4
5
use App\Activation;
6
use App\Models\User;
7
use App\Notifications\SendActivationEmail;
8
use App\Notifications\SendActivationEmailApi;
0 ignored issues
show
Bug introduced by
The type App\Notifications\SendActivationEmailApi was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
// use App\Traits\CaptureIpTrait;
10
use Carbon\Carbon;
11
use Str;
12
class ActivationRepository
13
{
14
    /**
15
     * Creates a token and send email.
16
     *
17
     * @param \App\Models\User $user
18
     *
19
     * @return bool or void
20
     */
21
    public function createTokenAndSendEmail(User $user)
22
    {
23
        $activations = Activation::where('user_id', $user->id)
0 ignored issues
show
Unused Code introduced by
The assignment to $activations is dead and can be removed.
Loading history...
24
            ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
25
            ->count();
26
27
        // if ($activations >= config('settings.maxAttempts')) {
28
        //     return true;
29
        // }
30
31
        //if user changed activated email to new one
32
        if ($user->email_verified_at) {
0 ignored issues
show
Bug Best Practice introduced by
The property email_verified_at does not exist on App\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
            $user->update([
34
                'email_verified_at' => null,
35
            ]);
36
        }
37
38
        // Create new Activation record for this user
39
        $activation = self::createNewActivationToken($user);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Logic\Activation\Act...ateNewActivationToken() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        /** @scrutinizer ignore-call */ 
40
        $activation = self::createNewActivationToken($user);
Loading history...
40
41
        // Send activation email notification
42
        self::sendNewActivationEmail($user, $activation->token);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Logic\Activation\Act...endNewActivationEmail() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        self::/** @scrutinizer ignore-call */ 
43
              sendNewActivationEmail($user, $activation->token);
Loading history...
43
    }
44
45
46
    
47
    /**
48
     * Creates a new activation token.
49
     *
50
     * @param \App\Models\User $user
51
     *
52
     * @return \App\Models\Activation $activation
0 ignored issues
show
Bug introduced by
The type App\Models\Activation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
     */
54
    public function createNewActivationToken(User $user)
55
    {
56
        // $ipAddress = new CaptureIpTrait();
57
        $activation = new Activation();
58
        $activation->user_id = $user->id;
59
        $activation->token = Str::random(64);
60
        // $activation->ip_address = $ipAddress->getClientIp();
61
        $activation->save();
62
63
        return $activation;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $activation returns the type App\Activation which is incompatible with the documented return type App\Models\Activation.
Loading history...
64
    }
65
66
    /**
67
     * Sends a new activation email.
68
     *
69
     * @param \App\Models\User $user  The user
70
     * @param string           $token The token
71
     */
72
    public function sendNewActivationEmail(User $user, $token)
73
    {
74
        $user->notify(new SendActivationEmail($token));
75
    }
76
77
78
     /**
79
     * Creates a token and send email.- api
80
     *
81
     * @param \App\Models\User $user
82
     *
83
     * @return bool or void
84
     */
85
    public function createTokenAndSendEmailApi(User $user)
86
    {
87
        $activations = Activation::where('user_id', $user->id)
88
            ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
89
            ->count();
90
91
        if ($activations >= config('settings.maxAttempts')) {
92
            return true;
93
        }
94
95
        //if user changed activated email to new one
96
        if ($user->activated) {
0 ignored issues
show
Bug Best Practice introduced by
The property activated does not exist on App\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
97
            $user->update([
98
                'activated' => false,
99
            ]);
100
        }
101
102
        // Create new Activation record for this user
103
        $activation = self::createNewActivationTokenApi($user);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Logic\Activation\Act...NewActivationTokenApi() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        /** @scrutinizer ignore-call */ 
104
        $activation = self::createNewActivationTokenApi($user);
Loading history...
104
105
        // Send activation email notification
106
        self::sendNewActivationEmailApi($user, $activation->token);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Logic\Activation\Act...NewActivationEmailApi() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        self::/** @scrutinizer ignore-call */ 
107
              sendNewActivationEmailApi($user, $activation->token);
Loading history...
107
    }
108
109
    /**
110
     * Creates a new activation token.
111
     *
112
     * @param \App\Models\User $user
113
     *
114
     * @return \App\Models\Activation $activation
115
     */
116
    public function createNewActivationTokenApi(User $user)
117
    {
118
        $token = sprintf("%06d", mt_rand(1, 999999));
119
        // $ipAddress = new CaptureIpTrait();
120
        $activation = new Activation();
121
        $activation->user_id = $user->id;
122
        $activation->token = $token;
123
        // $activation->ip_address = $ipAddress->getClientIp();
124
        $activation->save();
125
126
        return $activation;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $activation returns the type App\Activation which is incompatible with the documented return type App\Models\Activation.
Loading history...
127
    }
128
129
    /**
130
     * Sends a new activation email.
131
     *
132
     * @param \App\Models\User $user  The user
133
     * @param string           $token The token
134
     */
135
    public function sendNewActivationEmailApi(User $user, $token)
136
    {
137
        $user->notify(new SendActivationEmailApi($token));
138
    }
139
140
    /**
141
     * Method to removed expired activations.
142
     *
143
     * @return void
144
     */
145
    public function deleteExpiredActivations()
146
    {
147
        Activation::where('created_at', '<=', Carbon::now()->subHours(72))->delete();
148
    }
149
}
150