1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Webhook\Bundle\Repository; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Webhook\Domain\Model\Webhook; |
11
|
|
|
use Webhook\Domain\Repository\WebhookRepositoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MessageRepository |
15
|
|
|
* |
16
|
|
|
* @package Webhook\Bundle\Repository |
17
|
|
|
*/ |
18
|
|
|
class WebhookRepository implements WebhookRepositoryInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ObjectManager |
22
|
|
|
*/ |
23
|
|
|
private $em; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MessageRepository constructor. |
27
|
|
|
* |
28
|
|
|
* @param EntityManager $em |
29
|
|
|
*/ |
30
|
|
|
public function __construct(EntityManager $em) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->em = $em; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $id |
37
|
|
|
* |
38
|
|
|
* @return null|object|Webhook |
39
|
|
|
*/ |
40
|
|
|
public function get($id) |
41
|
|
|
{ |
42
|
|
|
return $this->em->find(Webhook::class, $id); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $reference |
47
|
|
|
* |
48
|
|
|
* @return null|object|Webhook |
49
|
|
|
*/ |
50
|
|
|
public function getByReference($reference) |
51
|
|
|
{ |
52
|
|
|
$qb = $this->em->getRepository(Webhook::class)->createQueryBuilder('e'); |
53
|
|
|
|
54
|
|
|
$qb |
55
|
|
|
->where('e.reference=:reference') |
56
|
|
|
->setParameter('reference', $reference); |
57
|
|
|
|
58
|
|
|
return $qb->getQuery()->getResult(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $count |
63
|
|
|
* |
64
|
|
|
* @return Webhook[] |
65
|
|
|
*/ |
66
|
|
|
public function getLastWebhooks(int $count): array |
67
|
|
|
{ |
68
|
|
|
$qb = $this->em->getRepository(Webhook::class)->createQueryBuilder('e'); |
69
|
|
|
$qb->orderBy('e.created', 'DESC'); |
70
|
|
|
$qb->setMaxResults($count); |
71
|
|
|
|
72
|
|
|
return $qb->getQuery()->getResult(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Webhook $webhook |
77
|
|
|
*/ |
78
|
|
|
public function update(Webhook $webhook) |
79
|
|
|
{ |
80
|
|
|
$this->save($webhook); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Webhook $webhook |
85
|
|
|
*/ |
86
|
|
|
public function save(Webhook $webhook) |
87
|
|
|
{ |
88
|
|
|
$this->em->persist($webhook); |
89
|
|
|
$this->em->flush(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \DateTime $time |
94
|
|
|
*/ |
95
|
|
|
public function clearOutdated(\DateTime $time) |
96
|
|
|
{ |
97
|
|
|
$this->em->createQuery('DELETE FROM Webhook\Domain\Model\Webhook m where m.created < :time') |
98
|
|
|
->setParameter('time', $time->format(\DATE_RFC822))->execute(); |
99
|
|
|
} |
100
|
|
|
} |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.