Completed
Push — master ( 45301f...e76351 )
by Joachim
07:16
created

EventRepository::findRecentEvents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Entity;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use JMS\Serializer\SerializerInterface;
7
use Knp\Component\Pager\PaginatorInterface;
8
use Loevgaard\DandomainAltapayBundle\Event\EventInterface;
9
10
class EventRepository extends EntityRepository
11
{
12
    /**
13
     * @var SerializerInterface
14
     */
15
    private $serializer;
16
17
    public function __construct(ManagerRegistry $managerRegistry, PaginatorInterface $paginator, string $class, SerializerInterface $serializer)
18
    {
19
        parent::__construct($managerRegistry, $paginator, $class);
20
21
        $this->serializer = $serializer;
22
    }
23
24
    public function saveEvent(EventInterface $event)
25
    {
26
        $event = new Event(get_class($event), $this->serializer->serialize($event, 'json'));
27
        $this->save($event);
28
    }
29
30
    /**
31
     * @param int $offsetEventId
32
     * @param int $limit
33
     * @return Event[]|null
34
     */
35
    public function findRecentEvents(int $offsetEventId, int $limit = 0) : ?array
36
    {
37
        $qb = $this->repository->createQueryBuilder('e');
38
        $qb
39
            ->where($qb->expr()->gt('e.id', $offsetEventId))
40
            ->orderBy('e.id')
41
        ;
42
43
        if($limit) {
44
            $qb->setMaxResults($limit);
45
        }
46
47
        return $qb->getQuery()->getResult();
48
    }
49
}
50