RestException::modelNotFound()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Josh\Exceptions;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Anetwork\Respond\Facades\Respond;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
trait RestException
13
{
14
    /**
15
     * Datas of response
16
     *
17
     * @var array
18
     */
19
    protected $datas = [];
20
21
    /**
22
     * In development mode return trace of exception
23
     *
24
     * @var array
25
     */
26
    protected $trace = [];
27
28
    /**
29
     * Creates a new JSON response based on exception type.
30
     *
31
     * @author Alireza Josheghani <[email protected]>
32
     * @since  11 Nov 2016
33
     * @param  Exception $exception
34
     * @return \Anetwork\Respond\Facades\Respond
35
     */
36
    protected function getJsonResponseForException(Exception $exception)
37
    {
38
        $message = $exception->getMessage();
39
40
        $this->getTrace($exception);
41
42
        if ($exception instanceof ModelNotFoundException) {
43
            return $this->modelNotFound($message);
44
        } elseif ($exception instanceof NotFoundHttpException) {
45
            return $this->httpNotFound();
46
        } elseif ($exception instanceof BadRequestHttpException) {
47
            return $this->badRequest($message);
48
        }
49
50
        return $this->errorException($message);
51
    }
52
53
    /**
54
     * Returns json response for generic bad request.
55
     *
56
     * @author Alireza Josheghani <[email protected]>
57
     * @since  11 Nov 2016
58
     * @param  string $message
59
     * @return \Anetwork\Respond\Facades\Respond
60
     */
61
    protected function badRequest($message)
62
    {
63
        $this->datas['message'] = $message;
64
65
        return Respond::setStatusCode(400)
66
            ->setStatusText('bad request !')
67
            ->respondWithResult($this->datas);
68
    }
69
70
    /**
71
     * Returns json response ErrorException
72
     *
73
     * @author Alireza Josheghani <[email protected]>
74
     * @since  11 Nov 2016
75
     * @param  string $message
76
     * @return \Anetwork\Respond\Facades\Respond
77
     */
78
    protected function errorException($message)
79
    {
80
        $this->datas['message'] = $message;
81
82
        return Respond::setStatusCode(400)
83
            ->setStatusText('ErrorException !')
84
            ->respondWithMessage($this->datas);
85
    }
86
87
    /**
88
     * Returns json response for Eloquent model not found exception.
89
     *
90
     * @author Alireza Josheghani <[email protected]>
91
     * @since  11 Nov 2016
92
     * @param  string $message
93
     * @return \Anetwork\Respond\Facades\Respond
94
     */
95
    protected function modelNotFound($message)
96
    {
97
        $this->datas['message'] = $message;
98
99
        return Respond::setStatusCode(404)
100
            ->setStatusText('model notfound !')
101
            ->respondWithResult($this->datas);
102
    }
103
104
    /**
105
     * Returns json response for Eloquent model not found exception.
106
     *
107
     * @author Alireza Josheghani <[email protected]>
108
     * @since  11 Nov 2016
109
     * @return \Anetwork\Respond\Facades\Respond
110
     */
111
    protected function httpNotFound()
112
    {
113
        $this->datas['message'] = 'Sorry, the page you are looking for could not be found.';
114
115
        return Respond::setStatusCode(404)
116
            ->setStatusText('NotFoundHttpException')
117
            ->respondWithResult($this->datas);
118
    }
119
120
    /**
121
     * Get Trace in development mode
122
     *
123
     * @author Alireza Josheghani <[email protected]>
124
     * @since  11 Nov 2016
125
     * @param  Exception $exception
126
     * @return void
127
     */
128
    private function getTrace(Exception $exception)
129
    {
130
131
        if (config('app.debug')) {
132
133
            $traces = $exception->getTrace();
134
135
            if (!empty($traces)) {
136
                foreach ($traces as $trace) {
137
                    unset($trace['args']);
138
                    $this->trace[] = $trace;
139
                }
140
141
                $this->datas['trace'] = $this->trace;
142
            }
143
        }
144
    }
145
146
    /**
147
     * Render json exception
148
     *
149
     * @author Alireza Josheghani <[email protected]>
150
     * @since  11 Nov 2016
151
     * @param  Request   $request
152
     * @param  Exception $exception
153
     * @return \Anetwork\Respond\Facades\Respond
154
     */
155
    public function renderRestException(Request $request, Exception $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        return $this->getJsonResponseForException($exception);
158
    }
159
160
}
161