1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\Doctrine\Session; |
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\Session\SessionEntityInterface; |
11
|
|
|
use Sinergi\Users\Session\SessionRepositoryInterface; |
12
|
|
|
use Sinergi\Users\User\UserRepositoryInterface; |
13
|
|
|
|
14
|
|
|
class SessionRepository extends EntityRepository implements SessionRepositoryInterface |
15
|
|
|
{ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
View Code Duplication |
public function __construct(EntityManager $em, ClassMetadata $class, ContainerInterface $container) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
parent::__construct($em, $class); |
21
|
|
|
if ($container instanceof Container) { |
22
|
|
|
$this->container = $container; |
23
|
|
|
} else { |
24
|
|
|
$this->container = new Container($container); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function save(SessionEntityInterface $session) |
29
|
|
|
{ |
30
|
|
|
$this->getEntityManager()->persist($session); |
31
|
|
|
$this->getEntityManager()->flush($session); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @return SessionEntityInterface */ |
35
|
|
View Code Duplication |
public function findById(string $id) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$result = $this->createQueryBuilder('s') |
38
|
|
|
->where('s.id = :id') |
39
|
|
|
->setParameter('id', $id) |
40
|
|
|
->getQuery() |
41
|
|
|
->getResult(); |
42
|
|
|
|
43
|
|
|
if ($result && $result[0]) { |
|
|
|
|
44
|
|
|
/** @var SessionEntityInterface $session */ |
45
|
|
|
$session = $result[0]; |
46
|
|
|
$session->setUserRepository($this->container->get(UserRepositoryInterface::class)); |
47
|
|
|
return $session; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @return SessionEntityInterface[] */ |
54
|
|
View Code Duplication |
public function findByUserId(int $id) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$result = $this->createQueryBuilder('s') |
57
|
|
|
->where('s.userId = :userId') |
58
|
|
|
->setParameter('userId', $id) |
59
|
|
|
->getQuery() |
60
|
|
|
->getResult(); |
61
|
|
|
|
62
|
|
|
if ($result && $result[0]) { |
63
|
|
|
/** @var SessionEntityInterface $session */ |
64
|
|
|
foreach ($result as $session) { |
65
|
|
|
$session->setUserRepository($this->container->get(UserRepositoryInterface::class)); |
66
|
|
|
} |
67
|
|
|
return $result; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function delete(SessionEntityInterface $session) |
74
|
|
|
{ |
75
|
|
|
$this->getEntityManager()->remove($session); |
76
|
|
|
$this->getEntityManager()->flush($session); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.