Completed
Push — master ( bc1f52...d1acb5 )
by Marcin
20s queued 18s
created

BaseApiCodes::EX_AUTHENTICATION_EXCEPTION()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2020 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
use Symfony\Component\HttpFoundation\Response as HttpResponse;
18
19
/**
20
 * BaseApiCodes handling class
21
 */
22
class BaseApiCodes
23
{
24
    use ApiCodesHelpers;
25
26
    /**
27
     * protected code range - lowest code for reserved range.
28
     *
29
     * @var int
30
     */
31
    public const RESERVED_MIN_API_CODE_OFFSET = 0;
32
33
    /**
34
     * protected code range - highest code for reserved range
35
     *
36
     * @var int
37
     */
38
    public const RESERVED_MAX_API_CODE_OFFSET = 19;
39
40
    /**
41
     * built-in codes: OK
42
     *
43
     * @var int
44
     */
45
    protected const OK_OFFSET = 0;
46
    /**
47
     * built-in code for fallback message mapping
48
     *
49
     * @var int
50
     */
51
    protected const NO_ERROR_MESSAGE_OFFSET = 1;
52
    /**
53
     * built-in error code for HTTP_NOT_FOUND exception
54
     *
55
     * @var int
56
     */
57
    protected const EX_HTTP_NOT_FOUND_OFFSET = 10;
58
    /**
59
     * built-in error code for HTTP_SERVICE_UNAVAILABLE exception
60
     *
61
     * @var int
62
     */
63
    protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11;
64
    /**
65
     * built-in error code for HTTP_EXCEPTION
66
     *
67
     * @var int
68
     */
69
    protected const EX_HTTP_EXCEPTION_OFFSET = 12;
70
    /**
71
     * built-in error code for UNCAUGHT_EXCEPTION
72
     *
73
     * @var int
74
     */
75
    protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13;
76
77
    /**
78
     * built-in error code for \Illuminate\Auth\AuthenticationException
79
     *
80
     * @var int
81
     */
82
    protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14;
83
84
    /**
85
     * built-in error code for \Illuminate\Auth\AuthenticationException
86
     *
87
     * @var int
88
     */
89
    protected const EX_VALIDATION_EXCEPTION_OFFSET = 15;
90
91
    /**
92
     * Returns base code mapping array
93
     *
94
     * @return array
95
     */
96 87
    protected static function getBaseMap(): array
97
    {
98 87
        $tpl = 'response-builder::builder.http_%d';
99
100
        return [
101 87
            self::OK()                          => 'response-builder::builder.ok',
102 87
            self::NO_ERROR_MESSAGE()            => 'response-builder::builder.no_error_message',
103 87
            self::EX_HTTP_EXCEPTION()           => 'response-builder::builder.http_exception',
104 87
            self::EX_UNCAUGHT_EXCEPTION()       => 'response-builder::builder.uncaught_exception',
105 87
            self::EX_HTTP_NOT_FOUND()           => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND),
106 87
            self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE),
107 87
            self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED),
108 87
            self::EX_VALIDATION_EXCEPTION()     => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY),
109
        ];
110
    }
111
112
    /**
113
     * Returns API code for internal code OK
114
     *
115
     * @return int valid API code in current range
116
     */
117 87
    public static function OK(): int
118
    {
119 87
        return static::getCodeForInternalOffset(static::OK_OFFSET);
120
    }
121
122
    /**
123
     * Returns API code for internal code NO_ERROR_MESSAGE
124
     *
125
     * @return int valid API code in current range
126
     */
127 87
    public static function NO_ERROR_MESSAGE(): int
128
    {
129 87
        return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET);
130
    }
131
132
    /**
133
     * Returns API code for internal code EX_HTTP_NOT_FOUND
134
     *
135
     * @return int valid API code in current range
136
     */
137 87
    public static function EX_HTTP_NOT_FOUND(): int
138
    {
139 87
        return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET);
140
    }
141
142
    /**
143
     * Returns API code for internal code EX_HTTP_EXCEPTION
144
     *
145
     * @return int valid API code in current range
146
     */
147 87
    public static function EX_HTTP_EXCEPTION(): int
148
    {
149 87
        return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET);
150
    }
151
152
    /**
153
     * Returns API code for internal code EX_UNCAUGHT_EXCEPTION
154
     *
155
     * @return int valid API code in current range
156
     */
157 87
    public static function EX_UNCAUGHT_EXCEPTION(): int
158
    {
159 87
        return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET);
160
    }
161
162
    /**
163
     * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION
164
     *
165
     * @return int valid API code in current range
166
     */
167 87
    public static function EX_AUTHENTICATION_EXCEPTION(): int
168
    {
169 87
        return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET);
170
    }
171
172
    /**
173
     * Returns API code for internal code EX_VALIDATION_EXCEPTION
174
     *
175
     * @return int valid API code in current range
176
     */
177 87
    public static function EX_VALIDATION_EXCEPTION(): int
178
    {
179 87
        return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET);
180
    }
181
182
    /**
183
     * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE
184
     *
185
     * @return int valid API code in current range
186
     */
187 87
    public static function EX_HTTP_SERVICE_UNAVAILABLE(): int
188
    {
189 87
        return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET);
190
    }
191
192
}
193