InvitationService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A invite() 0 35 3
A finishInvitation() 0 25 3
A generateKey() 0 8 2
1
<?php
2
3
namespace App\Services;
4
5
use App\Mail\UserInvitation;
6
use App\Models\User;
7
use App\TenantUser;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\Mail;
11
12
class InvitationService
13
{
14
    const KEY_MAXLENGTH = 8;
15
16
    public static function invite(Request $request, $user, $tenant)
17
    {
18
        $invitation = new \App\Models\UserInvitation();
19
20
        $invitation->inviter_id = $user->id;
0 ignored issues
show
Bug introduced by
The property inviter_id does not seem to exist on App\Models\UserInvitation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
21
        $invitation->email = $request->get('email');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

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

21
        /** @scrutinizer ignore-call */ 
22
        $invitation->email = $request->get('email');

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...
22
        if ($request->get('name')) {
23
            $invitation->name = $request->get('name');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Models\UserInvitation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
24
        }
25
        $invitation->token = self::generateKey();
26
27
        $user = new User();
28
        $user->email = $request->get('email');
29
        $user->pending_invitation = true;
30
31
        $tenantUser = new TenantUser();
32
        $tenantUser->username = $user->email;
33
        $tenantUser->tenant_id = $tenant->id;
0 ignored issues
show
Bug introduced by
The property tenant_id does not exist on App\TenantUser. Did you mean tenant?
Loading history...
34
35
        DB::transaction(function () use ($tenantUser, $user, $invitation) {
36
            $tenantUser->save();
37
            $user->save();
38
            $invitation->save();
39
        });
40
41
        if ($request->get('role')) {
42
            $user->assignRole($request->get('role'));
43
        }
44
45
        $invitation->subdomain = $tenant->subdomain;
0 ignored issues
show
Bug introduced by
The property subdomain does not seem to exist on App\Models\UserInvitation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
46
        Mail::to($user->email)->queue(new UserInvitation($invitation));
47
48
        unset($invitation->token);
49
50
        return $invitation;
51
    }
52
53
    public static function finishInvitation(Request $request, $token)
54
    {
55
        $invitation = \App\Models\UserInvitation::where('token', $token)->first();
56
57
        if (!$invitation) {
58
            return false;
59
        }
60
61
        $user = User::where('email', $invitation->email)->first();
62
        if (!$user) {
63
            throw new \Exception('Invited user not found');
64
        }
65
66
        $user->name = $request->get('name');
67
        $user->password = bcrypt($request->get('password'));
68
        $user->pending_invitation = false;
69
70
        $invitation->token = null;
71
72
        DB::transaction(function () use ($user, $invitation) {
73
            $user->save();
74
            $invitation->save();
75
        });
76
77
        return $user;
78
    }
79
80
    private static function generateKey()
81
    {
82
        do {
83
            $key = strtoupper(\Str::random(self::KEY_MAXLENGTH));
0 ignored issues
show
Bug introduced by
The method random() does not exist on Str. ( Ignorable by Annotation )

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

83
            $key = strtoupper(\Str::/** @scrutinizer ignore-call */ random(self::KEY_MAXLENGTH));

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...
84
            $invitations = \App\Models\UserInvitation::where('token', $key)->count();
85
        } while ($invitations > 0);
86
87
        return $key;
88
    }
89
}
90