Completed
Push — master ( 5d31f9...7b1630 )
by Sander
21s queued 10s
created

src/Kunstmaan/AdminBundle/Entity/Group.php (1 issue)

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\Security\Core\Role\RoleInterface;
11
use Symfony\Component\Validator\Constraints as Assert;
12
use Symfony\Component\Validator\Context\ExecutionContextInterface;
13
14
/**
15
 * Group
16
 *
17
 * @ORM\Entity
18
 * @ORM\Table(name="kuma_groups")
19
 */
20
class Group implements RoleInterface, GroupInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Symfony\Component\Security\Core\Role\RoleInterface has been deprecated with message: The RoleInterface is deprecated since version 3.3 and will be removed in 4.0. Extend the Symfony\Component\Security\Core\Role\Role class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
{
22
    /**
23
     * @ORM\Id
24
     * @ORM\Column(type="integer")
25
     * @ORM\GeneratedValue(strategy="AUTO")
26
     */
27
    protected $id;
28
29
    /**
30
     * @Assert\NotBlank()
31
     * @ORM\Column(type="string")
32
     */
33
    protected $name;
34
35
    /**
36
     * @ORM\ManyToMany(targetEntity="Role")
37
     * @ORM\JoinTable(name="kuma_groups_roles",
38
     *      joinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")},
39
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
40
     * )
41
     */
42
    protected $roles;
43
44
    /**
45
     * Construct a new group
46
     *
47
     * @param string $name Name of the group
48
     */
49
    public function __construct($name = '')
50
    {
51
        $this->name = $name;
52
        $this->roles = new ArrayCollection();
53
    }
54
55
    /**
56
     * Get id
57
     *
58
     * @return int
59
     */
60
    public function getId()
61
    {
62
        return $this->id;
63
    }
64
65
    /**
66
     * Get string representation of object
67
     *
68
     * @return string
69
     */
70
    public function __toString()
71
    {
72
        return $this->getName();
73
    }
74
75
    /**
76
     * Returns an array of strings (needed because Symfony ACL doesn't support using RoleInterface yet)
77
     *
78
     * @return array
79
     */
80
    public function getRoles()
81
    {
82
        $result = array();
83
        /* @var $role RoleInterface */
84
        foreach ($this->roles as $role) {
85
            $result[] = $role->getRole();
86
        }
87
88
        return $result;
89
    }
90
91
    /**
92
     * Returns the true ArrayCollection of Roles.
93
     *
94
     * @return ArrayCollection
95
     */
96
    public function getRolesCollection()
97
    {
98
        return $this->roles;
99
    }
100
101
    /**
102
     * Pass a string, get the desired Role object or null.
103
     *
104
     * @param string $role
105
     *
106
     * @return Role|null
107
     */
108
    public function getRole($role = null)
109
    {
110
        /* @var $roleItem RoleInterface */
111
        foreach ($this->roles as $roleItem) {
112
            if ($role == $roleItem->getRole()) {
113
                return $roleItem;
114
            }
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * Pass a string, checks if we have that Role. Same functionality as getRole() except it returns a boolean.
122
     *
123
     * @param string $role
124
     *
125
     * @return bool
126
     */
127
    public function hasRole($role)
128
    {
129
        if ($this->getRole($role)) {
130
            return true;
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * Adds a Role object to the ArrayCollection. Can't type hint due to interface so throws Exception.
138
     *
139
     * @param Role $role
140
     *
141
     * @return GroupInterface
142
     *
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