TeamService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 105
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllUserRoles() 0 3 1
A createTeam() 0 5 1
A getAllTeamRoles() 0 7 1
A deleteTeam() 0 4 1
A getAllAdminRoles() 0 3 1
A updateTeam() 0 7 1
A getById() 0 3 1
A getAll() 0 3 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Http\Requests\TeamStoreRequest;
6
use Spatie\Permission\Models\Role;
7
8
class TeamService
9
{
10
11
12
    /**
13
     * Create a user team (Spatie role)
14
     *
15
     * @param TeamStoreRequest $request
16
     *
17
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
18
     */
19 1
    public function createTeam(TeamStoreRequest $request)
20
    {
21 1
        $team = Role::create(['name' => $request->name, 'guard_name' => 'web']);
22
23 1
        return $team;
24
    }
25
26
    /**
27
     * Update a user team (Spatie role)
28
     *
29
     * @param \App\Http\Requests\TeamStoreRequest $request
30
     * @param int $teamId
31
     *
32
     * @return \Spatie\Permission\Contracts\Role
33
     */
34 1
    public function updateTeam(TeamStoreRequest $request, int $teamId)
35
    {
36 1
        $team = Role::findById($teamId);
37 1
        $team->name = $request->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Spatie\Permission\Models\Role. 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...
38 1
        $team->save();
39
40 1
        return $team;
41
    }
42
43
    /**
44
     * Return the team from the database
45
     *
46
     * @param int $teamId
47
     *
48
     * @return \Spatie\Permission\Contracts\Role
49
     */
50 1
    public function getById(int $teamId)
51
    {
52 1
        return Role::findById($teamId);
53
    }
54
55
    /**
56
     * Get all teams.
57
     *
58
     * @return iterable
59
     */
60 1
    public function getAll()
61
    {
62 1
        return Role::whereNotIn('name', ['Super Admin', 'Admin', 'Individual', 'Organisation', 'Venue'])->get();
63
    }
64
65
    /**
66
     * Delete the team from the database
67
     *
68
     * @param int $teamId
69
     *
70
     * @throws \Exception
71
     */
72 1
    public function deleteTeam(int $teamId)
73
    {
74 1
        $team = Role::findById($teamId);
75 1
        $team->delete();
76 1
    }
77
78
    /**
79
     * Return all the roles related to administration
80
     *
81
     * @return \Spatie\Permission\Models\Role[]
82
     */
83 1
    public function getAllAdminRoles()
84
    {
85 1
        return Role::where('name', 'like', '%Admin%')->get(); // Admin, Super Admin
86
    }
87
88
    /**
89
     * Return all the roles related to teams
90
     * (just admins can be assigned to teams)
91
     *
92
     * @return \Spatie\Permission\Models\Role[]
93
     */
94 1
    public function getAllTeamRoles()
95
    {
96 1
        return Role::orWhere(function ($query) {
97 1
            $query->where('name', 'not like', '%Super admin%')
98 1
                ->where('name', 'not like', '%Admin%')
99 1
                ->where('name', 'not like', '%Registered%');
100 1
        })->get();
101
    }
102
103
    /**
104
     * Return all the roles Admins and Teams
105
     *
106
     * @param null $userId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userId is correct as it would always require null to be passed?
Loading history...
107
     *
108
     * @return \Illuminate\Support\Collection
109
     */
110 1
    public function getAllUserRoles($userId = null)
0 ignored issues
show
Unused Code introduced by
The parameter $userId 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

110
    public function getAllUserRoles(/** @scrutinizer ignore-unused */ $userId = null)

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...
111
    {
112 1
        return Role::all()->pluck('name');
113
    }
114
115
116
117
118
}