Completed
Push — master ( 874644...01e6fb )
by Philip
23:41
created

EntityService::getTransactionManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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