Completed
Push — master ( 22fd50...798002 )
by Neomerx
03:53
created

BlockReplies::isResultSuccessful()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 1
nop 1
1
<?php namespace Limoncello\Validation\Execution;
2
3
/**
4
 * Copyright 2015-2017 [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\Validation\Contracts\Execution\ContextInterface;
20
21
/**
22
 * @package Limoncello\Validation
23
 */
24
final class BlockReplies
25
{
26
    /**
27
     * Rule reply key.
28
     */
29
    const REPLY_SUCCESS_VALUE = 0;
30
31
    /**
32
     * Rule reply key.
33
     */
34
    const REPLY_ERRORS_INFO = self::REPLY_SUCCESS_VALUE + 1;
35
36
    /**
37
     * Error info key.
38
     */
39
    const ERROR_INFO_BLOCK_INDEX = 0;
40
41
    /**
42
     * Error info key.
43
     */
44
    const ERROR_INFO_VALUE = self::ERROR_INFO_BLOCK_INDEX + 1;
45
46
    /**
47
     * Error info key.
48
     */
49
    const ERROR_INFO_CODE = self::ERROR_INFO_VALUE + 1;
50
51
    /**
52
     * Error info key.
53
     */
54
    const ERROR_INFO_CONTEXT = self::ERROR_INFO_CODE + 1;
55
56
    /**
57
     * @param mixed $result
58
     *
59
     * @return array
60
     */
61
    public static function createSuccessReply($result): array
62
    {
63
        return [
64
            static::REPLY_SUCCESS_VALUE => $result,
65
            static::REPLY_ERRORS_INFO   => null,
66
        ];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public static function createStartSuccessReply(): array
73
    {
74
        return [];
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public static function createEndSuccessReply(): array
81
    {
82
        return [];
83
    }
84
85
    /**
86
     * @param ContextInterface $context
87
     * @param mixed            $errorValue
88
     * @param int              $errorCode
89
     * @param mixed|null       $errorContext
90
     *
91
     * @return array
92
     */
93
    public static function createErrorReply(
94
        ContextInterface $context,
95
        $errorValue,
96
        int $errorCode,
97
        $errorContext = null
98
    ): array {
99
        return [
100
            static::REPLY_SUCCESS_VALUE => null,
101
            static::REPLY_ERRORS_INFO   => [
102
                static::createErrorInfoEntry(
103
                    $context->getCurrentBlockId(),
104
                    $errorValue,
105
                    $errorCode,
106
                    $errorContext
107
                ),
108
            ],
109
        ];
110
    }
111
112
    /**
113
     * @param ContextInterface $context
114
     * @param int              $errorCode
115
     * @param mixed|null       $errorContext
116
     *
117
     * @return array
118
     */
119 View Code Duplication
    public static function createStartErrorReply(ContextInterface $context, int $errorCode, $errorContext = null): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $value = null;
122
123
        return [
124
            static::createErrorInfoEntry(
125
                $context->getCurrentBlockId(),
126
                $value,
127
                $errorCode,
128
                $errorContext
129
            ),
130
        ];
131
    }
132
133
    /**
134
     * @param ContextInterface $context
135
     * @param int              $errorCode
136
     * @param mixed|null       $errorContext
137
     *
138
     * @return array
139
     */
140 View Code Duplication
    public static function createEndErrorReply(ContextInterface $context, int $errorCode, $errorContext = null): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $value = null;
143
144
        return [
145
            static::createErrorInfoEntry(
146
                $context->getCurrentBlockId(),
147
                $value,
148
                $errorCode,
149
                $errorContext
150
            ),
151
        ];
152
    }
153
154
    /**
155
     * @param int        $blockId
156
     * @param mixed      $value
157
     * @param int        $code
158
     * @param null|mixed $context
159
     *
160
     * @return array
161
     */
162
    protected static function createErrorInfoEntry(int $blockId, $value, int $code, $context = null): array
163
    {
164
        return [
165
            static::ERROR_INFO_BLOCK_INDEX => $blockId,
166
            static::ERROR_INFO_VALUE       => $value,
167
            static::ERROR_INFO_CODE        => $code,
168
            static::ERROR_INFO_CONTEXT     => $context,
169
        ];
170
    }
171
172
    /**
173
     * @param array $result
174
     *
175
     * @return bool
176
     */
177
    public static function isResultSuccessful(array $result): bool
178
    {
179
        assert(
180
            count($result) === 2 &&
181
            ($result[static::REPLY_ERRORS_INFO] === null || is_array($result[static::REPLY_ERRORS_INFO]) === true)
182
        );
183
184
        // if error code is `null`
185
        $isOk = $result[static::REPLY_ERRORS_INFO] === null;
186
187
        return $isOk;
188
    }
189
190
    /**
191
     * @param array $result
192
     *
193
     * @return mixed
194
     */
195
    public static function extractResultOutput(array $result)
196
    {
197
        // extracting result only make sense when error is `null`.
198
        assert(static::isResultSuccessful($result) === true && $result[static::REPLY_ERRORS_INFO] === null);
199
200
        $value = $result[static::REPLY_SUCCESS_VALUE];
201
202
        return $value;
203
    }
204
205
    /**
206
     * @param array $result
207
     *
208
     * @return array
209
     */
210
    public static function extractResultErrorsInfo(array $result): array
211
    {
212
        assert(
213
            count($result) === 2 &&
214
            $result[static::REPLY_SUCCESS_VALUE] === null &&
215
            is_array($result[static::REPLY_ERRORS_INFO]) === true &&
216
            empty($result[static::REPLY_ERRORS_INFO]) === false
217
        );
218
219
        $value = $result[static::REPLY_ERRORS_INFO];
220
221
        return $value;
222
    }
223
}
224