Completed
Push — develop ( 50755f...053d8d )
by Neomerx
09:27 queued 03:23
created

ProcedureBlock::setExecuteCallable()   A

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 namespace Limoncello\Validation\Blocks;
2
3
/**
4
 * Copyright 2015-2018 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Common\Reflection\CheckCallableTrait;
20
use Limoncello\Validation\Contracts\Blocks\ProcedureBlockInterface;
21
use Limoncello\Validation\Contracts\Execution\ContextInterface;
22
23
/**
24
 * @package Limoncello\Validation
25
 */
26
final class ProcedureBlock implements ProcedureBlockInterface
27
{
28
    use CheckCallableTrait;
29
30
    /**
31
     * @var callable|null
32
     */
33
    private $startCallable;
34
35
    /**
36
     * @var callable|null
37
     */
38
    private $endCallable;
39
40
    /**
41
     * @var callable
42
     */
43
    private $executeCallable;
44
45
    /**
46
     * @var array
47
     */
48
    private $properties;
49
50
    /**
51
     * @param callable      $executeCallable
52
     * @param array         $properties
53
     * @param callable|null $startCallable
54
     * @param callable|null $endCallable
55
     */
56 38
    public function __construct(
57
        callable $executeCallable,
58
        array $properties = [],
59
        callable $startCallable = null,
60
        callable $endCallable = null
61
    ) {
62 38
        $this->setExecuteCallable($executeCallable)->setProperties($properties);
63
64 38
        if ($startCallable !== null) {
65 6
            $this->setStartCallable($startCallable);
66
        }
67
68 38
        if ($endCallable !== null) {
69 6
            $this->setEndCallable($endCallable);
70
        }
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 25
    public function getStartCallable(): ?callable
77
    {
78 25
        return $this->startCallable;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 25
    public function getExecuteCallable(): callable
85
    {
86 25
        return $this->executeCallable;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 25
    public function getEndCallable(): ?callable
93
    {
94 25
        return $this->endCallable;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 34
    public function getProperties(): array
101
    {
102 34
        return $this->properties;
103
    }
104
105
    /**
106
     * @param array $properties
107
     *
108
     * @return self
109
     */
110 38
    public function setProperties(array $properties): self
111
    {
112 38
        $this->properties = $properties;
113
114 38
        return $this;
115
    }
116
117
    /**
118
     * @param callable $startCallable
119
     *
120
     * @return self
121
     */
122 6
    public function setStartCallable(callable $startCallable): self
123
    {
124 6
        assert($this->checkProcedureStartOrEndCallableSignature($startCallable));
125
126 6
        $this->startCallable = $startCallable;
127
128 6
        return $this;
129
    }
130
131
    /**
132
     * @param callable $endCallable
133
     *
134
     * @return self
135
     */
136 8
    public function setEndCallable(callable $endCallable): self
137
    {
138 8
        assert($this->checkProcedureStartOrEndCallableSignature($endCallable));
139
140 8
        $this->endCallable = $endCallable;
141
142 8
        return $this;
143
    }
144
145
    /**
146
     * @param callable $executeCallable
147
     *
148
     * @return self
149
     */
150 38
    private function setExecuteCallable(callable $executeCallable): self
151
    {
152 38
        assert($this->checkProcedureExecuteCallableSignature($executeCallable));
153
154 38
        $this->executeCallable = $executeCallable;
155
156 38
        return $this;
157
    }
158
159
    /** @noinspection PhpDocMissingThrowsInspection
160
     * @param callable $procedureCallable
161
     *
162
     * @return bool
163
     */
164 38
    private function checkProcedureExecuteCallableSignature(callable $procedureCallable): bool
165
    {
166
        /** @noinspection PhpUnhandledExceptionInspection */
167 38
        return static::checkPublicStaticCallable($procedureCallable, [null, ContextInterface::class], 'array');
168
    }
169
170
    /** @noinspection PhpDocMissingThrowsInspection
171
     * @param callable $procedureCallable
172
     *
173
     * @return bool
174
     */
175 8
    private function checkProcedureStartOrEndCallableSignature(callable $procedureCallable): bool
176
    {
177
        /** @noinspection PhpUnhandledExceptionInspection */
178 8
        return static::checkPublicStaticCallable($procedureCallable, [ContextInterface::class], 'array');
179
    }
180
}
181