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

EventAuthoriser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 36.62 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 26
loc 71
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 23 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\Event;
7
use TechWilk\Rota\EventPersonQuery;
8
use TechWilk\Rota\User;
9
10
class EventAuthoriser implements AuthoriserInterface
11
{
12
    private $event;
13
14
    public function __construct(Event $event)
15
    {
16
        $this->event = $event;
17
    }
18
19
    public function readableBy(User $user)
20
    {
21
        // created by me
22
        if ($this->event->getCreatedBy() === $user) {
23
            return true;
24
        }
25
        // we're a superadmin
26
        if ($user->isAdmin()) {
27
            return true;
28
        }
29
        // we're involved in the event
30
        $involvementCount = EventPersonQuery::create()
31
            ->useUserRoleQuery()
32
                ->filterByUser($user)
33
            ->endUse()
34
            ->filterByEvent($this->event)
35
            ->count();
36
        if ($involvementCount > 0) {
37
            return true;
38
        }
39
40
        return false;
41
    }
42
43
    public static function createableBy(User $user)
44
    {
45
        // we're a superadmin
46
        if ($user->isAdmin()) {
47
            return true;
48
        }
49
50
        return false;
51
    }
52
53 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...
54
    {
55
        // created by me
56
        if ($this->event->getCreatedBy() === $user) {
57
            return true;
58
        }
59
        // we're a superadmin
60
        if ($user->isAdmin()) {
61
            return true;
62
        }
63
64
        return false;
65
    }
66
67 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...
68
    {
69
        // created by me
70
        if ($this->event->getCreatedBy() === $user) {
71
            return true;
72
        }
73
        // we're a superadmin
74
        if ($user->isAdmin()) {
75
            return true;
76
        }
77
78
        return false;
79
    }
80
}
81