Completed
Push — master ( 2604a0...14e09a )
by Joachim
02:14
created

EventRepository::saveEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
    /**
25
     * @param EventInterface $event
26
     * @return Event
27
     */
28
    public function createFromDomainEvent(EventInterface $event) : Event
29
    {
30
        return new Event($event->getName(), $this->serializer->serialize($event, 'json'));
31
    }
32
33
    /**
34
     * @param int $offsetEventId
35
     * @param int $limit
36
     *
37
     * @return Event[]|null
38
     */
39
    public function findRecentEvents(int $offsetEventId, int $limit = 0): ?array
40
    {
41
        $qb = $this->getQueryBuilder('e');
42
        $qb
43
            ->where($qb->expr()->gt('e.id', $offsetEventId))
44
            ->orderBy('e.id')
45
        ;
46
47
        if ($limit) {
48
            $qb->setMaxResults($limit);
49
        }
50
51
        return $qb->getQuery()->getResult();
52
    }
53
}
54