PasswordBlacklistValidator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 42
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A validate() 0 10 3
A getBlacklistPasswords() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Groundsix\Password\Validators;
5
6
use Generator;
7
use Groundsix\Password\PasswordException;
8
use Groundsix\Password\Validator;
9
10
class PasswordBlacklistValidator implements Validator
11
{
12
    /** @var string $file */
13
    private $file;
14
15
    public function __construct(string $file = null)
16
    {
17
        if ($file !== null) {
18
            $this->file = $file;
19
        } else {
20
            $this->file = __DIR__ . '/../../res/top_10000.txt';
21
        }
22
    }
23
24
    /** {@inheritdoc} */
25
    public function validate(string $password): bool
26
    {
27
        foreach ($this->getBlacklistPasswords() as $badPassword) {
28
            if ($password === $badPassword) {
29
                throw new PasswordException('Password is blacklisted.');
30
            }
31
        }
32
33
        return true;
34
    }
35
36
    /**
37
     * Iterates over and yields each blacklisted password
38
     *
39
     * @return Generator
40
     */
41
    private function getBlacklistPasswords(): Generator
42
    {
43
        $fh = fopen($this->file, 'rb');
44
45
        while (($password = fgets($fh)) !== false) {
46
            yield trim($password);
47
        }
48
49
        fclose($fh);
50
    }
51
}
52