Role::getResource()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of Webcook security bundle.
5
 *
6
 * See LICENSE file in the root of the bundle. Webcook
7
 */
8
9
namespace Webcook\Cms\SecurityBundle\Entity;
10
11
use Symfony\Component\Security\Core\Role\RoleInterface;
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Webcook\Cms\CoreBundle\Base\BasicEntity;
14
use Doctrine\ORM\Mapping as ORM;
15
use ApiPlatform\Core\Annotation\ApiResource;
16
use ApiPlatform\Core\Annotation\ApiProperty;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
/**
20
 * System role entity.
21
 *
22
 * @ApiResource
23
 * @ORM\Table(name="SecurityRole")
24
 * @ORM\Entity()
25
 */
26
class Role extends BasicEntity implements RoleInterface
27
{
28
    /**
29
     * Role name.
30
     *
31
     * @ORM\Column(name="name", type="string", length=30)
32
     * @Assert\NotNull
33
     * @Assert\NotBlank
34
     */
35
    private $name;
36
37
    /**
38
     * Role identification.
39
     *
40
     * @ORM\Column(name="role", type="string", length=20, unique=true)
41
     * @Assert\NotNull
42
     * @Assert\NotBlank
43
     */
44
    private $role;
45
46
    /**
47
     * Resources of the role and their permissions.
48 49
     *
49
     * @ORM\OneToMany(targetEntity="RoleResource", mappedBy="role", cascade={"remove"}, fetch="EAGER"))
50 49
     **/
51 49
    private $resources;
52
53
    /**
54
     * Class constructor.
55
     */
56
    public function __construct()
57
    {
58 6
        $this->resources = new ArrayCollection();
59
    }
60 6
61
    /**
62
     * Get role object.
63
     *
64
     * @see RoleInterface
65
     */
66
    public function getRole()
67
    {
68 6
        return $this->role;
69
    }
70 6
71
    /**
72
     * Gets the value of name.
73
     *
74
     * @return mixed
75
     */
76
    public function getName()
77
    {
78
        return $this->name;
79
    }
80 49
81
    /**
82 49
     * Sets the value of name.
83
     *
84 49
     * @param string $name the name
85
     *
86
     * @return self
87
     */
88
    public function setName($name)
89
    {
90
        $this->name = $name;
91
92
        return $this;
93
    }
94 49
95
    /**
96 49
     * Sets the value of role.
97
     *
98 49
     * @param string $role the role
99
     *
100
     * @return self
101
     */
102
    public function setRole($role)
103
    {
104
        $this->role = $role;
105
106
        return $this;
107
    }
108 30
109
    /**
110 30
     * Get resource by name.
111 30
     *
112 30
     * @param string $name [description]
113
     *
114
     * @return Resource [description]
115
     */
116 1
    public function getResource($name)
117
    {
118
        foreach ($this->resources as $resource) {
119
            if ($resource->getResource()->getName() == $name) {
120
                return $resource;
121
            }
122
        }
123
124 2
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Webcook\Cms\SecurityBund...ntity\Role::getResource of type resource.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
125
    }
126 2
127
    /**
128
     * Gets the value of resources.
129
     *
130
     * @return mixed
131
     */
132
    public function getResources()
133
    {
134
        return $this->resources;
135
    }
136 1
137
    /**
138 1
     * Sets the value of resources.
139
     *
140 1
     * @param mixed $resources the resources
141
     *
142
     * @return self
143
     */
144
    public function setResources($resources)
145
    {
146
        $this->resources = $resources;
147
148
        return $this;
149
    }
150
}
151