Failed Conditions
Pull Request — master (#322)
by Anton
05:37 queued 03:16
created

Equals::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 2
cp 0.5
c 0
b 0
f 0
cc 1
crap 1.125
rs 10
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\Model;
13
use Application\Auth\Row;
14
use Application\Auth\Table;
15
use Application\Users\Row as User;
16
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...
17
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...
18
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...
19
use Bluz\Db\Exception\TableNotFoundException;
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Exception\TableNotFoundException 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
21
/**
22
 * Equals Provider
23
 *
24
 * @package  Application\Auth\Provider
25
 */
26
class Equals extends AbstractProvider
27
{
28
    public const PROVIDER = Table::PROVIDER_EQUALS;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws AuthException
34 2
     * @throws DbException
35
     * @throws InvalidPrimaryKeyException
36 2
     */
37 1
    public static function authenticate(string $login, string $password = null): Row
38
    {
39
        $authRow = self::find($login);
40 1
41 1
        self::verify($authRow, $password);
42
43
        self::login($authRow);
44
45
        return $authRow;
46
    }
47
48
49
    /**
50
     * {@inheritdoc}
51
     *
52 4
     * @return Row
53
     * @throws DbException
54
     * @throws AuthException
55 4
     */
56
    public static function find(string $login): ?Row
57 4
    {
58
        return Table::findRowWhere(['foreignKey' => $login, 'provider' => self::PROVIDER]);
59
    }
60
61
    /**
62 4
     * {@inheritdoc}
63 2
     *
64
     * @param Row|null $authRow
65
     * @return void
66 2
     * @throws AuthException
67
     */
68
    protected static function verify(?Row $authRow, string $password = null): void
69
    {
70
        if (!$authRow) {
71
            throw new AuthException('User can\'t login with password');
72
        }
73
74
        // verify password
75
        if (!Model::verify($password, $authRow->token)) {
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type null; however, parameter $password of Application\Auth\Model::verify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        if (!Model::verify(/** @scrutinizer ignore-type */ $password, $authRow->token)) {
Loading history...
76
            throw new AuthException('Wrong password');
77
        }
78
    }
79
80
    /**
81
     * @param User $user
82
     * @param string $password
83
     * @return Row
84
     * @throws DbException
85
     * @throws InvalidPrimaryKeyException
86
     * @throws TableNotFoundException
87
     */
88
    public static function create(User $user, string $password = ''): Row
89
    {
90
        // remove old Auth record
91
        self::remove($user->id);
92
93
        // create new auth row
94
        $row = new Row();
95
96
        $row->userId = $user->id;
97
        $row->foreignKey = $user->login;
98
        $row->provider = self::PROVIDER;
99
        $row->tokenType = Table::TYPE_ACCESS;
100
        // generate secret part is not required
101
        // encrypt password and save as token
102
        $row->token = Model::hash($password);
103
104
        $row->save();
105
106
        return $row;
107
    }
108
}
109