-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.php
More file actions
59 lines (49 loc) · 1.66 KB
/
UserManager.php
File metadata and controls
59 lines (49 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace App\Manager\User;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User\User;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class UserManager
{
/** @var EntityManagerInterface */
protected $em;
/** @var UserPasswordEncoderInterface */
protected $encoder;
public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $encoder)
{
$this->em = $em;
$this->encoder = $encoder;
}
public function get(int $id): ?User
{
return $this->em->getRepository(User::class)->find($id);
}
public function getByUsername(string $username): ?User
{
return $this->em->getRepository(User::class)->findOneByUsername($username);
}
public function getByEmail(string $email): ?User
{
return $this->em->getRepository(User::class)->findOneByEmail($email);
}
public function create(string $username, string $email, string $password): User
{
if ($this->getByUsername($username) !== null) {
throw new BadRequestHttpException('users.already_token_username');
}
if ($this->getByEmail($email) !== null) {
throw new BadRequestHttpException('users.already_taken_email');
}
$user = (new User())
->setUsername($username)
->setEmail($email)
->activate(true)
->setRoles(['ROLE_USER'])
;
$user->setPassword($this->encoder->encodePassword($user, $password));
$this->em->persist($user);
$this->em->flush();
return $user;
}
}