Passed
Push — master ( 5a9db6...0ba8a9 )
by Melech
28:27 queued 23:54
created

Adapter2::hashPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Auth\Adapters;
15
16
use Valkyrja\Auth\Adapter2 as Contract;
17
use Valkyrja\Auth\Config\Config;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Auth\Config\Config 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 Valkyrja\Auth\User;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Auth\User 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
20
use const PASSWORD_DEFAULT;
21
22
/**
23
 * Class Adapter.
24
 *
25
 * @author Melech Mizrachi
26
 */
27
abstract class Adapter2 implements Contract
28
{
29
    /**
30
     * Adapter constructor.
31
     *
32
     * @param class-string<User> $user
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<User> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<User>.
Loading history...
33
     */
34
    public function __construct(
35
        protected string $user,
36
        protected Config|array $config
37
    ) {
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    protected function verifyPassword(string $password, string $attemptedPassword): bool
44
    {
45
        return password_verify($attemptedPassword, $password);
46
    }
47
48
    /**
49
     * Determine if a password attempt verifies against a user's existing password.
50
     *
51
     * @param User   $user
52
     * @param string $attemptedPassword
53
     *
54
     * @return bool
55
     */
56
    protected function verifyUserPassword(User $user, string $attemptedPassword): bool
57
    {
58
        return $this->verifyPassword($user->{$user::getPasswordField()}, $attemptedPassword);
59
    }
60
61
    /**
62
     * Hash a plain password.
63
     *
64
     * @param string $password
65
     *
66
     * @return string
67
     */
68
    protected function hashPassword(string $password): string
69
    {
70
        return password_hash($password, PASSWORD_DEFAULT);
71
    }
72
}
73