Completed
Push — master ( 1b0f3f...d1195d )
by Pavel
01:37
created

RestException::createForbidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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 int             $httpStatus
72
     * @param string          $message
73
     * @param \Exception|null $previous
74
     *
75
     * @return static
76
     */
77 2
    static public function create(
78
        $httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR,
79
        $message = '',
80
        \Exception $previous = null
81
    ) {
82 2
        return new static($httpStatus, $message, $previous);
83
    }
84
85
    /**
86
     * RestException constructor.
87
     *
88
     * @param int             $httpStatus
89
     * @param string          $message
90
     * @param \Exception|null $previous
91
     */
92 3
    public function __construct(
93
        $httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR,
94
        $message = '',
95
        \Exception  $previous = null
96
    ) {
97 3
        parent::__construct($message, $httpStatus, $previous);
98 3
    }
99
100
    /**
101
     * @param $attribute
102
     * @param $detail
103
     *
104
     * @return $this
105
     */
106
    public function errorAttribute($attribute, $detail)
107
    {
108
        return $this->error('attribute-error', ['attribute' => $attribute], $detail);
109
    }
110
111
    /**
112
     * @param $relation
113
     * @param $detail
114
     *
115
     * @return RestException
116
     */
117
    public function errorRelation($relation, $detail)
118
    {
119
        return $this->error('relation-error', ['relation' => $relation], $detail);
120
    }
121
122
    /**
123
     * @param string $applicationCode
124
     * @param array  $source
125
     * @param string $detail
126
     * @param array  $extra
127
     *
128
     * @return $this
129
     */
130 2
    public function error($applicationCode, $source, $detail, $extra = [])
131
    {
132 2
        $this->errors[] = array_merge(['code' => $applicationCode, 'source' => $source, 'detail' => $detail] + $extra);
133
134 2
        return $this;
135
    }
136
137
    /**
138
     * @return array
139
     */
140 3
    public function errors()
141
    {
142 3
        return $this->errors;
143
    }
144
}
145