UsersController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 32
c 1
b 1
f 0
dl 0
loc 88
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 2 1
A me() 0 3 1
A update() 0 10 2
A invite() 0 5 1
A list() 0 5 1
A finishInvitation() 0 9 2
A __construct() 0 3 1
A delete() 0 26 4
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\UserUpdateRequest;
6
use App\Http\Resources\UserResource;
7
use App\Models\User;
8
use App\Services\InvitationService;
9
use App\Services\TenantManager;
10
use App\TenantUser;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\DB;
14
15
class UsersController
16
{
17
    protected $tenantManager;
18
19
    /**
20
     * Create a new controller instance.
21
     *
22
     * @param TenantManager $tenantManager
23
     *
24
     * @return void
25
     */
26
    public function __construct(TenantManager $tenantManager)
27
    {
28
        $this->tenantManager = $tenantManager;
29
    }
30
31
    public function list()
32
    {
33
        $users = User::with('roles')->get();
34
35
        return UserResource::collection($users);
36
    }
37
38
    public function me()
39
    {
40
        return response()->json(Auth::user());
41
    }
42
43
    public function create(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public function create(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
44
    {
45
    }
46
47
    public function update(UserUpdateRequest $request, User $user)
48
    {
49
        $user->update($request->validated());
50
        $user->save();
51
52
        if ($request->get('role')) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on App\Http\Requests\UserUpdateRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        if ($request->/** @scrutinizer ignore-call */ get('role')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            $user->syncRoles($request->get('role'));
54
        }
55
56
        return response()->json($user, 200);
57
    }
58
59
    public function delete($userId)
60
    {
61
        if ($userId != Auth::user()->id) {
62
            $user = User::findOrFail($userId);
63
64
            $tenantUser = TenantUser::where('username', $user->email)->where('tenant_id', $this->tenantManager->getTenant()->id)->first();
65
66
            if (!$tenantUser) {
67
                throw new \Exception('inconsistency detected');
68
            }
69
70
            DB::transaction(function () use ($tenantUser, $user) {
71
                $user->grantedRecruitments()->detach();
72
                $tenantUser->delete();
73
74
                if ($user->pending_invitation) {
75
                    $user->forceDelete();
76
                } else {
77
                    $user->delete();
78
                }
79
            });
80
81
            return response(200);
82
        }
83
84
        return response('Cannot delete yourself', 403);
85
    }
86
87
    public function invite(Request $request)
88
    {
89
        $invitation = InvitationService::invite($request, Auth::user(), $this->tenantManager->getTenant());
90
91
        return response()->json($invitation);
92
    }
93
94
    public function finishInvitation(Request $request, $token)
95
    {
96
        $user = InvitationService::finishInvitation($request, $token);
97
98
        if (!$user) {
99
            return response('Invitation not found', 404);
100
        }
101
102
        return response()->json($user);
103
    }
104
}
105