Completed
Push — master ( 548654...ca6abf )
by Dmytro
24:35
created

WebhookRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

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:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
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
}