Completed
Push — master ( ecedb8...999d6a )
by Jeff
04:44 queued 02:17
created

User::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Entity;
12
13
use Doctrine\ORM\Mapping as ORM;
14
use Serializable;
15
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
/**
20
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
21
 * @ORM\Table(name="mail_users", uniqueConstraints={@ORM\UniqueConstraint(name="user_idx", columns={"name", "domain_id"})})
22
 * @UniqueEntity({"name", "domain"})
23
 */
24
class User implements UserInterface, Serializable
25
{
26
    /**
27
     * @ORM\Id()
28
     * @ORM\GeneratedValue()
29
     * @ORM\Column(type="integer")
30
     */
31
    private ?int $id = null;
32
33
    /**
34
     * @ORM\ManyToOne(targetEntity="Domain", inversedBy="users")
35
     * @Assert\NotNull()
36
     */
37
    private ?Domain $domain = null;
38
39
    /**
40
     * @ORM\Column(type="string", name="name", options={"collation":"utf8_unicode_ci"})
41
     * @Assert\NotBlank()
42
     * @Assert\Regex(pattern="/^[a-z0-9\-\_.]{1,50}$/")
43
     */
44
    private string $name = '';
45
46
    /**
47
     * @ORM\Column(type="string", name="password", options={"collation":"utf8_unicode_ci"})
48
     */
49
    private string $password = '';
50
51
    /**
52
     * @Assert\Length(min="6", max="5000")
53
     */
54
    private ?string $plainPassword = null;
55
56
    /**
57
     * @ORM\Column(type="boolean", name="admin")
58
     */
59
    private bool $admin = false;
60
61
    /**
62
     * @ORM\Column(type="boolean", name="enabled")
63
     */
64
    private bool $enabled = true;
65
66
    /**
67
     * @ORM\Column(type="boolean", name="send_only")
68
     */
69
    private bool $sendOnly = false;
70
71
    /**
72
     * @ORM\Column(type="integer", name="quota")
73
     * @Assert\Range(min="0")
74
     * @Assert\NotBlank()
75
     */
76
    private int $quota = 0;
77
78
    private ?string $domainName = null;
79
80
    public function __toString(): string
81
    {
82
        if (null !== $this->domain) {
83
            return sprintf('%s@%s', $this->name, $this->domain->getName());
84
        }
85
86
        if (null !== $this->domainName) {
87
            return sprintf('%s@%s', $this->name, $this->domainName);
88
        }
89
90
        return '';
91
    }
92
93
    public function getDomain(): ?Domain
94
    {
95
        return $this->domain;
96
    }
97
98
    public function setDomain(Domain $domain): void
99
    {
100
        $this->domain = $domain;
101
    }
102
103
    public function getId(): ?int
104
    {
105
        return $this->id;
106
    }
107
108
    public function getName(): string
109
    {
110
        return $this->name;
111
    }
112
113
    public function setName(string $name): void
114
    {
115
        $this->name = $name;
116
    }
117
118
    public function getPassword(): string
119
    {
120
        return $this->password;
121
    }
122
123
    public function setPassword(string $password): void
124
    {
125
        $this->password = $password;
126
    }
127
128
    public function getRoles(): array
129
    {
130
        if ($this->admin) {
131
            return ['ROLE_ADMIN', 'ROLE_USER'];
132
        }
133
134
        return ['ROLE_USER'];
135
    }
136
137
    public function isAdmin(): bool
138
    {
139
        return $this->admin;
140
    }
141
142
    public function setAdmin(bool $admin): void
143
    {
144
        $this->admin = $admin;
145
    }
146
147
    public function getSalt(): string
148
    {
149
        $parts = explode('$', $this->password, 5);
150
151
        return $parts[3] ?? '';
152
    }
153
154
    public function getUsername(): string
155
    {
156
        return (string) $this;
157
    }
158
159
    public function eraseCredentials(): void
160
    {
161
        $this->plainPassword = '';
162
    }
163
164
    public function getPlainPassword(): ?string
165
    {
166
        return $this->plainPassword;
167
    }
168
169
    public function setPlainPassword(?string $plainPassword): void
170
    {
171
        $this->plainPassword = $plainPassword;
172
    }
173
174
    public function getEnabled(): bool
175
    {
176
        return $this->enabled;
177
    }
178
179
    public function setEnabled(bool $enabled): void
180
    {
181
        $this->enabled = $enabled;
182
    }
183
184
    public function getSendOnly(): bool
185
    {
186
        return $this->sendOnly;
187
    }
188
189
    public function setSendOnly(bool $sendOnly): void
190
    {
191
        $this->sendOnly = $sendOnly;
192
    }
193
194
    public function getQuota(): int
195
    {
196
        return $this->quota;
197
    }
198
199
    public function setQuota(int $quota): void
200
    {
201
        $this->quota = $quota;
202
    }
203
204
    public function serialize(): string
205
    {
206
        return serialize([$this->id, $this->password, $this->domain->getName(), $this->admin, $this->name]);
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        return serialize([$this->id, $this->password, $this->domain->/** @scrutinizer ignore-call */ getName(), $this->admin, $this->name]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
207
    }
208
209
    public function unserialize($serialized): void
210
    {
211
        [$this->id, $this->password, $this->domainName, $this->admin, $this->name] = unserialize($serialized, ['allowed_classes' => false]);
212
    }
213
}
214