Completed
Push — 6.13.7 ( b1546d )
by
unknown
14:00
created

PasswordHashGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHashType() 0 4 1
A createPasswordHash() 0 15 3
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\User;
10
11
use eZ\Publish\API\Repository\Values\User\User;
12
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
13
14
/**
15
 * @internal
16
 */
17
final class PasswordHashGenerator implements PasswordHashGeneratorInterface
18
{
19
    /** @var int */
20
    private $hashType;
21
22
    public function __construct(int $hashType = User::DEFAULT_PASSWORD_HASH)
23
    {
24
        $this->hashType = $hashType;
25
    }
26
27
    public function getHashType(): int
28
    {
29
        return $this->hashType;
30
    }
31
32
    public function createPasswordHash(string $password, ?int $hashType = null): string
33
    {
34
        $hashType = $hashType ?? $this->hashType;
35
36
        switch ($hashType) {
37
            case User::PASSWORD_HASH_BCRYPT:
38
                return password_hash($password, PASSWORD_BCRYPT);
39
40
            case User::PASSWORD_HASH_PHP_DEFAULT:
41
                return password_hash($password, PASSWORD_DEFAULT);
42
43
            default:
44
                throw new InvalidArgumentException('hashType', "Password hash type '$hashType' is not recognized");
45
        }
46
    }
47
}
48