Completed
Pull Request — master (#6)
by Christopher
15:01 queued 06:11
created

UserAuthoriser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 32.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 26
loc 80
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B readableBy() 0 32 4
A createableBy() 0 9 2
A updatableBy() 13 13 3
A deteteableBy() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace TechWilk\Rota\Authoriser;
4
5
use TechWilk\Rota\AuthoriserInterface;
6
use TechWilk\Rota\GroupQuery;
7
use TechWilk\Rota\User;
8
9
class UserAuthoriser implements AuthoriserInterface
10
{
11
    private $user;
12
13
    public function __construct(User $user)
14
    {
15
        $this->user = $user;
16
    }
17
18
    public function readableBy(User $user)
19
    {
20
        // it's me!
21
        if ($this->user === $user) {
22
            return true;
23
        }
24
        // we're a superadmin
25
        if ($user->isAdmin()) {
26
            return true;
27
        }
28
        // we're in the same group
29
        $myGroups = GroupQuery::create()
30
            ->useRoleQuery()
31
                ->useUserRoleQuery()
32
                    ->filterByUser($user)
33
                ->endUse()
34
            ->endUse()
35
            ->find();
36
        $sameGroups = GroupQuery::create()
37
            ->useRoleQuery()
38
                ->useUserRoleQuery()
39
                    ->filterByUser($this->user)
40
                ->endUse()
41
                ->filterByGroup($myGroups)
42
            ->endUse()
43
            ->count();
44
        if ($sameGroups > 0) {
45
            return true;
46
        }
47
48
        return false;
49
    }
50
51
    public static function createableBy(User $user)
52
    {
53
        // we're a superadmin
54
        if ($user->isAdmin()) {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61 View Code Duplication
    public function updatableBy(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        // it's me!
64
        if ($this->user === $user) {
65
            return true;
66
        }
67
        // we're a superadmin
68
        if ($user->isAdmin()) {
69
            return true;
70
        }
71
72
        return false;
73
    }
74
75 View Code Duplication
    public function deteteableBy(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        // it's me!
78
        if ($this->user === $user) {
79
            return true;
80
        }
81
        // we're a superadmin
82
        if ($user->isAdmin()) {
83
            return true;
84
        }
85
86
        return false;
87
    }
88
}
89