Passed
Push — master ( f1a782...17ae54 )
by Ion
04:19 queued 45s
created

Controller::userErrorResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Constants\TranslationCode;
6
use App\Models\User;
7
use App\Services\BaseService;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\Response;
10
use Illuminate\Support\Facades\Auth;
11
use IonGhitun\JwtToken\Jwt;
12
use Laravel\Lumen\Routing\Controller as BaseController;
13
14
/**
15
 * Class Controller
16
 *
17
 * All controllers should extend this controller
18
 *
19
 * @package App\Http\Controllers
20
 */
21
class Controller extends BaseController
22
{
23
    /** @var BaseService */
24
    protected $baseService;
25
26
    /** @var bool */
27
    private $isError = false;
28
29
    /** @var array */
30
    private $errorMessage = [];
31
32
    /** @var bool */
33
    private $isForbidden = false;
34
35
    /** @var array */
36
    private $forbiddenMessage = [];
37
38
    /** @var bool */
39
    private $userFault = false;
40
41
    /** @var null */
42
    private $result = null;
43
44
    /** @var array */
45
    private $pagination = [];
46
47
    /** @var bool */
48
    private $refreshToken = false;
49
50
    /**
51
     * Controller constructor.
52
     */
53
    public function __construct()
54
    {
55
        $this->baseService = new BaseService();
56
    }
57
58
    /**
59
     * Success response
60
     *
61
     * @param string|array|null $data
62
     * @param array|null $pagination
63
     * @param bool|null $refreshToken
64
     *
65
     * @return JsonResponse
66
     */
67
    protected function successResponse($data = null, $pagination = null, $refreshToken = null)
68
    {
69
        if ($data !== null) {
70
            $this->result = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type string or array is incompatible with the declared type null of property $result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        }
72
73
        if ($pagination !== null) {
74
            $this->pagination = $pagination;
75
        }
76
77
        if ($refreshToken !== null) {
78
            $this->refreshToken = $refreshToken;
79
        }
80
81
        return $this->buildResponse();
82
    }
83
84
    /**
85
     * Build the response.
86
     *
87
     * @return JsonResponse
88
     */
89
    private function buildResponse()
90
    {
91
        if ($this->isError) {
92
            $response = [
93
                'isError' => $this->isError,
94
                'userFault' => $this->userFault,
95
                'errorMessage' => $this->errorMessage
96
            ];
97
        } elseif ($this->isForbidden) {
98
            $response = [
99
                'isForbidden' => $this->isForbidden,
100
                'forbiddenMessage' => $this->forbiddenMessage
101
            ];
102
        } else {
103
            $response = [
104
                'isError' => $this->isError
105
            ];
106
107
            if ($this->result !== null) {
108
                $response['result'] = $this->result;
109
            }
110
111
            if (count($this->pagination) > 0) {
112
                $response['pagination'] = $this->pagination;
113
            }
114
        }
115
116
        if ($this->refreshToken && Auth::check()) {
117
            /** @var User $user */
118
            $user = Auth::user();
119
120
            $response['refreshedToken'] = Jwt::generateToken([
121
                'id' => $user->id
122
            ]);
123
        }
124
125
        return response()->json($response, Response::HTTP_OK);
126
    }
127
128
    /**
129
     * Return user fault response.
130
     *
131
     * @param array $errorMessage
132
     * @param bool|null $refreshToken
133
     *
134
     * @return JsonResponse
135
     */
136
    protected function userErrorResponse(array $errorMessage, $refreshToken = null)
137
    {
138
        $this->isError = true;
139
        $this->userFault = true;
140
        $this->errorMessage = $errorMessage;
141
142
        if ($refreshToken !== null) {
143
            $this->refreshToken = $refreshToken;
144
        }
145
146
        return $this->buildResponse();
147
    }
148
149
    /**
150
     * Return application error response.
151
     *
152
     * @return JsonResponse
153
     */
154
    protected function errorResponse()
155
    {
156
        $this->isError = true;
157
        $this->errorMessage = ['application' => TranslationCode::ERROR_APPLICATION];
158
159
        return $this->buildResponse();
160
    }
161
162
    /**
163
     * Return access forbidden response.
164
     *
165
     * @return JsonResponse
166
     */
167
    protected function forbiddenResponse()
168
    {
169
        $this->isForbidden = true;
170
        $this->forbiddenMessage = ['forbidden' => TranslationCode::ERROR_FORBIDDEN];
171
172
        return $this->buildResponse();
173
    }
174
}
175