Passed
Push — master ( 7da4f0...e73242 )
by Hirofumi
04:58
created

DoctrineJobStore   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 43
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A append() 0 9 1
A storedJobsSince() 0 11 2
1
<?php
2
3
namespace Shippinno\Job\Infrastructure\Domain\Model;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use JMS\Serializer\SerializerBuilder;
8
use Shippinno\Job\Domain\Model\Job;
9
use Shippinno\Job\Domain\Model\JobStore;
10
use Shippinno\Job\Domain\Model\StoredJob;
11
use Shippinno\Job\Infrastructure\Serialization\JMS\BuildsSerializer;
12
13
class DoctrineJobStore extends EntityRepository implements JobStore
14
{
15
    use BuildsSerializer;
16
17
    /**
18
     * @param $em
19
     * @param ClassMetadata $class
20
     * @param SerializerBuilder $serializerBuilder
21
     */
22 3
    public function __construct($em, ClassMetadata $class, SerializerBuilder $serializerBuilder)
23
    {
24 3
        parent::__construct($em, $class);
25 3
        $this->buildSerializer($serializerBuilder);
26 3
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function append(Job $job): void
32
    {
33 2
        $storedEvent = new StoredJob(
34 2
            get_class($job),
35 2
            $this->serializer->serialize($job, 'json'),
36 2
            $job->createdAt()
37
        );
38 2
        $this->getEntityManager()->persist($storedEvent);
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function storedJobsSince(?int $jobId): array
45
    {
46 2
        $query = $this->createQueryBuilder('j');
47 2
        if (null !== $jobId) {
48 1
            $query->where('j.id > :id');
49 1
            $query->setParameters(['id' => $jobId]);
50
        }
51 2
        $query->orderBy('j.id');
52
53 2
        return $query->getQuery()->getResult();
54
    }
55
}
56