1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProjetNormandie\UserBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\Persistence\ObjectRepository; |
7
|
|
|
use ProjetNormandie\UserBundle\Entity\User; |
8
|
|
|
|
9
|
|
|
class UserManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Constructor. |
13
|
|
|
*/ |
14
|
|
|
public function __construct(private readonly EntityManagerInterface $em) |
15
|
|
|
{ |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param User $user |
20
|
|
|
*/ |
21
|
|
|
public function deleteUser(User $user): void |
22
|
|
|
{ |
23
|
|
|
$this->em->remove($user); |
24
|
|
|
$this->em->flush(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param User $user |
29
|
|
|
*/ |
30
|
|
|
public function updateUser(User $user): void |
31
|
|
|
{ |
32
|
|
|
$this->em->persist($user); |
33
|
|
|
$this->em->flush(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $email |
38
|
|
|
* @return User|null |
39
|
|
|
*/ |
40
|
|
|
public function findUserByEmail(string $email): ?User |
41
|
|
|
{ |
42
|
|
|
return $this->findUserBy(['email' => $email]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $username |
47
|
|
|
* @return User|null |
48
|
|
|
*/ |
49
|
|
|
public function findUserByUsername(string $username): ?User |
50
|
|
|
{ |
51
|
|
|
return $this->findUserBy(['username' => $username]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $usernameOrEmail |
56
|
|
|
* @return User|null |
57
|
|
|
*/ |
58
|
|
|
public function findUserByUsernameOrEmail(string $usernameOrEmail): ?User |
59
|
|
|
{ |
60
|
|
|
if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) { |
61
|
|
|
$user = $this->findUserByEmail($usernameOrEmail); |
62
|
|
|
if (null !== $user) { |
63
|
|
|
return $user; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return $this->findUserByUsername($usernameOrEmail); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $token |
71
|
|
|
* @return User|null |
72
|
|
|
*/ |
73
|
|
|
public function findUserByConfirmationToken(string $token): ?User |
74
|
|
|
{ |
75
|
|
|
return $this->findUserBy(['confirmationToken' => $token]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $criteria |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
private function findUserBy(array $criteria): mixed |
84
|
|
|
{ |
85
|
|
|
return $this->getRepository()->findOneBy($criteria); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return ObjectRepository |
90
|
|
|
*/ |
91
|
|
|
protected function getRepository(): ObjectRepository |
92
|
|
|
{ |
93
|
|
|
return $this->em->getRepository('ProjetNormandie\UserBundle\Entity\User'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|