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

EntityService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 114
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A listAll() 0 4 1
A findById() 0 4 1
A fetchById() 0 9 2
A save() 0 15 3
A removeById() 0 4 1
A remove() 0 4 1
A removeAll() 0 4 1
A listPaginated() 0 4 1
A getTransactionManager() 0 4 1
A getRepository() 0 4 1
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