Passed
Push — master ( 635200...ad05d4 )
by Vince
03:32 queued 01:55
created

errorException::throwError()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 3
b 0
f 0
nc 4
nop 0
dl 0
loc 21
rs 9.7998
1
<?php
2
/**
3
 * ==================================
4
 * Responsible PHP API
5
 * ==================================
6
 *
7
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
8
 *
9
 * @api Responible API
10
 * @package responsible\core\exception
11
 *
12
 * @author Vince scarpa <[email protected]>
13
 *
14
 */
15
namespace responsible\core\exception;
16
17
use responsible\core\headers;
18
19
class errorException extends responsibleException
20
{
21
    /**
22
     * Responsible API options
23
     */
24
    private static $options;
25
26
    /**
27
     * [__construct Use parent constructor]
28
     */
29
    public function __construct()
30
    {}
31
32
    /**
33
     * [$ERRORS Error sets Default error messages for supported error codes]
34
     * @var array
35
     */
36
    private $ERRORS = array(
37
38
        'APPLICATION_ERROR' => array(
39
            'ERROR_CODE' => 404,
40
            'ERROR_STATUS' => 'APPLICATION_ERROR',
41
            'MESSAGE' => '',
42
        ),
43
44
        'NOT_EXTENDED' => array(
45
            'ERROR_CODE' => 510,
46
            'ERROR_STATUS' => 'API_ERROR',
47
            'MESSAGE' => '',
48
        ),
49
50
        'NO_CONTENT' => array(
51
            'ERROR_CODE' => 200,
52
            'ERROR_STATUS' => 'NO_CONTENT',
53
            'MESSAGE' => [
54
                'error' => 'empty',
55
                'description' => 'No results'
56
            ],
57
        ),
58
59
        'NOT_FOUND' => array(
60
            'ERROR_CODE' => 404,
61
            'ERROR_STATUS' => 'NOT_FOUND',
62
            'MESSAGE' => [
63
                'error' => 'not found',
64
                'description' => 'We could not find the resource you requested or the request was not found'
65
            ],
66
        ),
67
68
        'METHOD_NOT_ALLOWED' => array(
69
            'ERROR_CODE' => 405,
70
            'ERROR_STATUS' => 'METHOD_NOT_ALLOWED',
71
            'MESSAGE' => [
72
                'error' => 'not allowed',
73
                'description' => 'The method request provided is not allowed'
74
            ],
75
        ),
76
77
        'UNAUTHORIZED' => array(
78
            'ERROR_CODE' => 401,
79
            'ERROR_STATUS' => 'UNAUTHORIZED',
80
            'MESSAGE' => [
81
                'error' => 'denied',
82
                'description' => 'Permission denied'
83
            ],
84
        ),
85
86
        'BAD_REQUEST' => array(
87
            'ERROR_CODE' => 400,
88
            'ERROR_STATUS' => 'BAD_REQUEST',
89
            'MESSAGE' => [
90
                'error' => 'bad request',
91
                'description' => 'The request provided was Invalid'
92
            ],
93
        ),
94
95
        'TOO_MANY_REQUESTS' => array(
96
            'ERROR_CODE' => 429,
97
            'ERROR_STATUS' => 'TOO_MANY_REQUESTS',
98
            'MESSAGE' => [
99
                'error' => 'too many requests',
100
                'description' => 'Too Many Requests'
101
            ],
102
        ),
103
    );
104
105
    /**
106
     * [$ERROR_STATE Current error state]
107
     * @var array
108
     */
109
    private $ERROR_STATE = array();
110
111
    /**
112
     * [error]
113
     * @return void
114
     */
115
    public function error($type)
116
    {
117
        if (!isset($this->ERRORS[$type])) {
118
            if (isset($this->ERRORS['MESSAGE'])) {
119
                $this->ERROR_STATE = array(
120
                    'ERROR_CODE' => 500,
121
                    'ERROR_STATUS' => $type,
122
                    'MESSAGE' => $this->ERRORS['MESSAGE'],
123
                );
124
                $this->throwError();
125
            }
126
127
            $this->ERROR_STATE = $this->ERRORS['APPLICATION_ERROR'];
128
            $this->ERROR_STATE['MESSAGE'] = "`" . $type . "`" . ' is not defined as an error code';
129
            $this->throwError();
130
        }
131
132
        $this->ERROR_STATE = $this->ERRORS[$type];
133
        $this->throwError();
134
    // @codeCoverageIgnoreStart
135
    }
136
    // @codeCoverageIgnoreEnd
137
138
    /**
139
     * [message Custom message override]
140
     * @return self
141
     */
142
    public function message($message)
143
    {
144
        $this->ERRORS['MESSAGE'] = $message;
145
        return $this;
146
    }
147
148
    /**
149
     * [errorMessage]
150
     */
151
    private function throwError()
152
    {
153
        $options = $this->getOptions();
0 ignored issues
show
Unused Code introduced by
The assignment to $options is dead and can be removed.
Loading history...
154
155
        $headers = new headers\header;
156
        $headers->setOptions($this->getOptions());
157
        $headers->setHeaders();
158
159
        http_response_code($this->ERROR_STATE['ERROR_CODE']);
160
161
        $message = isset($this->ERRORS['MESSAGE']) && !empty($this->ERRORS['MESSAGE'])
162
        ? $this->ERRORS['MESSAGE']
163
        : $this->ERROR_STATE['MESSAGE'];
164
165
        $eMessage = json_encode(array(
166
            'ERROR_CODE' => $this->ERROR_STATE['ERROR_CODE'],
167
            'ERROR_STATUS' => $this->ERROR_STATE['ERROR_STATUS'],
168
            'MESSAGE' => $message,
169
        ), JSON_PRETTY_PRINT);
170
171
        throw new responsibleException($eMessage, $this->ERROR_STATE['ERROR_CODE']);
172
    }
173
174
    /**
175
     * [setOptions Inherit Responsible API options]
176
     * @param array $options
177
     * @return self
178
     */
179
    public function setOptions($options)
180
    {
181
        self::$options = $options;
182
        return $this;
183
    }
184
185
    /**
186
     * [getOptions Get available options]
187
     * @return array
188
     */
189
    public function getOptions()
190
    {
191
        return self::$options;
192
    }
193
}
194