Passed
Push — master ( 30b9f9...28ee83 )
by Magnar Ovedal
02:55
created

PolicyException::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice;
6
7
use RuntimeException;
8
use Stadly\PasswordPolice\Rule\RuleException;
9
use Symfony\Component\Translation\Translator;
10
use Throwable;
11
12
/**
13
 * Exception thrown when a policy could not be enforced.
14
 */
15
final class PolicyException extends RuntimeException
16
{
17
    /**
18
     * @var Policy Policy that could not be enforced.
19
     */
20
    private $policy;
21
22
    /**
23
     * @var RuleException[] Exceptions thrown by rules that could not be enforced.
24
     */
25
    private $ruleExceptions = [];
26
27
    /**
28
     * @param Policy $policy Policy that could not be enforced.
29
     * @param RuleException[] $ruleExceptions Exceptions thrown by rules that could not be enforced.
30
     * @param Throwable $previous Previous exception, used for exception chaining.
31
     */
32 1
    public function __construct(Policy $policy, array $ruleExceptions, ?Throwable $previous = null)
33
    {
34 1
        $this->policy = $policy;
35 1
        $this->ruleExceptions = $ruleExceptions;
36
37 1
        $messages = [];
38 1
        foreach ($ruleExceptions as $ruleException) {
39 1
            $messages[] = $ruleException->getMessage();
40
        }
41
42 1
        parent::__construct(implode(' ', $messages), /*code*/0, $previous);
43 1
    }
44
45
    /**
46
     * @return Policy Policy that could not be enforced.
47
     */
48 1
    public function getPolicy(): Policy
49
    {
50 1
        return $this->policy;
51
    }
52
53
    /**
54
     * @return RuleException[] Exceptions thrown by rules in the policy that could not be enforced.
55
     */
56 1
    public function getRuleExceptions(): array
57
    {
58 1
        return $this->ruleExceptions;
59
    }
60
}
61