ProcedureBlock::setStartCallable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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\ProcedureBlockInterface;
23
use Limoncello\Validation\Contracts\Execution\ContextInterface;
24
use function assert;
25
26
/**
27
 * @package Limoncello\Validation
28
 */
29
final class ProcedureBlock implements ProcedureBlockInterface
30
{
31
    use CheckCallableTrait;
32
33
    /**
34
     * @var callable|null
35
     */
36
    private $startCallable;
37
38
    /**
39
     * @var callable|null
40
     */
41
    private $endCallable;
42
43
    /**
44
     * @var callable
45
     */
46
    private $executeCallable;
47
48
    /**
49
     * @var array
50
     */
51
    private $properties;
52
53
    /**
54
     * @param callable      $executeCallable
55
     * @param array         $properties
56
     * @param callable|null $startCallable
57
     * @param callable|null $endCallable
58
     */
59 38
    public function __construct(
60
        callable $executeCallable,
61
        array $properties = [],
62
        callable $startCallable = null,
63
        callable $endCallable = null
64
    ) {
65 38
        $this->setExecuteCallable($executeCallable)->setProperties($properties);
66
67 38
        if ($startCallable !== null) {
68 6
            $this->setStartCallable($startCallable);
69
        }
70
71 38
        if ($endCallable !== null) {
72 6
            $this->setEndCallable($endCallable);
73
        }
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 25
    public function getStartCallable(): ?callable
80
    {
81 25
        return $this->startCallable;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 25
    public function getExecuteCallable(): callable
88
    {
89 25
        return $this->executeCallable;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 25
    public function getEndCallable(): ?callable
96
    {
97 25
        return $this->endCallable;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 34
    public function getProperties(): array
104
    {
105 34
        return $this->properties;
106
    }
107
108
    /**
109
     * @param array $properties
110
     *
111
     * @return self
112
     */
113 38
    public function setProperties(array $properties): self
114
    {
115 38
        $this->properties = $properties;
116
117 38
        return $this;
118
    }
119
120
    /**
121
     * @param callable $startCallable
122
     *
123
     * @return self
124
     */
125 6
    public function setStartCallable(callable $startCallable): self
126
    {
127 6
        assert($this->checkProcedureStartOrEndCallableSignature($startCallable));
128
129 6
        $this->startCallable = $startCallable;
130
131 6
        return $this;
132
    }
133
134
    /**
135
     * @param callable $endCallable
136
     *
137
     * @return self
138
     */
139 8
    public function setEndCallable(callable $endCallable): self
140
    {
141 8
        assert($this->checkProcedureStartOrEndCallableSignature($endCallable));
142
143 8
        $this->endCallable = $endCallable;
144
145 8
        return $this;
146
    }
147
148
    /**
149
     * @param callable $executeCallable
150
     *
151
     * @return self
152
     */
153 38
    private function setExecuteCallable(callable $executeCallable): self
154
    {
155 38
        assert($this->checkProcedureExecuteCallableSignature($executeCallable));
156
157 38
        $this->executeCallable = $executeCallable;
158
159 38
        return $this;
160
    }
161
162
    /** @noinspection PhpDocMissingThrowsInspection
163
     * @param callable $procedureCallable
164
     *
165
     * @return bool
166
     */
167 38
    private function checkProcedureExecuteCallableSignature(callable $procedureCallable): bool
168
    {
169
        /** @noinspection PhpUnhandledExceptionInspection */
170 38
        return static::checkPublicStaticCallable($procedureCallable, [null, ContextInterface::class], 'array');
171
    }
172
173
    /** @noinspection PhpDocMissingThrowsInspection
174
     * @param callable $procedureCallable
175
     *
176
     * @return bool
177
     */
178 8
    private function checkProcedureStartOrEndCallableSignature(callable $procedureCallable): bool
179
    {
180
        /** @noinspection PhpUnhandledExceptionInspection */
181 8
        return static::checkPublicStaticCallable($procedureCallable, [ContextInterface::class], 'array');
182
    }
183
}
184