Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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 13
    public function __construct($name = '')
48
    {
49 13
        $this->name = $name;
50 13
        $this->roles = new ArrayCollection();
51 13
    }
52
53
    /**
54
     * Get id
55
     *
56
     * @return int
57
     */
58 1
    public function getId()
59
    {
60 1
        return $this->id;
61
    }
62
63
    /**
64
     * Get string representation of object
65
     *
66
     * @return string
67
     */
68 1
    public function __toString()
69
    {
70 1
        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 4
    public function getRoles()
79
    {
80 4
        $result = [];
81
        /* @var $role RoleInterface */
82 4
        foreach ($this->roles as $role) {
83 3
            $result[] = $role->getRole();
84
        }
85
86 4
        return $result;
87
    }
88
89
    /**
90
     * Returns the true ArrayCollection of Roles.
91
     *
92
     * @return ArrayCollection
93
     */
94 2
    public function getRolesCollection()
95
    {
96 2
        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 7
    public function getRole($role = null)
107
    {
108
        /* @var $roleItem RoleInterface */
109 7
        foreach ($this->roles as $roleItem) {
110 4
            if ($role == $roleItem->getRole()) {
111 3
                return $roleItem;
112
            }
113
        }
114
115 7
        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 7
    public function hasRole($role)
126
    {
127 7
        if ($this->getRole($role)) {
128 2
            return true;
129
        }
130
131 7
        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 8
    public function addRole($role)
144
    {
145 8
        if (!$role instanceof Role) {
146 1
            throw new InvalidArgumentException('addRole takes a Role object as the parameter');
147
        }
148
149 7
        if (!$this->hasRole($role->getRole())) {
150 7
            $this->roles->add($role);
151
        }
152
153 7
        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 1
    public function removeRole($role)
164
    {
165 1
        $roleElement = $this->getRole($role);
166 1
        if ($roleElement) {
167 1
            $this->roles->removeElement($roleElement);
168
        }
169
170 1
        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 1
    public function setRoles(array $roles)
181
    {
182 1
        $this->roles->clear();
183 1
        foreach ($roles as $role) {
184 1
            $this->addRole($role);
185
        }
186
187 1
        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
     * @return GroupInterface
194
     */
195 1
    public function setRolesCollection(Collection $collection)
196
    {
197 1
        $this->roles = $collection;
198
199 1
        return $this;
200
    }
201
202
    /**
203
     * Return the name of the group
204
     *
205
     * @return string
206
     */
207 2
    public function getName()
208
    {
209 2
        return $this->name;
210
    }
211
212
    /**
213
     * Set the name of the group
214
     *
215
     * @param string $name New name of the group
216
     *
217
     * @return GroupInterface
218
     */
219 1
    public function setName($name)
220
    {
221 1
        $this->name = $name;
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * @Assert\Callback
228
     */
229 2
    public function isGroupValid(ExecutionContextInterface $context)
230
    {
231 2
        if (!(\count($this->getRoles()) > 0)) {
232
            $context
233 1
                ->buildViolation('errors.group.selectone', [])
234 1
                ->atPath('rolesCollection')
235 1
                ->addViolation();
236
        }
237 2
    }
238
}
239