Test Failed
Pull Request — main (#59)
by Dimitri
06:14
created

Hasher::initialize()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 10
c 2
b 1
f 0
nc 6
nop 1
dl 0
loc 19
ccs 0
cts 7
cp 0
crap 20
rs 9.9332
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Security\Hashing;
13
14
use BlitzPHP\Contracts\Security\HasherInterface;
15
use BlitzPHP\Exceptions\HashingException;
16
17
/**
18
 * Gestionnaire de hashage de BlitzPHP
19
 */
20
class Hasher implements HasherInterface
21
{
22
    /**
23
     * Le hasheur que nous utilisons
24
     */
25
    protected ?HasherInterface $hasher = null;
26
27
    /**
28
     * Le pilote utilisé
29
     */
30
    protected string $driver = '';
31
32
    /**
33
     * Pilotes aux classes de gestionnaires, par ordre de préférence
34
     */
35
    protected array $drivers = [
36
        'bcrypt',
37
        'argon',
38
        'argon2id',
39
    ];
40
41
    /**
42
     * Constructeur
43
     */
44
    public function __construct(protected ?object $config = null)
45
    {
46
        $config ??= (object) config('hashing');
47
48
        $this->config = $config;
49
        $this->driver = $config->driver;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function info(string $hashedValue): array
56
    {
57
        return $this->driver()->info($hashedValue);
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function make(string $value, array $options = []): string
64
    {
65
        return $this->driver()->make($value, $options);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function check(string $value, string $hashedValue, array $options = []): bool
72
    {
73
        return $this->driver()->check($value, $hashedValue, $options);
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function needsRehash(string $hashedValue, array $options = []): bool
80
    {
81
        return $this->driver()->needsRehash($hashedValue, $options);
82
    }
83
84
    /**
85
     * Détermine si une chaîne donnée est déjà hachée.
86
     */
87
    public function isHashed(string $value): bool
88
    {
89
        return $this->driver()->info($value)['algo'] !== null;
90
    }
91
92
    /**
93
     * Vérifie que la configuration est inférieure ou égale à ce qui est configuré.
94
     *
95
     * @internal
96
     */
97
    public function verifyConfiguration(array $value): bool
98
    {
99
        return $this->driver()->verifyConfiguration($value);
0 ignored issues
show
Bug introduced by
The method verifyConfiguration() does not exist on BlitzPHP\Contracts\Security\HasherInterface. It seems like you code against a sub-type of said class. However, the method does not exist in BlitzPHP\Security\Hashing\Handlers\BaseHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

99
        return $this->driver()->/** @scrutinizer ignore-call */ verifyConfiguration($value);
Loading history...
100
    }
101
102
    /**
103
     * Initialiser ou réinitialiser le hasheur
104
     *
105
     * @throws HashingException
106
     */
107
    public function initialize(?object $config = null): HasherInterface
108
    {
109
        if ($config) {
110
            $this->driver = $config->driver;
111
        }
112
113
        if ($this->driver === '') {
114
            throw HashingException::noDriverRequested();
115
        }
116
117
        if (! in_array($this->driver, $this->drivers, true)) {
118
            throw HashingException::unKnownHandler($this->driver);
119
        }
120
121
        $handlerName  = 'BlitzPHP\\Security\\Hashing\\Handlers\\' . ucfirst($this->driver) . 'Handler';
122
        $params       = (array) $config;
123
        $this->hasher = new $handlerName($params[$this->driver]);
124
125
        return $this->hasher;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hasher could return the type null which is incompatible with the type-hinted return BlitzPHP\Contracts\Security\HasherInterface. Consider adding an additional type-check to rule them out.
Loading history...
126
    }
127
128
    private function driver(): HasherInterface
129
    {
130
        if (null === $this->hasher) {
131
            $this->hasher = $this->initialize($this->config);
132
        }
133
134
        return $this->hasher;
135
    }
136
}
137