CheckIfUserIsConfirmedTask   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 14 4
A loginWithCredentials() 0 9 2
A setUser() 0 4 1
1
<?php
2
3
namespace App\Containers\Authentication\Tasks;
4
5
use App\Containers\Authentication\Exceptions\LoginFailedException;
6
use App\Containers\Authentication\Exceptions\UserNotConfirmedException;
7
use App\Containers\User\Models\User;
8
use App\Ship\Parents\Tasks\Task;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Config;
11
12
class CheckIfUserIsConfirmedTask extends Task
13
{
14
    private $user;
15
16
    public function run()
17
    {
18
        // is the config flag set?
19
        if (Config::get('authentication-container.require_email_confirmation', false)) {
20
21
            if (! $this->user) {
22
                throw new LoginFailedException();
23
            }
24
25
            if (! $this->user->confirmed) {
26
                throw new UserNotConfirmedException();
27
            }
28
        }
29
    }
30
31
    /**
32
     * @param string $username The username / email / whatever to be used
33
     * @param string $password the corresponding password
34
     * @param string $field the field to be checked against
35
     *
36
     * @throws LoginFailedException
37
     */
38
    public function loginWithCredentials($username, $password, $field = 'email')
39
    {
40
        if (Auth::attempt([$field => $username, 'password' => $password])) {
41
            $this->user = Auth::user();
42
        }
43
        else {
44
            throw new LoginFailedException();
45
        }
46
    }
47
48
    public function setUser(User $user)
49
    {
50
        $this->user = $user;
51
    }
52
}
53