1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\Doctrine\User; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
8
|
|
|
use Interop\Container\ContainerInterface; |
9
|
|
|
use Sinergi\Users\Container; |
10
|
|
|
use Sinergi\Users\Group\GroupRepositoryInterface; |
11
|
|
|
use Sinergi\Users\Session\SessionRepositoryInterface; |
12
|
|
|
use Sinergi\Users\User\UserEntityInterface; |
13
|
|
|
use Sinergi\Users\User\UserRepositoryInterface; |
14
|
|
|
|
15
|
|
|
class UserRepository extends EntityRepository implements UserRepositoryInterface |
16
|
|
|
{ |
17
|
|
|
private $container; |
18
|
|
|
|
19
|
|
|
public function __construct(EntityManager $em, ClassMetadata $class, ContainerInterface $container) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($em, $class); |
22
|
|
|
if ($container instanceof Container) { |
23
|
|
|
$this->container = $container; |
24
|
|
|
} else { |
25
|
|
|
$this->container = new Container($container); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** @return UserEntity */ |
30
|
|
View Code Duplication |
public function findByEmail(string $email) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$result = $this->createQueryBuilder('u') |
33
|
|
|
->where('u.email = :email') |
34
|
|
|
->setParameter('email', $email) |
35
|
|
|
->getQuery() |
36
|
|
|
->getResult(); |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
if ($result && $result[0]) { |
|
|
|
|
40
|
|
|
/** @var UserEntityInterface $user */ |
41
|
|
|
$user = $result[0]; |
42
|
|
|
$user->setGroupRepository($this->container->get(GroupRepositoryInterface::class)); |
43
|
|
|
$user->setSessionRepository($this->container->get(SessionRepositoryInterface::class)); |
44
|
|
|
return $user; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @return UserEntity */ |
51
|
|
View Code Duplication |
public function findById(int $id) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$result = $this->createQueryBuilder('u') |
54
|
|
|
->where('u.id = :id') |
55
|
|
|
->setParameter('id', $id) |
56
|
|
|
->getQuery() |
57
|
|
|
->getResult(); |
58
|
|
|
|
59
|
|
|
if ($result && $result[0]) { |
|
|
|
|
60
|
|
|
/** @var UserEntityInterface $user */ |
61
|
|
|
$user = $result[0]; |
62
|
|
|
$user->setGroupRepository($this->container->get(GroupRepositoryInterface::class)); |
63
|
|
|
$user->setSessionRepository($this->container->get(SessionRepositoryInterface::class)); |
64
|
|
|
return $user; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @return UserEntityInterface[] */ |
71
|
|
View Code Duplication |
public function findByGroupId(int $id) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$result = $this->createQueryBuilder('u') |
74
|
|
|
->where('u.groupId = :groupId') |
75
|
|
|
->setParameter('groupId', $id) |
76
|
|
|
->getQuery() |
77
|
|
|
->getResult(); |
78
|
|
|
|
79
|
|
|
if ($result && $result[0]) { |
80
|
|
|
/** @var UserEntityInterface $user */ |
81
|
|
|
foreach ($result as $user) { |
82
|
|
|
$user->setGroupRepository($this->container->get(GroupRepositoryInterface::class)); |
83
|
|
|
$user->setSessionRepository($this->container->get(SessionRepositoryInterface::class)); |
84
|
|
|
} |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @return UserEntityInterface */ |
92
|
|
View Code Duplication |
public function findByResetPasswordToken(string $token) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if (empty($token)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$result = $this->createQueryBuilder('u') |
99
|
|
|
->where('u.passwordResetToken = :token') |
100
|
|
|
->setParameter('token', $token) |
101
|
|
|
->getQuery() |
102
|
|
|
->getResult(); |
103
|
|
|
|
104
|
|
|
if ($result && $result[0]) { |
|
|
|
|
105
|
|
|
/** @var UserEntityInterface $user */ |
106
|
|
|
$user = $result[0]; |
107
|
|
|
$user->setGroupRepository($this->container->get(GroupRepositoryInterface::class)); |
108
|
|
|
$user->setSessionRepository($this->container->get(SessionRepositoryInterface::class)); |
109
|
|
|
return $user; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function save(UserEntityInterface $user) |
116
|
|
|
{ |
117
|
|
|
$this->getEntityManager()->persist($user); |
118
|
|
|
$this->getEntityManager()->flush($user); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.