Completed
Push — master ( bd02ec...4ff445 )
by Dmytro
04:14
created

WebhookRepository::getLastWebhooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Webhook\Bundle\Repository;
6
7
8
use Doctrine\ORM\EntityManager;
9
use Webhook\Domain\Model\Webhook;
10
use Webhook\Domain\Repository\WebhookRepositoryInterface;
11
12
/**
13
 * Class MessageRepository
14
 *
15
 * @package Webhook\Bundle\Repository
16
 */
17
class WebhookRepository implements WebhookRepositoryInterface
18
{
19
    /**
20
     * @var EntityManager
21
     */
22
    private $em;
23
24
    /**
25
     * MessageRepository constructor.
26
     *
27
     * @param EntityManager $em
28
     */
29
    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...
30
    {
31
        $this->em = $em;
32
    }
33
34
    /**
35
     * @param $id
36
     *
37
     * @return null|object|Webhook
38
     */
39
    public function get($id)
40
    {
41
        return $this->em->find(Webhook::class, $id);
42
    }
43
44
    /**
45
     * @param int $count
46
     *
47
     * @return Webhook[]
48
     */
49
    public function getLastWebhooks(int $count): array
50
    {
51
        $qb = $this->em->getRepository(Webhook::class)->createQueryBuilder('e');
52
        $qb->orderBy('e.created', 'DESC');
53
        $qb->setMaxResults($count);
54
55
        return $qb->getQuery()->getResult();
56
    }
57
58
    /**
59
     * @param Webhook $webhook
60
     */
61
    public function update(Webhook $webhook)
62
    {
63
        $this->save($webhook);
64
    }
65
66
    /**
67
     * @param Webhook $webhook
68
     */
69
    public function save(Webhook $webhook)
70
    {
71
        $this->em->persist($webhook);
72
        $this->em->flush();
73
    }
74
75
    /**
76
     * @param \DateTime $time
77
     */
78
    public function clearOutdated(\DateTime $time)
79
    {
80
        $this->em->createQuery('DELETE FROM Webhook\Domain\Model\Webhook m where m.created  < :time')
81
            ->setParameter('time', $time->format(\DATE_RFC822))->execute();
82
    }
83
}