Lines 100.00% 34 / 34
Functions and Methods 100.00% 18 / 18
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
User 100.00% 34 / 34 100.00% 18 / 18 20 100.00% 1 / 1
 __serialize 100.00% 4 / 4 100.00% 1 / 1 1
 __unserialize 100.00% 2 / 2 100.00% 1 / 1 1
 getRoles 100.00% 3 / 3 100.00% 1 / 1 1
 getParams 100.00% 1 / 1 100.00% 1 / 1 1
 getParam 100.00% 3 / 3 100.00% 1 / 1 2
 setParams 100.00% 2 / 2 100.00% 1 / 1 1
 getIdentifier 100.00% 1 / 1 100.00% 1 / 1 1
 getUserIdentifier 100.00% 3 / 3 100.00% 1 / 1 2
 setIdentifier 100.00% 2 / 2 100.00% 1 / 1 1
 getPassword 100.00% 1 / 1 100.00% 1 / 1 1
 getGoogleId 100.00% 1 / 1 100.00% 1 / 1 1
 setGoogleId 100.00% 2 / 2 100.00% 1 / 1 1
 getGitHubId 100.00% 1 / 1 100.00% 1 / 1 1
 setGitHubId 100.00% 2 / 2 100.00% 1 / 1 1
 getGitLabId 100.00% 1 / 1 100.00% 1 / 1 1
 setGitLabId 100.00% 2 / 2 100.00% 1 / 1 1
 setRole 100.00% 2 / 2 100.00% 1 / 1 1
 getRole 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3namespace App\Entity;
4
5use App\Enum\UserRole;
6use App\Repository\UserRepository;
7use Doctrine\DBAL\Types\Types;
8use Doctrine\ORM\Mapping\Column;
9use Doctrine\ORM\Mapping\Entity;
10use Doctrine\ORM\Mapping\GeneratedValue;
11use Doctrine\ORM\Mapping\Id;
12use Doctrine\ORM\Mapping\Table;
13use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
14use Symfony\Component\Security\Core\User\UserInterface;
15use Symfony\Component\Validator\Constraints as Assert;
16use Symfony\Component\Validator\Constraints\NotBlank;
17
18#[Entity(repositoryClass: UserRepository::class)]
19#[Table(name: 'system_user')]
20#[UniqueEntity(fields: 'identifier', message: 'This identifier is already in use')]
21class User implements UserInterface
22{
23    #[Column, Id, GeneratedValue(strategy: 'SEQUENCE')]
24    public ?int $id = 0 {
25        get {
26            return $this->id;
27        }
28    }
29
30    #[Column(unique: true), NotBlank]
31    private string $identifier = '';
32
33    #[Column(type: Types::STRING, length: 50, enumType: UserRole::class)]
34    #[Assert\NotNull]
35    #[Assert\Type(type: UserRole::class, message: 'The role must be a valid user role.')]
36    private UserRole $role = UserRole::USER;
37
38    /**
39     * @var array<string>
40     */
41    #[Column(type: Types::JSON, nullable: true)]
42    private array $params = [];
43
44    #[Column(length: 100, nullable: true)]
45    private ?string $googleId = null;
46
47    #[Column(nullable: true)]
48    private ?int $gitHubId = null;
49
50    #[Column(nullable: true)]
51    private ?int $gitLabId = null;
52
53    /**
54     * @return array{
55     *     id: int|null,
56     *     identifier: string|null
57     * }
58     */
59    public function __serialize(): array
60    {
61        return [
62            'id' => $this->id,
63            'identifier' => $this->identifier,
64        ];
65    }
66
67    /**
68     * @param array{
69     *     id: int|null,
70     *     identifier: string|null
71     * } $data
72     */
73    public function __unserialize(array $data): void
74    {
75        $this->id = $data['id'] ?? null;
76        $this->identifier = (string) ($data['identifier'] ?? null);
77    }
78
79    #[\Override]
80    public function getRoles(): array
81    {
82        $roles = [$this->role->value];
83
84        // guarantee every user at least has ROLE_USER
85        $roles[] = UserRole::USER->value;
86
87        return \array_unique($roles);
88    }
89
90    /**
91     * @return array<string>
92     */
93    public function getParams(): array
94    {
95        return $this->params;
96    }
97
98    public function getParam(string $name): string
99    {
100        return \array_key_exists($name, $this->params)
101            ? $this->params[$name]
102            : '';
103    }
104
105    /**
106     * @param array<string> $params
107     */
108    public function setParams(array $params): self
109    {
110        $this->params = $params;
111
112        return $this;
113    }
114
115    public function getIdentifier(): string
116    {
117        return $this->identifier;
118    }
119
120    #[\Override]
121    public function getUserIdentifier(): string
122    {
123        if ('' === $this->identifier) {
124            throw new \LogicException('User identifier is not set.');
125        }
126
127        return $this->identifier;
128    }
129
130    public function setIdentifier(string $identifier): self
131    {
132        $this->identifier = $identifier;
133
134        return $this;
135    }
136
137    /**
138     * @todo this method is required by the the rememberMe functionality :(
139     */
140    public function getPassword(): ?string
141    {
142        return null;
143    }
144
145    public function getGoogleId(): ?string
146    {
147        return $this->googleId;
148    }
149
150    public function setGoogleId(?string $googleId): self
151    {
152        $this->googleId = $googleId;
153
154        return $this;
155    }
156
157    public function getGitHubId(): ?int
158    {
159        return $this->gitHubId;
160    }
161
162    public function setGitHubId(?int $gitHubId): self
163    {
164        $this->gitHubId = $gitHubId;
165
166        return $this;
167    }
168
169    public function getGitLabId(): ?int
170    {
171        return $this->gitLabId;
172    }
173
174    public function setGitLabId(?int $gitLabId): self
175    {
176        $this->gitLabId = $gitLabId;
177
178        return $this;
179    }
180
181    public function setRole(UserRole $role): self
182    {
183        $this->role = $role;
184
185        return $this;
186    }
187
188    public function getRole(): UserRole
189    {
190        return $this->role;
191    }
192}