IfBlock   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 87
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getConditionCallable() 0 4 1
A getOnTrue() 0 4 1
A getOnFalse() 0 4 1
A getProperties() 0 4 1
A checkConditionCallableSignature() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Validation\Blocks;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Common\Reflection\CheckCallableTrait;
22
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
23
use Limoncello\Validation\Contracts\Blocks\IfExpressionInterface;
24
use Limoncello\Validation\Contracts\Execution\ContextInterface;
25
use function assert;
26
27
/**
28
 * @package Limoncello\Validation
29
 */
30
final class IfBlock implements IfExpressionInterface
31
{
32
    use CheckCallableTrait;
33
34
    /**
35
     * @var callable
36
     */
37
    private $condition;
38
39
    /**
40
     * @var ExecutionBlockInterface
41
     */
42
    private $onTrue;
43
44
    /**
45
     * @var ExecutionBlockInterface
46
     */
47
    private $onFalse;
48
49
    /**
50
     * @var array
51
     */
52
    private $properties;
53
54
    /**
55
     * @param callable                $condition
56
     * @param ExecutionBlockInterface $onTrue
57
     * @param ExecutionBlockInterface $onFalse
58
     * @param array                   $properties
59
     */
60 11
    public function __construct(
61
        callable $condition,
62
        ExecutionBlockInterface $onTrue,
63
        ExecutionBlockInterface $onFalse,
64
        array $properties = []
65
    ) {
66 11
        assert($this->checkConditionCallableSignature($condition));
67
68 11
        $this->condition  = $condition;
69 11
        $this->onTrue     = $onTrue;
70 11
        $this->onFalse    = $onFalse;
71 11
        $this->properties = $properties;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 10
    public function getConditionCallable(): callable
78
    {
79 10
        return $this->condition;
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 10
    public function getOnTrue(): ExecutionBlockInterface
86
    {
87 10
        return $this->onTrue;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 10
    public function getOnFalse(): ExecutionBlockInterface
94
    {
95 10
        return $this->onFalse;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 11
    public function getProperties(): array
102
    {
103 11
        return $this->properties;
104
    }
105
106
    /** @noinspection PhpDocMissingThrowsInspection
107
     * @param callable $procedureCallable
108
     *
109
     * @return bool
110
     */
111 11
    private function checkConditionCallableSignature(callable $procedureCallable): bool
112
    {
113
        /** @noinspection PhpUnhandledExceptionInspection */
114 11
        return static::checkPublicStaticCallable($procedureCallable, [null, ContextInterface::class], 'bool');
115
    }
116
}
117