Completed
Push — master ( 7c0346...9ff857 )
by Philip
08:18
created

EntityService::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dontdrinkandroot\Service;
4
5
use Dontdrinkandroot\Entity\EntityInterface;
6
use Dontdrinkandroot\Exception\NoResultFoundException;
7
use Dontdrinkandroot\Repository\EntityRepositoryInterface;
8
9
/**
10
 * @author Philip Washington Sorst <[email protected]>
11
 */
12
class EntityService implements EntityServiceInterface
13
{
14
    /**
15
     * @var EntityRepositoryInterface
16
     */
17
    protected $repository;
18
19
    /**
20
     * @param EntityRepositoryInterface $repository
21
     */
22 4
    public function __construct(EntityRepositoryInterface $repository)
23
    {
24 4
        $this->repository = $repository;
25 4
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 4
    public function listAll()
31
    {
32 4
        return $this->repository->findAll();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function findById($id)
39
    {
40 2
        return $this->repository->find($id);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    public function fetchById($id)
47
    {
48 2
        $entity = $this->findById($id);
49 2
        if (null === $entity) {
50
            throw new NoResultFoundException('No entity with id: ' . $id);
51
        }
52
53 2
        return $entity;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2
    public function save(EntityInterface $entity, $forceFlush = false)
60
    {
61 2
        if (null === $entity->getId()) {
62 2
            $this->repository->persist($entity);
63
64 2
            return $entity;
65
        }
66
67
        /* Flush if not in transaction */
68
        if ($forceFlush || !$this->repository->getTransactionManager()->isInTransaction()) {
69
            $this->repository->flush($entity);
70
        }
71
72
        return $entity;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function removeById($id)
79
    {
80
        $this->repository->removeById($id);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function remove(EntityInterface $entity)
87
    {
88
        $this->repository->remove($entity);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function removeAll()
95
    {
96
        $this->repository->removeAll();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function listPaginated($page, $perPage)
103
    {
104
        return $this->repository->findPaginatedBy($page, $perPage);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getTransactionManager()
111
    {
112
        return $this->getRepository()->getTransactionManager();
113
    }
114
115
    /**
116
     * @return EntityRepositoryInterface
117
     */
118
    protected function getRepository()
119
    {
120
        return $this->repository;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getEntityClass()
127
    {
128
        return $this->getRepository()->getClassName();
129
    }
130
}
131