Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserController
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 getUserMe
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 putUserMe
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 putUserMePassword
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 __validateUserRequest
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 __validateUserPasswordRequest
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Http\Controllers\api;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Request;
7use Illuminate\Http\JsonResponse;
8
9use App\Models\User;
10
11class UserController extends BaseController
12{
13
14
15    /**
16     * Get session user
17     *
18     * @author gbh
19     * @see http://swagger-humanverify.fruitbat.systems/#get-/api/users/me
20     */
21    public function getUserMe(Request $request):JsonResponse
22    {
23        /**
24         * Select session user
25         */
26        $me = User::whereMe()->first();
27
28        /**
29         * HTTP 200
30         */
31        return $this->sendResponse(200, $me);
32    } // getUserMe
33
34
35    /**
36     * Put session user
37     *
38     * @author gbh
39     * @see http://swagger-humanverify.fruitbat.systems/#put-/api/users/me
40     */
41    public function putUserMe(Request $request):JsonResponse
42    {
43        /**
44         * Select session user
45         */
46        $me = User::whereMe()->first();
47
48        /**
49         * Update session user
50         */
51        $me->name = $request->name;
52        $me->save();
53
54        /**
55         * HTTP 201
56         */
57        return $this->sendResponse(201, $me);
58    } // putUserMe
59
60
61    /**
62     * Put session user password
63     *
64     * @author gbh
65     * @see http://swagger-humanverify.fruitbat.systems/#put-/api/users/me
66     */
67    public function putUserMePassword(Request $request):JsonResponse
68    {
69        /**
70         * Validate password reqeust
71         * HTTP 400
72         */
73        $validationResult = $this->__validateUserPasswordRequest($request);
74        if (!is_bool($validationResult)) {
75            return $validationResult;
76        }
77
78        /**
79         * Update session user
80         */
81        $me = User::whereMe()->first();
82        $me->password = bcrypt($request->new_password);
83        $me->save();
84
85        /**
86         * HTTP 201
87         */
88        return $this->sendResponse(201, $me);
89    } // putUserMePassword
90
91
92    /**
93     * Validates request body of a User
94     *
95     */
96    protected function __validateUserRequest(Request $request) // @phpstan-ignore-line
97    {
98        $itemValidationRules = [
99            'name' => 'required|string',
100        ];
101
102        $validationResult = $this->__validateJson($itemValidationRules, $request);
103
104        if (is_array($validationResult)) {
105            return $this->sendError(400, $validationResult, "Validation error");
106        } else {
107            return true;
108        }
109    } // __validateUserRequest
110
111
112    /**
113     * Valdiates request body of a password change for session user
114     *
115     */
116    protected function __validateUserPasswordRequest(Request $request) // @phpstan-ignore-line
117    {
118        $itemValidationRules = [
119            'new_password' => 'required|string',
120            'new_password_again' => 'required|string|same:new_password',
121        ];
122
123        $validationResult = $this->__validateJson($itemValidationRules, $request);
124
125        if (is_array($validationResult)) {
126            return $this->sendError(400, $validationResult, "Validation error");
127        } else {
128            return true;
129        }
130
131    } // __validateUserPasswordRequest
132}