ActivationRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 20
c 2
b 1
f 0
dl 0
loc 46
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createNewActivationToken() 0 10 1
A deleteExpiredActivations() 0 3 1
A sendNewActivationEmail() 0 3 1
A createTokenAndSendEmail() 0 22 3
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\Traits\CaptureIpTrait;
9
use Carbon\Carbon;
10
11
class ActivationRepository
12
{
13
    public function createTokenAndSendEmail(User $user)
14
    {
15
        $activations = Activation::where('user_id', $user->id)
16
            ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
17
            ->count();
18
19
        if ($activations >= config('settings.maxAttempts')) {
20
            return true;
21
        }
22
23
        //if user changed activated email to new one
24
        if ($user->activated) {
25
            $user->update([
26
                'activated' => false,
27
            ]);
28
        }
29
30
        // Create new Activation record for this user
31
        $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

31
        /** @scrutinizer ignore-call */ 
32
        $activation = self::createNewActivationToken($user);
Loading history...
32
33
        // Send activation email notification
34
        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

34
        self::/** @scrutinizer ignore-call */ 
35
              sendNewActivationEmail($user, $activation->token);
Loading history...
35
    }
36
37
    public function createNewActivationToken(User $user)
38
    {
39
        $ipAddress = new CaptureIpTrait();
40
        $activation = new Activation();
41
        $activation->user_id = $user->id;
42
        $activation->token = str_random(64);
43
        $activation->ip_address = $ipAddress->getClientIp();
44
        $activation->save();
45
46
        return $activation;
47
    }
48
49
    public function sendNewActivationEmail(User $user, $token)
50
    {
51
        $user->notify(new SendActivationEmail($token));
52
    }
53
54
    public function deleteExpiredActivations()
55
    {
56
        Activation::where('created_at', '<=', Carbon::now()->subHours(72))->delete();
57
    }
58
}
59