Completed
Push — master ( e881d8...a5189e )
by Pavel
02:13
created

RestException::notJsonApiResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Exceptions;
2
3
use Symfony\Component\HttpFoundation\Response;
4
use Symfony\Component\Validator\ConstraintViolation;
5
use Symfony\Component\Validator\ConstraintViolationInterface;
6
use Symfony\Component\Validator\ConstraintViolationListInterface;
7
8
class RestException extends \Exception
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $errors = [];
14
15
    /**
16
     * @param \Exception $exception
17
     * @param bool       $debug
18
     *
19
     * @return $this
20
     */
21 1
    public static function createFromException(\Exception $exception, $debug = false)
22
    {
23 1
        $extra = $debug ? ['trace' => $exception->getTrace()] : [];
24
25 1
        return static::create(Response::HTTP_INTERNAL_SERVER_ERROR, 'Internal Server Error', $exception)
26 1
            ->error('internal-error', [], $exception->getMessage(), $extra);
27
    }
28
29
    /**
30
     * @param ConstraintViolationListInterface $errors
31
     *
32
     * @return RestException
33
     */
34 1
    public static function createFromConstraintViolationList(ConstraintViolationListInterface $errors)
35
    {
36 1
        $exception = static::createUnprocessable('Input validation errors.');
37
38
        /** @var ConstraintViolationInterface $error */
39 1
        foreach ($errors as $error) {
40 1
            $exception->errorValidation($error->getPropertyPath(), $error->getMessage());
41
        }
42
43 1
        return $exception;
44
    }
45
46
    /**
47
     * @param string $message
48
     *
49
     * @return RestException
50
     */
51 1
    public static function createForbidden($message = 'Forbidden.')
52
    {
53 1
        return static::create(Response::HTTP_FORBIDDEN, $message);
54
    }
55
56
    /**
57
     * @param string $message
58
     *
59
     * @return RestException
60
     */
61 2
    public static function createUnprocessable($message = '')
62
    {
63 2
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY, $message);
64
    }
65
66
    /**
67
     * @param int|array $id
68
     * @param string    $resourceKey
69
     * @param string    $message
70
     *
71
     * @return $this
72
     */
73 1
    public static function createNotFound($id, $resourceKey, $message = '')
74
    {
75 1
        return static::create(Response::HTTP_NOT_FOUND, 'Entity not found.')
76 1
            ->error('entity-not-found', ['type' => $resourceKey, 'id' => $id], $message);
77
    }
78
79
    /**
80
     * @param array $source
81
     * @param       $message
82
     *
83
     * @return $this
84
     */
85 1
    public static function createFilterError(array $source, $message)
86
    {
87 1
        return static::create(Response::HTTP_BAD_REQUEST, 'Wrong filter input.')
88 1
            ->error('filter-input', $source, $message);
89
    }
90
91
    /**
92
     * @return static
93
     */
94 1
    public static function missingRootData()
95
    {
96 1
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
97 1
            ->error('missing-root-data', ['pointer' => ''], "Missing `data` member at document top level.");
98
    }
99
100
    /**
101
     * @param string $pointer
102
     *
103
     * @return $this
104
     */
105 1
    public static function missingData($pointer)
106
    {
107 1
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
108 1
            ->error('missing-data', ['pointer' => $pointer], "Missing `data` member at pointer level.");
109
    }
110
111
    /**
112
     * @param string $pointer
113
     *
114
     * @return $this
115
     */
116 1
    public static function missingDataMembers($pointer)
117
    {
118 1
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
119 1
            ->error('missing-data-members', ['pointer' => $pointer],
120 1
                'Missing or not array `data.attributes` or `data.relationships` at pointer level.'
121
            );
122
    }
123
124
    /**
125
     * @param string $pointer
126
     *
127
     * @return $this
128
     */
129 1
    public static function unknownAttribute($pointer)
130
    {
131 1
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
132 1
            ->error('unknown-attribute', ['pointer' => $pointer], 'Unknown attribute.');
133
    }
134
135
    /**
136
     * @param string $pointer
137
     *
138
     * @return static
139
     */
140 1
    public static function unknownRelation($pointer)
141
    {
142 1
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
143 1
            ->error('unknown-relation', ['pointer' => $pointer], 'Unknown relation.');
144
    }
145
146
    /**
147
     * @return static
148
     */
149 13
    public static function invalidInclude()
150
    {
151 13
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
152 13
            ->error('invalid-include', ['pointer' => 'include'], 'Invalid `include` param.');
153
    }
154
155
    /**
156
     * @param mixed $entity
157
     *
158
     * @return $this
159
     */
160 1
    public static function notJsonApiResource($entity)
161
    {
162 1
        $source = [];
163 1
        $message = 'Got not JsonApiResource entity.';
164
165 1
        if (is_object($entity)) {
166 1
            $source['class'] = get_class($entity);
167
        }
168
169 1
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY, $message)
170 1
            ->error('invalid-entity', $source, $message);
171
    }
172
173
    /**
174
     * @param int             $httpStatus
175
     * @param string          $message
176
     * @param \Exception|null $previous
177
     *
178
     * @return static
179
     */
180 19
    static public function create(
181
        $httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR,
182
        $message = '',
183
        \Exception $previous = null
184
    ) {
185 19
        return new static($httpStatus, $message, $previous);
186
    }
187
188
    /**
189
     * RestException constructor.
190
     *
191
     * @param int             $httpStatus
192
     * @param string          $message
193
     * @param \Exception|null $previous
194
     */
195 19
    public function __construct(
196
        $httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR,
197
        $message = '',
198
        \Exception  $previous = null
199
    ) {
200 19
        parent::__construct($message, $httpStatus, $previous);
201 19
    }
202
203
    /**
204
     * @param string $pointer
205
     * @param string $detail
206
     * @param array  $extra
207
     *
208
     * @return RestException
209
     */
210 1
    public function errorValidation($pointer, $detail, array $extra = [])
211
    {
212 1
        return $this->error('validation', ['pointer' => $pointer], $detail, $extra);
213
    }
214
215
    /**
216
     * @param string $applicationCode
217
     * @param array  $source
218
     * @param string $detail
219
     * @param array  $extra
220
     *
221
     * @return $this
222
     */
223 18
    public function error($applicationCode, $source, $detail, array $extra = [])
224
    {
225 18
        $this->errors[] = array_merge(['code' => $applicationCode, 'source' => $source, 'detail' => $detail] + $extra);
226
227 18
        return $this;
228
    }
229
230
    /**
231
     * @return array
232
     */
233 5
    public function errors()
234
    {
235 5
        return $this->errors;
236
    }
237
}
238