Passed
Push — master ( bf8929...f56605 )
by Smoren
02:31
created

WrappedCheck::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Checks;
6
7
use Smoren\Validator\Exceptions\CheckError;
8
use Smoren\Validator\Interfaces\CheckInterface;
9
10
final class WrappedCheck implements CheckInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected string $name;
16
    /**
17
     * @var CheckInterface
18
     */
19
    protected CheckInterface $check;
20
    /**
21
     * @var callable
22
     */
23
    protected $errorHandler;
24
25
    /**
26
     * @param string $name
27
     * @param CheckInterface $check
28
     * @param callable $errorHandler
29
     */
30 2
    public function __construct(string $name, CheckInterface $check, callable $errorHandler)
31
    {
32 2
        $this->name = $name;
33 2
        $this->check = $check;
34 2
        $this->errorHandler = $errorHandler;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 2
    public function __invoke($value, array $previousErrors, bool $preventDuplicate = false): void
41
    {
42
        try {
43 2
            ($this->check)($value, $previousErrors, $preventDuplicate);
44 1
        } catch (CheckError $e) {
45 1
            throw ($this->errorHandler)($e, $this->name);
46
        }
47
    }
48
}
49