Passed
Push — main ( 68245a...b2eda1 )
by Axel
05:57
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersBundle\Entity;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Doctrine\ORM\Mapping as ORM;
19
use Nucleos\UserBundle\Model\User as BaseUser;
20
use Nucleos\UserBundle\Validator\Constraints\Pattern as PasswordPattern;
21
use Symfony\Component\Validator\Constraints as Assert;
22
use Zikula\Bundle\CoreBundle\Doctrine\DBAL\CustomTypes;
23
24
// #[ORM\Entity(repositoryClass: UserRepository::class)] TODO remove if unneeded
25
#[ORM\Entity]
26
#[ORM\Table(name: 'nucleos_user__user')]
27
#[
28
    ORM\Index(fields: ['username'], name: 'username'),
29
    ORM\Index(fields: ['email'], name: 'email')
30
]
31
class User extends BaseUser
32
{
33
    use UserAttributesTrait;
34
35
    #[ORM\Id]
36
    #[ORM\Column]
37
    #[ORM\GeneratedValue(strategy: 'AUTO')]
38
    protected ?int $id = null;
39
40
    #[Assert\Length(min: 12)]
41
    #[PasswordPattern(minUpper: 1, minLower: 1, minNumeric: 1, minSpecial: 1)]
42
    protected ?string $plainPassword = null;
43
44
    #[ORM\ManyToMany(targetEntity: Group::class)]
45
    #[ORM\JoinTable(name: 'nucleos_user__user_group')]
46
    #[ORM\JoinColumn(name: 'user_id')]
47
    #[ORM\InverseJoinColumn(name: 'group_id')]
48
    protected Collection $groups;
49
50
    /**
51
     * Account State: The user's current state, see \Zikula\UsersModule\Constant::ACTIVATED_* for defined constants.
52
     * A state represented by a negative integer means that the user's account is in a pending state, and should not yet be considered a "real" user account.
53
     * For example, user accounts pending the completion of the registration process (because either moderation, e-mail verification, or both are in use)
54
     * will have a negative integer representing their state. If the user's registration request expires before it the process is completed, or if the administrator
55
     * denies the request for an new account, the user account record will be deleted.
56
     * When this deletion happens, it will be assumed by the system that no external module has yet interacted with the user account record,
57
     * because its state never progressed beyond its pending state, and therefore normal hooks/events may not be triggered
58
     * (although it is possible that events regarding the pending account may be triggered).
59
     *
60
     */
61
    #[Assert\Choice(callback: 'getActivatedValues')] // TODO replace by enum
62
    #[ORM\Column]
63
    private int $activated;
64
65
    /**
66
     * Account Approved Date/Time: The date and time the user's registration request was approved through the moderation process.
67
     * If the moderation process was not in effect at the time the user made a registration request, then this will be the date and time of the registration request.
68
     */
69
    #[ORM\Column(name: 'approved_date', type: CustomTypes::DATETIME_UTC)]
70
    private \DateTimeInterface $approvedDate;
71
72
    /**
73
     * The uid of the user account that approved the request to register a new account.
74
     * If this is the same as the user account's uid, then moderation was not in use at the time the request for a new account was made.
75
     * If this is -1, the the user account that approved the request has since been deleted. If this is 0, the user account has not yet been approved.
76
     */
77
    #[ORM\Column(name: 'approved_by')]
78
    private int $approvedBy;
79
80
    /**
81
     * Registration Date/Time: Date/time the user account was registered.
82
     * For users not pending the completion of the registration process, this is the date and time the user account completed the process.
83
     * For example, if registrations are moderated, then this is the date and time the registration request was approved.
84
     * If registration e-mail addresses must be verified, then this is the date and time the user completed the verification process.
85
     * If both moderation and verification are in use, then this is the later of those two dates.
86
     * If neither is in use, then this is simply the date and time the user's registration request was made.
87
     * If the user account's activated state is "pending registration" (implying that either moderation, verification, or both are in use)
88
     * then this will be the date and time the user made the registration request UNTIL the registration process is complete, and then it is updated as above.
89
     */
90
    #[ORM\Column(name: 'user_regdate', type: CustomTypes::DATETIME_UTC)]
91
    private \DateTimeInterface $registrationDate;
92
93
    /**
94
     * Additional attributes of this user
95
     */
96
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAttribute::class, cascade: ['all'], orphanRemoval: true, indexBy: 'name')]
97
    private Collection $attributes;
98
99
    public function __construct()
100
    {
101
        parent::__construct();
102
103
        $utcTZ = new \DateTimeZone('UTC');
104
        $this->approvedDate = new \DateTime('1970-01-01 00:00:00', $utcTZ);
105
        $this->approvedBy = 0;
106
        $this->registrationDate = new \DateTime('1970-01-01 00:00:00', $utcTZ);
107
108
        $this->attributes = new ArrayCollection();
109
    }
110
111
    public function getId(): ?int
112
    {
113
        return $this->id;
114
    }
115
116
    public function setId(?int $id): void
117
    {
118
        $this->id = $id;
119
    }
120
121
    public function getActivated(): int
122
    {
123
        return $this->activated;
124
    }
125
126
    public function setActivated(int $activated): self
127
    {
128
        $this->activated = $activated;
129
130
        return $this;
131
    }
132
133
    public function getApprovedDate(): \DateTimeInterface
134
    {
135
        if (null !== $this->getTimezone()) {
136
            $this->approvedDate->setTimeZone(new \DateTimeZone($this->getTimezone()));
137
        }
138
139
        return $this->approvedDate;
140
    }
141
142
    public function setApprovedDate(string|\DateTimeInterface $approvedDate): self
143
    {
144
        if ($approvedDate instanceof \DateTimeInterface) {
145
            $this->approvedDate = $approvedDate;
146
        } else {
147
            $this->approvedDate = new \DateTime($approvedDate);
148
        }
149
        $this->setTimezone($this->approvedDate->getTimeZone()->getName());
150
151
        return $this;
152
    }
153
154
    public function getApprovedBy(): int
155
    {
156
        return $this->approvedBy;
157
    }
158
159
    public function setApprovedBy(int $approvedBy): self
160
    {
161
        $this->approvedBy = $approvedBy;
162
163
        return $this;
164
    }
165
166
    public function isApproved(): bool
167
    {
168
        return 0 !== $this->approvedBy;
169
    }
170
171
    public function getRegistrationDate(): \DateTimeInterface
172
    {
173
        if (null !== $this->getTimezone()) {
174
            $this->registrationDate->setTimeZone(new \DateTimeZone($this->getTimezone()));
175
        }
176
177
        return $this->registrationDate;
178
    }
179
180
    public function setRegistrationDate(string|\DateTimeInterface $registrationDate): self
181
    {
182
        if ($registrationDate instanceof \DateTimeInterface) {
183
            $this->registrationDate = $registrationDate;
184
        } else {
185
            $this->registrationDate = new \DateTime($registrationDate);
186
        }
187
        $this->setTimezone($this->registrationDate->getTimeZone()->getName());
188
189
        return $this;
190
    }
191
192
    /**
193
     * Callback function used to validate the activated value.
194
     */
195
    public static function getActivatedValues(): array
196
    {
197
        return [
198
            UsersConstant::ACTIVATED_ACTIVE,
0 ignored issues
show
Bug introduced by
The type Zikula\UsersBundle\Entity\UsersConstant was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
199
            UsersConstant::ACTIVATED_INACTIVE,
200
            UsersConstant::ACTIVATED_PENDING_DELETE,
201
            UsersConstant::ACTIVATED_PENDING_REG,
202
        ];
203
    }
204
}
205