ActivationRepository::sendNewActivationEmailApi()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Logic\Activation;
4
5
use App\Models\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
13
class ActivationRepository
14
{
15
    /**
16
     * Creates a token and send email.
17
     *
18
     * @return bool or void
19
     */
20
    public function createTokenAndSendEmail(User $user)
21
    {
22
        Activation::where('user_id', $user->id)
23
            ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
24
            ->count();
25
26
        // if ($activations >= config('settings.maxAttempts')) {
27
        //     return true;
28
        // }
29
30
        //if user changed activated email to new one
31
        if ($user->email_verified_at) {
32
            $user->update([
33
                'email_verified_at' => null,
34
            ]);
35
        }
36
37
        // Create new Activation record for this user
38
        $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

38
        /** @scrutinizer ignore-call */ 
39
        $activation = self::createNewActivationToken($user);
Loading history...
39
40
        // Send activation email notification
41
        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

41
        self::/** @scrutinizer ignore-call */ 
42
              sendNewActivationEmail($user, $activation->token);
Loading history...
42
    }
43
44
    /**
45
     * Creates a new activation token.
46
     *
47
     * @return \App\Models\Activation $activation
48
     */
49
    public function createNewActivationToken(User $user)
50
    {
51
        // $ipAddress = new CaptureIpTrait();
52
        $activation = new Activation();
53
        $activation->user_id = $user->id;
54
        $activation->token = Str::random(64);
55
        // $activation->ip_address = $ipAddress->getClientIp();
56
        $activation->save();
57
58
        return $activation;
59
    }
60
61
    /**
62
     * Sends a new activation email.
63
     *
64
     * @param  \App\Models\User  $user  The user
65
     * @param  string  $token  The token
66
     */
67
    public function sendNewActivationEmail(User $user, $token)
68
    {
69
        $user->notify(new SendActivationEmail($token));
70
    }
71
72
    /**
73
     * Creates a token and send email.- api.
74
     *
75
     * @return bool or void
76
     */
77
    public function createTokenAndSendEmailApi(User $user)
78
    {
79
        $activations = Activation::where('user_id', $user->id)
80
            ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
81
            ->count();
82
83
        if ($activations >= config('settings.maxAttempts')) {
84
            return true;
85
        }
86
87
        //if user changed activated email to new one
88
        if ($user->activated) {
0 ignored issues
show
Bug introduced by
The property activated does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
89
            $user->update([
90
                'activated' => false,
91
            ]);
92
        }
93
94
        // Create new Activation record for this user
95
        $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

95
        /** @scrutinizer ignore-call */ 
96
        $activation = self::createNewActivationTokenApi($user);
Loading history...
96
97
        // Send activation email notification
98
        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

98
        self::/** @scrutinizer ignore-call */ 
99
              sendNewActivationEmailApi($user, $activation->token);
Loading history...
99
    }
100
101
    /**
102
     * Creates a new activation token.
103
     *
104
     * @return \App\Models\Activation $activation
105
     */
106
    public function createNewActivationTokenApi(User $user)
107
    {
108
        $token = sprintf('%06d', random_int(1, 999999));
109
        // $ipAddress = new CaptureIpTrait();
110
        $activation = new Activation();
111
        $activation->user_id = $user->id;
112
        $activation->token = $token;
113
        // $activation->ip_address = $ipAddress->getClientIp();
114
        $activation->save();
115
116
        return $activation;
117
    }
118
119
    /**
120
     * Sends a new activation email.
121
     *
122
     * @param  \App\Models\User  $user  The user
123
     * @param  string  $token  The token
124
     */
125
    public function sendNewActivationEmailApi(User $user, $token)
126
    {
127
        $user->notify(new SendActivationEmailApi($token));
128
    }
129
130
    /**
131
     * Method to removed expired activations.
132
     *
133
     * @return void
134
     */
135
    public function deleteExpiredActivations()
136
    {
137
        Activation::where('created_at', '<=', Carbon::now()->subHours(72))->delete();
138
    }
139
}
140