Completed
Push — master ( 321399...55a1a5 )
by Pavel
01:36
created

RestException::unknownRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Exceptions;
2
3
use Symfony\Component\HttpFoundation\Response;
4
5
class RestException extends \Exception
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $errors = [];
11
12
    /**
13
     * @param \Exception $exception
14
     * @param bool       $debug
15
     *
16
     * @return $this
17
     */
18
    public static function createFromException(\Exception $exception, $debug = false)
19
    {
20
        $extra = $debug ? ['trace' => $exception->getTrace()] : [];
21
22
        return static::create(Response::HTTP_INTERNAL_SERVER_ERROR, 'Internal Server Error', $exception)
23
            ->error('internal-error', [], $exception->getMessage(), $extra);
24
    }
25
26
    /**
27
     * @param string $message
28
     *
29
     * @return RestException
30
     */
31
    public static function createForbidden($message = 'Forbidden.')
32
    {
33
        return static::create(Response::HTTP_FORBIDDEN, $message);
34
    }
35
36
    /**
37
     * @param int|array $id
38
     * @param string    $resourceKey
39
     * @param string    $message
40
     *
41
     * @return $this
42
     */
43 1
    public static function createNotFound($id, $resourceKey, $message = '')
44
    {
45 1
        return static::create(Response::HTTP_NOT_FOUND, 'Entity not found.')
46 1
            ->error('entity-not-found', ['type' => $resourceKey, 'id' => $id], $message);
47
    }
48
49
    /**
50
     * @param array $source
51
     * @param       $message
52
     *
53
     * @return $this
54
     */
55 1
    public static function createFilterError(array $source, $message)
56
    {
57 1
        return static::create(Response::HTTP_BAD_REQUEST, 'Wrong filter input.')
58 1
            ->error('filter-input', $source, $message);
59
    }
60
61
    /**
62
     * @return static
63
     */
64
    public static function missingRootData()
65
    {
66
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
67
            ->error('missing-root-data', [], "Missing `data` Member at document's top level.");
68
    }
69
70
    /**
71
     * @param string $pointer
72
     *
73
     * @return $this
74
     */
75
    public static function missingData($pointer)
76
    {
77
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
78
            ->error('missing-data', ['pointer' => $pointer], "Missing `data` member at pointer scope.");
79
    }
80
81
    /**
82
     * @param string $pointer
83
     *
84
     * @return $this
85
     */
86
    public static function missingAttributes($pointer)
87
    {
88
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
89
            ->error('missing-attributes', ['pointer' => $pointer], "Missing `data.attributes` at pointer level.");
90
    }
91
92
    /**
93
     * @param string $pointer
94
     *
95
     * @return $this
96
     */
97
    public static function unknownAttribute($pointer)
98
    {
99
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
100
            ->error('unknown-attribute', ['pointer' => $pointer], 'Unknown attribute, please see source.');
101
    }
102
103
    /**
104
     * @param string $pointer
105
     *
106
     * @return static
107
     */
108
    public static function unknownRelation($pointer)
109
    {
110
        return static::create(Response::HTTP_UNPROCESSABLE_ENTITY)
111
            ->error('unknown-relation', ['pointer' => $pointer], 'Unknown relation, please see source.');
112
    }
113
114
    /**
115
     * @param int             $httpStatus
116
     * @param string          $message
117
     * @param \Exception|null $previous
118
     *
119
     * @return static
120
     */
121 2
    static public function create(
122
        $httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR,
123
        $message = '',
124
        \Exception $previous = null
125
    ) {
126 2
        return new static($httpStatus, $message, $previous);
127
    }
128
129
    /**
130
     * RestException constructor.
131
     *
132
     * @param int             $httpStatus
133
     * @param string          $message
134
     * @param \Exception|null $previous
135
     */
136 3
    public function __construct(
137
        $httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR,
138
        $message = '',
139
        \Exception  $previous = null
140
    ) {
141 3
        parent::__construct($message, $httpStatus, $previous);
142 3
    }
143
144
    /**
145
     * @param $attribute
146
     * @param $detail
147
     *
148
     * @return $this
149
     */
150
    public function errorAttribute($attribute, $detail)
151
    {
152
        return $this->error('attribute-error', ['attribute' => $attribute], $detail);
153
    }
154
155
    /**
156
     * @param $relation
157
     * @param $detail
158
     *
159
     * @return RestException
160
     */
161
    public function errorRelation($relation, $detail)
162
    {
163
        return $this->error('relation-error', ['relation' => $relation], $detail);
164
    }
165
166
    /**
167
     * @param string $applicationCode
168
     * @param array  $source
169
     * @param string $detail
170
     * @param array  $extra
171
     *
172
     * @return $this
173
     */
174 2
    public function error($applicationCode, $source, $detail, $extra = [])
175
    {
176 2
        $this->errors[] = array_merge(['code' => $applicationCode, 'source' => $source, 'detail' => $detail] + $extra);
177
178 2
        return $this;
179
    }
180
181
    /**
182
     * @return array
183
     */
184 3
    public function errors()
185
    {
186 3
        return $this->errors;
187
    }
188
}
189