Completed
Branch master (85eed3)
by
unknown
02:32
created

JsonApiException   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 111
ccs 30
cts 30
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
A addError() 0 4 1
A addErrors() 0 6 2
A addErrorsFromArray() 0 6 2
A getErrors() 0 4 1
A getHttpCode() 0 4 1
A throwException() 0 4 1
1
<?php namespace Neomerx\JsonApi\Exceptions;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
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 \Exception;
20
use \RuntimeException;
21
use \Neomerx\JsonApi\Document\Error;
22
use \Neomerx\JsonApi\I18n\Translator as T;
23
24
/**
25
 * @package Neomerx\JsonApi
26
 */
27
class JsonApiException extends RuntimeException
28
{
29
    /** Default HTTP code */
30
    const HTTP_CODE_BAD_REQUEST = 400;
31
32
    /** Default HTTP code */
33
    const HTTP_CODE_FORBIDDEN = 403;
34
35
    /** Default HTTP code */
36
    const HTTP_CODE_NOT_ACCEPTABLE = 406;
37
38
    /** Default HTTP code */
39
    const HTTP_CODE_CONFLICT = 409;
40
41
    /** Default HTTP code */
42
    const HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415;
43
44
    /** Default HTTP code */
45
    const DEFAULT_HTTP_CODE = self::HTTP_CODE_BAD_REQUEST;
46
47
    /**
48
     * @var ErrorCollection
49
     */
50
    private $errors;
51
52
    /**
53
     * @var int
54
     */
55
    private $httpCode;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param Error|Error[]|ErrorCollection $errors
61
     * @param int                           $httpCode
62
     * @param Exception|null                $previous
63
     */
64 19
    public function __construct($errors, $httpCode = self::DEFAULT_HTTP_CODE, Exception $previous = null)
65
    {
66 19
        parent::__construct(T::t('JSON API error'), 0, $previous);
67
68 19
        $this->errors = new ErrorCollection();
69
70 19
        if ($errors instanceof ErrorCollection) {
71 1
            $this->addErrors($errors);
72 19
        } elseif (is_array($errors) === true) {
73 17
            $this->addErrorsFromArray($errors);
74 17
        } else {
75
            // should be Error
76 1
            $this->addError($errors);
77
        }
78
79 19
        $this->httpCode = $httpCode;
80 19
    }
81
82
    /**
83
     * @param Error $error
84
     */
85 3
    public function addError(Error $error)
86
    {
87 3
        $this->errors[] = $error;
88 3
    }
89
90
    /**
91
     * @param ErrorCollection $errors
92
     *
93
     * @return void
94
     */
95 1
    public function addErrors(ErrorCollection $errors)
96
    {
97 1
        foreach ($errors as $error) {
98 1
            $this->addError($error);
99 1
        }
100 1
    }
101
102
    /**
103
     * @param Error[] $errors
104
     *
105
     * @return void
106
     */
107 17
    public function addErrorsFromArray(array $errors)
108
    {
109 17
        foreach ($errors as $error) {
110 1
            $this->addError($error);
111 17
        }
112 17
    }
113
114
    /**
115
     * @return ErrorCollection
116
     */
117 3
    public function getErrors()
118
    {
119 3
        return $this->errors;
120
    }
121
122
    /**
123
     * @return int
124
     */
125 11
    public function getHttpCode()
126
    {
127 11
        return $this->httpCode;
128
    }
129
130
    /**
131
     * @param JsonApiException $exception
132
     */
133 12
    public static function throwException(JsonApiException $exception)
134
    {
135 12
        throw $exception;
136
    }
137
}
138