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