AbstractProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 84
ccs 6
cts 6
cp 1
c 1
b 0
f 0
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 9 1
A login() 0 16 5
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link      https://github.com/bluzphp/skeleton
6
 */
7
8
declare(strict_types=1);
9
10
namespace Application\Auth\Provider;
11
12
use Application\Auth\Row;
13
use Application\Auth\Table;
14
use Application\Exception;
15
use Application\Users\Row as User;
16
use Application\Users\Table as Users;
17
use Bluz\Auth\AuthException;
0 ignored issues
show
Bug introduced by
The type Bluz\Auth\AuthException 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...
18
use Bluz\Db\Exception\DbException;
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Exception\DbException 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...
19
use Bluz\Db\Exception\InvalidPrimaryKeyException;
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Exception\InvalidPrimaryKeyException 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...
20
use Bluz\Proxy\Auth;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Auth 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...
21
22
/**
23
 * AbstractProvider
24
 *
25
 * @package  Application\Auth\Provider
26
 */
27
abstract class AbstractProvider
28
{
29
    public const PROVIDER = 'abstract';
30
31
    /**
32
     * Authenticate user by token
33
     *
34
     * @param string $token
35
     * @return Row
36
     * @throws Exception
37
     * @throws AuthException
38
     */
39
    abstract public static function authenticate(string $token): Row;
40
41
    /**
42
     * Find AuthRow by token
43
     *
44
     * @param string $token
45
     * @return Row
46
     */
47
    abstract protected static function find(string $token): ?Row;
48
49
    /**
50
     * Check if supplied token is valid
51
     *
52
     * @param Row|null $row
53
     * @return void
54
     * @throws AuthException
55
     */
56
    abstract protected static function verify(?Row $row): void;
57
58
    /**
59
     * @param Row $authRow
60
     * @throws AuthException
61
     * @throws DbException
62
     * @throws InvalidPrimaryKeyException
63
     */
64 2
    public static function login(Row $authRow): void
65
    {
66
        $user = Users::findRow($authRow->userId);
67
68 2
        switch ($user->status) {
69
            case (Users::STATUS_PENDING):
70 2
                throw new AuthException('Your account is pending activation', 403);
71 2
            case (Users::STATUS_DISABLED):
72 2
                throw new AuthException('Your account is disabled by administrator', 403);
73
            case (Users::STATUS_ACTIVE):
74
                // save user to new session
75 2
                Auth::setIdentity($user);
76
                break;
77
            case (Users::STATUS_DELETED):
78
            default:
79
                throw new AuthException('User not found');
80
        }
81
    }
82
83
    /**
84
     * Create and save Auth record
85
     *
86
     * @param User $user
87
     *
88
     * @return Row
89
     * @throws Exception
90
     * @throws AuthException
91
     */
92
    abstract public static function create(User $user): Row;
93
94
    /**
95
     * Remove Auth record by user ID
96
     *
97
     * @param int|null $id User ID
98
     *
99
     * @return void
100
     * @throws DbException
101
     */
102
    public static function remove(?int $id): void
103
    {
104
        // clear previous generated Auth record
105
        // works with change password
106
        Table::delete(
107
            [
108
                'userId' => $id,
109
                'provider' => static::PROVIDER,
110
                'tokenType' => Table::TYPE_ACCESS
111
            ]
112
        );
113
    }
114
}
115