Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserController
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 getUsersDigest
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 postUser
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 getUserTransactions
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 __validateNewUserRequest
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace App\Http\Controllers\api\Root;
3
4use App\Http\Controllers\api\BaseController as BaseController;
5use App\Http\Controllers\Controller;
6
7use Illuminate\Http\Request;
8use Illuminate\Http\JsonResponse;
9
10use App\Models\User;
11use App\Models\UserConversionTransaction;
12
13class UserController extends BaseController
14{
15
16    /**
17     * Get users digest
18     *
19     * @author gbh
20     * @see  http://swagger-humanverify.fruitbat.systems/#get-/api/root/users/digest
21     */
22    public function getUsersDigest(Request $request):JsonResponse
23    {
24        /**
25         * Get pagination data from request
26         */
27        $paginationDataResult = $this->__validateAndExtractPaginationData($request);
28        if (!is_array($paginationDataResult)) {
29            return $paginationDataResult;
30        }
31        $size = $paginationDataResult['size'];
32
33        /**
34         * Get filter, order and sort data from request
35         */
36        $order = $request->query('order', 'id');
37        $sort = $request->query('sort', 'desc');
38        $filter = $request->query('filter', null);
39
40        /**
41         * Select page and items with optional filter
42         */
43        if (!is_null($filter)) {
44            $usersPage = User::where('name', 'like', "%$filter%")->orderBy($order, $sort)->paginate($size);
45        } else {
46            $usersPage = User::orderBy($order, $sort)->paginate($size);
47        }
48        $usersItem = $usersPage->items();
49
50        /**
51         * Build pagination hateoas
52         */
53        $paginationHateoas = $this->createPaginationFromOrm($usersPage);
54
55        /**
56         * HTTP 200
57         */
58        return $this->sendResponse(200, $usersItem, $paginationHateoas);
59    } // getUsersDigest
60
61
62    
63    /**
64     * Post a new user
65     *
66     * @author gbh
67     * @see http://swagger.humanverify.test/#post-/api/root/users
68     */
69    public function postUser(Request $request):JsonResponse
70    {
71        /**
72         * Validate new user
73         * HTTP 400
74         */
75        $validationResult = $this->__validateNewUserRequest($request);
76        if (!is_bool($validationResult)) {
77            return $validationResult;
78        }
79
80        /**
81         * Create new user
82         */
83        try {
84            $user = new User();
85            $user->name = $request->name;
86            $user->email = $request->email;
87            $user->password = bcrypt($request->password);
88            $user->role_id = 2;
89            $user->save();
90        }
91        /**
92         * Catch duplicate email
93         * HTTP 400
94         */
95        catch (\Illuminate\Database\QueryException $e) {
96            $errorCode = $e->errorInfo[1];
97            if ($errorCode == 1062) {
98                return $this->sendError(400, "User with email address ".$request->email." already exists");
99            }
100            return $this->sendError(500, $e->getMessage());
101        }
102
103        /**
104         * HTTP 201
105         */
106        return $this->sendResponse(201, $user);
107    } // postUser
108
109
110    /**
111     * Get transactions of one user, paged
112     *
113     * @author gbh
114     * @see
115     */
116    public function getUserTransactions(Int $id, Request $request):JsonResponse
117    {
118        /**
119         * Get pagination data from request
120         */
121        $paginationDataResult = $this->__validateAndExtractPaginationData($request);
122        if (!is_array($paginationDataResult)) {
123            return $paginationDataResult;
124        }
125        $size = $paginationDataResult['size'];
126
127        /**
128         * Select transactions page
129         */
130        $transactionsPage = UserConversionTransaction::where('user_id', '=', $id)->paginate($size);
131        $transactionsItems = $transactionsPage->items();
132
133        /**
134         * Build pagination hateoas
135         */
136        $paginationHateoas = $this->createPaginationFromOrm($transactionsPage);
137
138        /**
139         * HTTP 200
140         */
141        return $this->sendResponse(200, $transactionsItems, $paginationHateoas);
142    } // getUserTransactions
143
144
145    /**
146     * Validates request body of a new User
147     *
148     */
149    protected function __validateNewUserRequest(Request $request) // @phpstan-ignore-line
150    {
151        $itemValidationRules = [
152            'name' => 'required|string',
153            'email' => 'required|email',
154            'password' => 'required|string',
155            'password_again' => 'required|string|same:password',
156        ];
157
158        $validationResult = $this->__validateJson($itemValidationRules, $request);
159
160        if (is_array($validationResult)) {
161            return $this->sendError(400, $validationResult, "Validation error");
162        } else {
163            return true;
164        }
165    } // __validateNewUserRequest
166}