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 | |||
7 | use Doctrine\Common\Collections\Collection; |
||
8 | use Doctrine\ORM\Mapping as ORM; |
||
9 | use FOS\UserBundle\Model\GroupInterface; |
||
10 | use InvalidArgumentException; |
||
11 | use Symfony\Component\Security\Core\Role\RoleInterface; |
||
12 | use Symfony\Component\Validator\Constraints as Assert; |
||
13 | use Symfony\Component\Validator\Context\ExecutionContextInterface; |
||
14 | |||
15 | /** |
||
16 | * Group |
||
17 | * |
||
18 | * @ORM\Entity |
||
19 | * @ORM\Table(name="kuma_groups") |
||
20 | */ |
||
21 | class Group implements RoleInterface, GroupInterface |
||
22 | { |
||
23 | /** |
||
24 | * @ORM\Id |
||
25 | * @ORM\Column(type="integer") |
||
26 | * @ORM\GeneratedValue(strategy="AUTO") |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * @Assert\NotBlank() |
||
32 | * @ORM\Column(type="string") |
||
33 | */ |
||
34 | protected $name; |
||
35 | |||
36 | /** |
||
37 | * @ORM\ManyToMany(targetEntity="Role") |
||
38 | * @ORM\JoinTable(name="kuma_groups_roles", |
||
39 | * joinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}, |
||
40 | * inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")} |
||
41 | * ) |
||
42 | */ |
||
43 | protected $roles; |
||
44 | |||
45 | /** |
||
46 | * Construct a new group |
||
47 | * |
||
48 | * @param string $name Name of the group |
||
49 | */ |
||
50 | public function __construct($name = '') |
||
51 | { |
||
52 | $this->name = $name; |
||
53 | $this->roles = new ArrayCollection(); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get id |
||
58 | * |
||
59 | * @return int |
||
60 | */ |
||
61 | public function getId() |
||
62 | { |
||
63 | return $this->id; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get string representation of object |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function __toString() |
||
72 | { |
||
73 | return $this->getName(); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Returns an array of strings (needed because Symfony ACL doesn't support using RoleInterface yet) |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getRoles() |
||
82 | { |
||
83 | $result = array(); |
||
84 | /* @var $role RoleInterface */ |
||
85 | foreach ($this->roles as $role) { |
||
86 | $result[] = $role->getRole(); |
||
87 | } |
||
88 | |||
89 | return $result; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Returns the true ArrayCollection of Roles. |
||
94 | * |
||
95 | * @return ArrayCollection |
||
96 | */ |
||
97 | public function getRolesCollection() |
||
98 | { |
||
99 | return $this->roles; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Pass a string, get the desired Role object or null. |
||
104 | * |
||
105 | * @param string $role |
||
0 ignored issues
–
show
|
|||
106 | * |
||
107 | * @return Role|null |
||
108 | */ |
||
109 | public function getRole($role = null) |
||
110 | { |
||
111 | /* @var $roleItem RoleInterface */ |
||
112 | foreach ($this->roles as $roleItem) { |
||
113 | if ($role == $roleItem->getRole()) { |
||
114 | return $roleItem; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | return null; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Pass a string, checks if we have that Role. Same functionality as getRole() except it returns a boolean. |
||
123 | * |
||
124 | * @param string $role |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function hasRole($role) |
||
129 | { |
||
130 | if ($this->getRole($role)) { |
||
131 | return true; |
||
132 | } |
||
133 | |||
134 | return false; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Adds a Role object to the ArrayCollection. Can't type hint due to interface so throws Exception. |
||
139 | * |
||
140 | * @param Role $role |
||
141 | * |
||
142 | * @return GroupInterface |
||
143 | * @throws InvalidArgumentException |
||
144 | */ |
||
145 | public function addRole($role) |
||
146 | { |
||
147 | if (!$role instanceof Role) { |
||
148 | throw new InvalidArgumentException("addRole takes a Role object as the parameter"); |
||
149 | } |
||
150 | |||
151 | if (!$this->hasRole($role->getRole())) { |
||
152 | $this->roles->add($role); |
||
153 | } |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Pass a string, remove the Role object from collection. |
||
160 | * |
||
161 | * @param string $role |
||
162 | * |
||
163 | * @return GroupInterface |
||
164 | */ |
||
165 | public function removeRole($role) |
||
166 | { |
||
167 | $roleElement = $this->getRole($role); |
||
168 | if ($roleElement) { |
||
169 | $this->roles->removeElement($roleElement); |
||
170 | } |
||
171 | |||
172 | return $this; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Pass an ARRAY of Role objects and will clear the collection and re-set it with new Roles. |
||
177 | * |
||
178 | * @param Role[] $roles array of Role objects. |
||
179 | * |
||
180 | * @return GroupInterface |
||
181 | */ |
||
182 | public function setRoles(array $roles) |
||
183 | { |
||
184 | $this->roles->clear(); |
||
185 | foreach ($roles as $role) { |
||
186 | $this->addRole($role); |
||
187 | } |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Directly set the ArrayCollection of Roles. Type hinted as Collection which is the parent of (Array|Persistent)Collection. |
||
194 | * |
||
195 | * @param Collection $collection |
||
196 | * |
||
197 | * @return GroupInterface |
||
198 | */ |
||
199 | public function setRolesCollection(Collection $collection) |
||
200 | { |
||
201 | $this->roles = $collection; |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Return the name of the group |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getName() |
||
212 | { |
||
213 | return $this->name; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Set the name of the group |
||
218 | * |
||
219 | * @param string $name New name of the group |
||
220 | * |
||
221 | * @return GroupInterface |
||
222 | */ |
||
223 | public function setName($name) |
||
224 | { |
||
225 | $this->name = $name; |
||
226 | |||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @Assert\Callback |
||
232 | * |
||
233 | * @param ExecutionContextInterface $context |
||
234 | */ |
||
235 | public function isGroupValid(ExecutionContextInterface $context) |
||
236 | { |
||
237 | if (!(count($this->getRoles()) > 0)) { |
||
238 | $context |
||
239 | ->buildViolation('errors.group.selectone', array()) |
||
240 | ->atPath('rolesCollection') |
||
241 | ->addViolation(); |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 |
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.