Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

AdminBundle/Entity/GroupPropertiesTrait.php (1 issue)

Check that @param annotations have the correct type.

Documentation Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use FOS\UserBundle\Model\GroupInterface;
9
use InvalidArgumentException;
10
use Symfony\Component\Validator\Constraints as Assert;
11
use Symfony\Component\Validator\Context\ExecutionContextInterface;
12
13
/**
14
 * @final
15
 *
16
 * @internal
17
 */
18
trait GroupPropertiesTrait
19
{
20
    /**
21
     * @ORM\Id
22
     * @ORM\Column(type="integer")
23
     * @ORM\GeneratedValue(strategy="AUTO")
24
     */
25
    protected $id;
26
27
    /**
28
     * @Assert\NotBlank()
29
     * @ORM\Column(type="string")
30
     */
31
    protected $name;
32
33
    /**
34
     * @ORM\ManyToMany(targetEntity="Role")
35
     * @ORM\JoinTable(name="kuma_groups_roles",
36
     *      joinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")},
37
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
38
     * )
39
     */
40
    protected $roles;
41
42
    /**
43
     * Construct a new group
44
     *
45
     * @param string $name Name of the group
46
     */
47
    public function __construct($name = '')
48
    {
49
        $this->name = $name;
50
        $this->roles = new ArrayCollection();
51
    }
52
53
    /**
54
     * Get id
55
     *
56
     * @return int
57
     */
58
    public function getId()
59
    {
60
        return $this->id;
61
    }
62
63
    /**
64
     * Get string representation of object
65
     *
66
     * @return string
67
     */
68
    public function __toString()
69
    {
70
        return $this->getName();
71
    }
72
73
    /**
74
     * Returns an array of strings (needed because Symfony ACL doesn't support using RoleInterface yet)
75
     *
76
     * @return array
77
     */
78
    public function getRoles()
79
    {
80
        $result = array();
81
        /* @var $role RoleInterface */
82
        foreach ($this->roles as $role) {
83
            $result[] = $role->getRole();
84
        }
85
86
        return $result;
87
    }
88
89
    /**
90
     * Returns the true ArrayCollection of Roles.
91
     *
92
     * @return ArrayCollection
93
     */
94
    public function getRolesCollection()
95
    {
96
        return $this->roles;
97
    }
98
99
    /**
100
     * Pass a string, get the desired Role object or null.
101
     *
102
     * @param string $role
0 ignored issues
show
Should the type for parameter $role not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
103
     *
104
     * @return Role|null
105
     */
106
    public function getRole($role = null)
107
    {
108
        /* @var $roleItem RoleInterface */
109
        foreach ($this->roles as $roleItem) {
110
            if ($role == $roleItem->getRole()) {
111
                return $roleItem;
112
            }
113
        }
114
115
        return null;
116
    }
117
118
    /**
119
     * Pass a string, checks if we have that Role. Same functionality as getRole() except it returns a boolean.
120
     *
121
     * @param string $role
122
     *
123
     * @return bool
124
     */
125
    public function hasRole($role)
126
    {
127
        if ($this->getRole($role)) {
128
            return true;
129
        }
130
131
        return false;
132
    }
133
134
    /**
135
     * Adds a Role object to the ArrayCollection. Can't type hint due to interface so throws Exception.
136
     *
137
     * @param Role $role
138
     *
139
     * @return GroupInterface
140
     *
141
     * @throws InvalidArgumentException
142
     */
143
    public function addRole($role)
144
    {
145
        if (!$role instanceof Role) {
146
            throw new InvalidArgumentException('addRole takes a Role object as the parameter');
147
        }
148
149
        if (!$this->hasRole($role->getRole())) {
150
            $this->roles->add($role);
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * Pass a string, remove the Role object from collection.
158
     *
159
     * @param string $role
160
     *
161
     * @return GroupInterface
162
     */
163
    public function removeRole($role)
164
    {
165
        $roleElement = $this->getRole($role);
166
        if ($roleElement) {
167
            $this->roles->removeElement($roleElement);
168
        }
169
170
        return $this;
171
    }
172
173
    /**
174
     * Pass an ARRAY of Role objects and will clear the collection and re-set it with new Roles.
175
     *
176
     * @param Role[] $roles array of Role objects
177
     *
178
     * @return GroupInterface
179
     */
180
    public function setRoles(array $roles)
181
    {
182
        $this->roles->clear();
183
        foreach ($roles as $role) {
184
            $this->addRole($role);
185
        }
186
187
        return $this;
188
    }
189
190
    /**
191
     * Directly set the ArrayCollection of Roles. Type hinted as Collection which is the parent of (Array|Persistent)Collection.
192
     *
193
     * @param Collection $collection
194
     *
195
     * @return GroupInterface
196
     */
197
    public function setRolesCollection(Collection $collection)
198
    {
199
        $this->roles = $collection;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Return the name of the group
206
     *
207
     * @return string
208
     */
209
    public function getName()
210
    {
211
        return $this->name;
212
    }
213
214
    /**
215
     * Set the name of the group
216
     *
217
     * @param string $name New name of the group
218
     *
219
     * @return GroupInterface
220
     */
221
    public function setName($name)
222
    {
223
        $this->name = $name;
224
225
        return $this;
226
    }
227
228
    /**
229
     * @Assert\Callback
230
     *
231
     * @param ExecutionContextInterface $context
232
     */
233
    public function isGroupValid(ExecutionContextInterface $context)
234
    {
235
        if (!(count($this->getRoles()) > 0)) {
236
            $context
237
                ->buildViolation('errors.group.selectone', array())
238
                ->atPath('rolesCollection')
239
                ->addViolation();
240
        }
241
    }
242
}
243