UserRelations   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 151
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogsLoginFailure() 0 3 1
A getUserGroups() 0 3 1
A getLogsLogin() 0 3 1
A getLogsRequest() 0 3 1
A getRoles() 0 10 1
A addUserGroup() 0 9 2
A removeUserGroup() 0 7 2
A clearUserGroups() 0 5 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Entity/Traits/UserRelations.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Entity\Traits;
10
11
use App\Entity\LogLogin;
12
use App\Entity\LogLoginFailure;
13
use App\Entity\LogRequest;
14
use App\Entity\User;
15
use App\Entity\UserGroup;
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Doctrine\ORM\Mapping as ORM;
19
use Symfony\Component\Serializer\Annotation\Groups;
20
21
/**
22
 * Class UserRelations
23
 *
24
 * @package App\Entity\Traits
25
 * @author TLe, Tarmo Leppänen <[email protected]>
26
 */
27
trait UserRelations
28
{
29
    /**
30
     * @var Collection<int, UserGroup>|ArrayCollection<int, UserGroup>
31
     */
32
    #[ORM\ManyToMany(
33
        targetEntity: UserGroup::class,
34
        inversedBy: 'users',
35
    )]
36
    #[ORM\JoinTable(
37
        name: 'user_has_user_group',
38
    )]
39
    #[Groups([
40
        'User.userGroups',
41
    ])]
42
    protected Collection | ArrayCollection $userGroups;
43
44
    /**
45
     * @var Collection<int, LogRequest>|ArrayCollection<int, LogRequest>
46
     */
47
    #[ORM\OneToMany(
48
        mappedBy: 'user',
49
        targetEntity: LogRequest::class,
50
    )]
51
    #[Groups([
52
        'User.logsRequest',
53
    ])]
54
    protected Collection | ArrayCollection $logsRequest;
55
56
    /**
57
     * @var Collection<int, LogLogin>|ArrayCollection<int, LogLogin>
58
     */
59
    #[ORM\OneToMany(
60
        mappedBy: 'user',
61
        targetEntity: LogLogin::class,
62
    )]
63
    #[Groups([
64
        'User.logsLogin',
65
    ])]
66
    protected Collection | ArrayCollection $logsLogin;
67
68
    /**
69
     * @var Collection<int, LogLoginFailure>|ArrayCollection<int, LogLoginFailure>
70
     */
71
    #[ORM\OneToMany(
72
        mappedBy: 'user',
73
        targetEntity: LogLoginFailure::class,
74
    )]
75
    #[Groups([
76
        'User.logsLoginFailure',
77
    ])]
78
    protected Collection | ArrayCollection $logsLoginFailure;
79
80
    /**
81
     * Getter for roles.
82
     *
83
     * Note that this will only return _direct_ roles that user has and
84
     * not the inherited ones!
85
     *
86
     * If you want to get user inherited roles you need to implement that
87
     * logic by yourself OR use eg. `/user/{uuid}/roles` API endpoint.
88
     *
89
     * @return array<int, string>
90
     */
91 271
    #[Groups([
92
        'User.roles',
93
94
        User::SET_USER_PROFILE,
95
    ])]
96
    public function getRoles(): array
97
    {
98 271
        return $this->userGroups
99 271
            ->map(static fn (UserGroup $userGroup): string => $userGroup->getRole()->getId())
100 271
            ->toArray();
101
    }
102
103
    /**
104
     * Getter for user groups collection.
105
     *
106
     * @return Collection<int, UserGroup>|ArrayCollection<int, UserGroup>
107
     */
108 46
    public function getUserGroups(): Collection | ArrayCollection
109
    {
110 46
        return $this->userGroups;
111
    }
112
113
    /**
114
     * Getter for user request log collection.
115
     *
116
     * @return Collection<int, LogRequest>|ArrayCollection<int, LogRequest>
117
     */
118 2
    public function getLogsRequest(): Collection | ArrayCollection
119
    {
120 2
        return $this->logsRequest;
121
    }
122
123
    /**
124
     * Getter for user login log collection.
125
     *
126
     * @return Collection<int, LogLogin>|ArrayCollection<int, LogLogin>
127
     */
128 2
    public function getLogsLogin(): Collection | ArrayCollection
129
    {
130 2
        return $this->logsLogin;
131
    }
132
133
    /**
134
     * Getter for user login failure log collection.
135
     *
136
     * @return Collection<int, LogLoginFailure>|ArrayCollection<int, LogLoginFailure>
137
     */
138 18
    public function getLogsLoginFailure(): Collection | ArrayCollection
139
    {
140 18
        return $this->logsLoginFailure;
141
    }
142
143
    /**
144
     * Method to attach new user group to user.
145
     */
146 29
    public function addUserGroup(UserGroup $userGroup): self
147
    {
148 29
        if ($this->userGroups->contains($userGroup) === false) {
149 28
            $this->userGroups->add($userGroup);
150
151 28
            $userGroup->addUser($this);
152
        }
153
154 29
        return $this;
155
    }
156
157
    /**
158
     * Method to remove specified user group from user.
159
     */
160 7
    public function removeUserGroup(UserGroup $userGroup): self
161
    {
162 7
        if ($this->userGroups->removeElement($userGroup)) {
163 6
            $userGroup->removeUser($this);
164
        }
165
166 7
        return $this;
167
    }
168
169
    /**
170
     * Method to remove all many-to-many user group relations from current
171
     * user.
172
     */
173 3
    public function clearUserGroups(): self
174
    {
175 3
        $this->userGroups->clear();
176
177 3
        return $this;
178
    }
179
}
180