ImplodeHash::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
namespace kalanis\kw_auth_forms\Rules;
4
5
6
use kalanis\kw_accounts\Interfaces\ICert;
7
use kalanis\kw_accounts\Interfaces\IUser;
8
use kalanis\kw_auth_sources\Interfaces\IStatus;
9
use kalanis\kw_rules\Exceptions\RuleException;
10
use kalanis\kw_rules\Interfaces\IValidate;
11
12
13
/**
14
 * Class ImplodeHash
15
 * @package kalanis\kw_auth_forms\Rules
16
 * Check digest value for preselected inputs - validate hash
17
 */
18
class ImplodeHash extends ARule
19
{
20
    protected IUser $userSource;
21
    protected ICert $certSource;
22
    protected IStatus $libStatus;
23
    protected string $glue = '';
24
    protected string $algorithm = '';
25
26 14
    public function __construct(IUser $userSource, ICert $certSource, IStatus $libStatus, string $glue = '|', string $algorithm = 'md5')
27
    {
28 14
        $this->userSource = $userSource;
29 14
        $this->certSource = $certSource;
30 14
        $this->libStatus = $libStatus;
31 14
        $this->glue = $glue;
32 14
        $this->algorithm = $algorithm;
33 14
    }
34
35 14
    public function validate(IValidate $entry): void
36
    {
37 14
        if (!$this->libStatus->allowCert($this->userSource->getStatus())) {
38 4
            throw new RuleException($this->errorText);
39
        }
40 10
        $col = implode($this->glue, $this->sentInputs() + [$this->certSource->getSalt()]);
41 10
        if (hash($this->algorithm, $col) != strval($entry->getValue())) {
42 3
            throw new RuleException($this->errorText);
43
        }
44 7
    }
45
}
46