Completed
Push — master ( 5807a3...09e832 )
by Josh
07:37
created

ErrorResponsesTrait::errorInternalError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace NavJobs\Transmit\Traits;
4
5
trait ErrorResponsesTrait
6
{
7
    /**
8
     * Returns a response that indicates an an error occurred.
9
     *
10
     * @param $message
11
     * @param int $statusCode
12
     * @return \Illuminate\Http\JsonResponse
13
     */
14
    private function respondWithError($message, $statusCode = 400)
15
    {
16
        return response()->json([
17
            'errors' => [
18
                'http_code' => $statusCode,
19
                'message'   => $message,
20
            ]
21
        ], $statusCode);
22
    }
23
24
    /**
25
     * Returns a response that indicates a 403 Forbidden.
26
     *
27
     * @param string $message
28
     * @return \Illuminate\Http\JsonResponse
29
     */
30
    protected function errorForbidden($message = 'Forbidden')
31
    {
32
        return $this->respondWithError($message, 403);
33
    }
34
35
    /**
36
     * Returns a response that indicates an Internal Error has occurred.
37
     *
38
     * @param string $message
39
     * @return \Illuminate\Http\JsonResponse
40
     */
41
    protected function errorInternalError($message = 'Internal Error')
42
    {
43
        return $this->respondWithError($message, 500);
44
    }
45
46
    /**
47
     * Returns a response that indicates a 404 Not Found.
48
     *
49
     * @param string $message
50
     * @return \Illuminate\Http\JsonResponse
51
     */
52
    protected function errorNotFound($message = 'Resource Not Found')
53
    {
54
        return $this->respondWithError($message, 404);
55
    }
56
57
    /**
58
     * Returns a response that indicates a 401 Unauthorized.
59
     *
60
     * @param string $message
61
     * @return \Illuminate\Http\JsonResponse
62
     */
63
    protected function errorUnauthorized($message = 'Unauthorized')
64
    {
65
        return $this->respondWithError($message, 401);
66
    }
67
68
    /**
69
     * Returns a response that indicates a 422 Unprocessable Entity.
70
     *
71
     * @param string $message
72
     * @return \Illuminate\Http\JsonResponse
73
     */
74
    protected function errorUnprocessableEntity($message = 'Unprocessable Entity')
75
    {
76
        return $this->respondWithError($message, 422);
77
    }
78
79
    /**
80
     * Returns a response that indicates the wrong arguments were specified.
81
     *
82
     * @param string $message
83
     * @return \Illuminate\Http\JsonResponse
84
     */
85
    protected function errorWrongArgs($message = 'Wrong Arguments')
86
    {
87
        return $this->respondWithError($message, 400);
88
    }
89
90
    /**
91
     * Returns a response that indicates custom error type.
92
     *
93
     * @param $message
94
     * @param $statusCode
95
     * @return \Illuminate\Http\JsonResponse
96
     */
97
    protected function errorCustomType($message, $statusCode = 400)
98
    {
99
        return $this->respondWithError($message, $statusCode);
100
    }
101
102
    /**
103
     * Returns a response that indicates multiple errors in an array.
104
     *
105
     * @param array $errors
106
     * @param $statusCode
107
     * @return \Illuminate\Http\JsonResponse
108
     */
109
    protected function errorArray(array $errors, $statusCode = 400)
110
    {
111
        return response()->json([
112
            'errors' => $errors
113
        ], $statusCode);
114
    }
115
116
    /**
117
     * Returns a response that mimics Laravel validation failure response.
118
     *
119
     * @param array $errors
120
     * @return \Illuminate\Http\JsonResponse
121
     */
122
    protected function errorValidation(array $errors)
123
    {
124
        return $this->errorArray($errors, 422);
125
    }
126
}