Username::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/*
3
    Password Strength Library
4
    Copyright (C) 2019 CustomerGauge
5
    [email protected]
6
7
    This program is free software; you can redistribute it and/or
8
    modify it under the terms of the GNU Lesser General Public
9
    License as published by the Free Software Foundation; either
10
    version 3 of the License, or (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
    Lesser General Public License for more details.
16
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with this program; if not, write to the Free Software Foundation,
19
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
*/
21
22
declare(strict_types=1);
23
24
namespace CustomerGauge\Password\Rule;
25
26
use CustomerGauge\Password\Exception\InvalidName;
27
use CustomerGauge\Password\Rule;
28
29
use function mb_strpos;
30
use function mb_strtolower;
31
use function mb_substr;
32
33
final class Username implements Rule
34
{
35
    public const INICIAL_SIZE = 4;
36
37
    /** @var string[] */
38
    private array $usernames;
39
40
    private string $encoding;
41
42
    /**
43
     * @param string[] $usernames
44
     */
45 2
    public function __construct(array $usernames, string $encoding = 'utf8')
46
    {
47 2
        $this->usernames = $usernames;
48 2
        $this->encoding  = $encoding;
49 2
    }
50
51 2
    public function __invoke(string $password): void
52
    {
53 2
        foreach ($this->usernames as $username) {
54 2
            $this->find($password, $username);
55
56 1
            $this->findNormalizedUsername($password, $username);
57
58 1
            $this->findInicialUsername($password, $username);
59
        }
60
    }
61
62 2
    private function find(string $password, string $username): void
63
    {
64 2
        if (mb_strpos($password, $username, 0, $this->encoding) !== false) {
65 2
            throw InvalidName::contains($username);
66
        }
67 1
    }
68
69 1
    private function findNormalizedUsername(string $password, string $username): void
70
    {
71 1
        $username = mb_strtolower($username, $this->encoding);
72 1
        $password = mb_strtolower($password, $this->encoding);
73
74 1
        $this->find($password, $username);
75 1
    }
76
77 1
    private function findInicialUsername(string $password, string $username): void
78
    {
79 1
        $username = mb_strtolower($username, $this->encoding);
80 1
        $password = mb_strtolower($password, $this->encoding);
81 1
        $inicials = mb_substr($username, 0, self::INICIAL_SIZE, $this->encoding);
82
83 1
        $this->find($password, $inicials);
84
    }
85
}
86