ImplodeKeys::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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