EventRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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