Passed
Push — dev ( 7b9d4f...88bc17 )
by Marcin
05:50 queued 03:44
created

BaseApiCodes   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 30
c 6
b 0
f 0
dl 0
loc 190
ccs 26
cts 26
cp 1
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A EX_UNCAUGHT_EXCEPTION() 0 3 1
A NO_ERROR_MESSAGE() 0 3 1
A EX_HTTP_EXCEPTION() 0 3 1
A OK() 0 3 1
A EX_AUTHENTICATION_EXCEPTION() 0 3 1
A EX_HTTP_SERVICE_UNAVAILABLE() 0 3 1
A EX_HTTP_NOT_FOUND() 0 3 1
A getBaseMap() 0 21 1
A EX_VALIDATION_EXCEPTION() 0 3 1
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-2019 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 108
    protected static function getBaseMap(): array
97
    {
98 108
        $tpl = 'response-builder::builder.http_%d';
99
100
        return [
101
            /** @scrutinizer ignore-deprecated */
102 108
            self::OK()                          => 'response-builder::builder.ok',
103
            /** @scrutinizer ignore-deprecated */
104 108
            self::NO_ERROR_MESSAGE()            => 'response-builder::builder.no_error_message',
105
            /** @scrutinizer ignore-deprecated */
106 108
            self::EX_HTTP_EXCEPTION()           => 'response-builder::builder.http_exception',
107
            /** @scrutinizer ignore-deprecated */
108 108
            self::EX_UNCAUGHT_EXCEPTION()       => 'response-builder::builder.uncaught_exception',
109
            /** @scrutinizer ignore-deprecated */
110 108
            self::EX_HTTP_NOT_FOUND()           => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND),
111
            /** @scrutinizer ignore-deprecated */
112 108
            self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE),
113
            /** @scrutinizer ignore-deprecated */
114 108
            self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED),
115
            /** @scrutinizer ignore-deprecated */
116 108
            self::EX_VALIDATION_EXCEPTION()     => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY),
117
        ];
118
    }
119
120
    /**
121
     * Returns API code for internal code OK
122
     *
123
     * @return int valid API code in current range
124
     *
125
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
126
     */
127 108
    public static function OK(): int
128
    {
129 108
        return static::getCodeForInternalOffset(static::OK_OFFSET);
130
    }
131
132
    /**
133
     * Returns API code for internal code NO_ERROR_MESSAGE
134
     *
135
     * @return int valid API code in current range
136
     */
137 108
    public static function NO_ERROR_MESSAGE(): int
138
    {
139 108
        return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET);
140
    }
141
142
    /**
143
     * Returns API code for internal code EX_HTTP_NOT_FOUND
144
     *
145
     * @return int valid API code in current range
146
     *
147
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
148
     */
149 108
    public static function EX_HTTP_NOT_FOUND(): int
150
    {
151 108
        return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET);
152
    }
153
154
    /**
155
     * Returns API code for internal code EX_HTTP_EXCEPTION
156
     *
157
     * @return int valid API code in current range
158
     *
159
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
160
     */
161 108
    public static function EX_HTTP_EXCEPTION(): int
162
    {
163 108
        return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET);
164
    }
165
166
    /**
167
     * Returns API code for internal code EX_UNCAUGHT_EXCEPTION
168
     *
169
     * @return int valid API code in current range
170
     *
171
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
172
     */
173 108
    public static function EX_UNCAUGHT_EXCEPTION(): int
174
    {
175 108
        return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET);
176
    }
177
178
    /**
179
     * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION
180
     *
181
     * @return int valid API code in current range
182
     *
183
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
184
     */
185 108
    public static function EX_AUTHENTICATION_EXCEPTION(): int
186
    {
187 108
        return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET);
188
    }
189
190
    /**
191
     * Returns API code for internal code EX_VALIDATION_EXCEPTION
192
     *
193
     * @return int valid API code in current range
194
     *
195
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
196
     */
197 108
    public static function EX_VALIDATION_EXCEPTION(): int
198
    {
199 108
        return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET);
200
    }
201
202
    /**
203
     * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE
204
     *
205
     * @return int valid API code in current range
206
     *
207
     * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
208
     */
209 108
    public static function EX_HTTP_SERVICE_UNAVAILABLE(): int
210
    {
211 108
        return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET);
212
    }
213
214
}
215