Completed
Pull Request — master (#2720)
by Jeroen
12:50
created

BaseUser::getCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use FOS\UserBundle\Model\GroupInterface;
8
use FOS\UserBundle\Model\User as AbstractUser;
9
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictions;
10
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
11
use Symfony\Component\Validator\Constraints\Email;
12
use Symfony\Component\Validator\Constraints\NotBlank;
13
use Symfony\Component\Validator\Mapping\ClassMetadata;
14
15
abstract class BaseUser extends AbstractUser
16
{
17
    /**
18
     * @ORM\Id
19
     * @ORM\Column(type="integer")
20
     * @ORM\GeneratedValue(strategy="AUTO")
21
     */
22
    protected $id;
23
24
    /**
25
     * The doctrine metadata is set dynamically in Kunstmaan\AdminBundle\EventListener\MappingListener
26
     */
27
    protected $groups;
28
29
    /**
30
     * @ORM\Column(type="string", name="admin_locale", length=5, nullable=true)
31
     */
32
    protected $adminLocale;
33
34
    /**
35
     * @ORM\Column(type="boolean", name="password_changed", nullable=true)
36
     */
37
    protected $passwordChanged;
38
39
    /**
40
     * @ORM\Column(name="google_id", type="string", length=255, nullable=true)
41
     */
42
    protected $googleId;
43
44
    /**
45
     * @var \DateTimeImmutable|null
46
     * @ORM\Column(name="created_at", type="datetime_immutable", nullable=true)
47 35
     */
48
    protected $createdAt;
49 35
50 35
    /**
51 35
     * @var string|null
52
     * @ORM\Column(name="created_by", type="string", nullable=true)
53
     */
54
    protected $createdBy;
55
56
    /**
57
     * Construct a new user
58 3
     */
59
    public function __construct()
60 3
    {
61
        parent::__construct();
62
        $this->groups = new ArrayCollection();
63
        $this->createdAt = new \DateTimeImmutable();
64
    }
65
66
    /**
67
     * Get id
68
     *
69
     * @return int
70 8
     */
71
    public function getId()
72 8
    {
73
        return $this->id;
74 8
    }
75
76
    /**
77
     * Set id
78
     *
79
     * @param int $id
80
     *
81
     * @return BaseUser
82 1
     */
83
    public function setId($id)
84 1
    {
85
        $this->id = $id;
86 1
87 1
        return $this;
88
    }
89 1
90 1
    /**
91
     * Gets the groupIds for the user.
92
     *
93
     * @return array
94 1
     */
95
    public function getGroupIds()
96
    {
97
        $groups = $this->groups;
98
99
        $groupIds = array();
100
        if (\count($groups) > 0) {
101
            /* @var $group GroupInterface */
102 6
            foreach ($groups as $group) {
103
                $groupIds[] = $group->getId();
104 6
            }
105
        }
106
107
        return $groupIds;
108
    }
109
110
    /**
111
     * Gets the groups the user belongs to.
112 1
     *
113
     * @return ArrayCollection
114 1
     */
115
    public function getGroups()
116
    {
117
        return $this->groups;
118
    }
119
120
    /**
121
     * Get adminLocale
122
     *
123
     * @return string
124 2
     */
125
    public function getAdminLocale()
126 2
    {
127
        return $this->adminLocale;
128 2
    }
129
130
    /**
131
     * Set adminLocale
132
     *
133
     * @param string $adminLocale
134
     *
135
     * @return BaseUser
136 1
     */
137
    public function setAdminLocale($adminLocale)
138 1
    {
139
        $this->adminLocale = $adminLocale;
140
141
        return $this;
142
    }
143
144
    /**
145
     * is passwordChanged
146
     *
147
     * @return bool
148 2
     */
149
    public function isPasswordChanged()
150 2
    {
151
        return $this->passwordChanged;
152 2
    }
153
154
    /**
155
     * Set passwordChanged
156
     *
157
     * @param bool $passwordChanged
158 1
     *
159
     * @return User
160 1
     */
161
    public function setPasswordChanged($passwordChanged)
162
    {
163
        $this->passwordChanged = $passwordChanged;
164
165
        return $this;
166 3
    }
167
168 3
    /**
169 3
     * @return mixed
170
     */
171
    public function getGoogleId()
172
    {
173
        return $this->googleId;
174 1
    }
175
176 1
    /**
177 1
     * @param mixed $googleId
178 1
     */
179
    public function setGoogleId($googleId)
180 1
    {
181 1
        $this->googleId = $googleId;
182
    }
183
184 1
    /**
185 1
     * @param ClassMetadata $metadata
186 1
     */
187 1
    public static function loadValidatorMetadata(ClassMetadata $metadata)
188
    {
189
        $metadata->addPropertyConstraint('username', new NotBlank());
190 1
        $metadata->addPropertyConstraints(
191 1
            'plainPassword',
192
            array(
193
                new NotBlank(array('groups' => array('Registration'))),
194 1
                new PasswordRestrictions(array('groups' => array('Registration', 'Default'))),
195
            )
196
        );
197
        $metadata->addPropertyConstraint('email', new NotBlank());
198
        $metadata->addPropertyConstraint('email', new Email());
199
        $metadata->addConstraint(new UniqueEntity(array(
200
            'fields' => 'username',
201
            'message' => 'errors.user.loginexists',
202
        )));
203
        $metadata->addConstraint(new UniqueEntity(array(
204
            'fields' => 'email',
205
            'message' => 'errors.user.emailexists',
206 1
        )));
207
    }
208 1
209
    /**
210
     * Return class name of form type used to add & edit users
211
     *
212
     * @return string
213
     */
214
    abstract public function getFormTypeClass();
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function isAccountNonLocked()
220
    {
221
        return $this->isEnabled();
222
    }
223
224
    public function getCreatedAt(): ?\DateTimeImmutable
225
    {
226
        return $this->createdAt;
227
    }
228
229
    public function getCreatedBy(): ?string
230
    {
231
        return $this->createdBy;
232
    }
233
234
    public function setCreatedBy(string $createdBy): void
235
    {
236
        $this->createdBy = $createdBy;
237
    }
238
}
239