1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Dontdrinkandroot\Service; |
5
|
|
|
|
6
|
|
|
use Dontdrinkandroot\Entity\EntityInterface; |
7
|
|
|
use Dontdrinkandroot\Exception\NoResultFoundException; |
8
|
|
|
use Dontdrinkandroot\Repository\EntityRepositoryInterface; |
9
|
|
|
|
10
|
|
|
class EntityService extends AbstractService implements EntityServiceInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var EntityRepositoryInterface |
15
|
|
|
*/ |
16
|
|
|
protected $repository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param EntityRepositoryInterface $repository |
20
|
|
|
*/ |
21
|
|
|
public function __construct(EntityRepositoryInterface $repository) |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$this->repository = $repository; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function listAll() |
31
|
|
|
{ |
32
|
|
|
return $this->repository->findAll(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function findById($id) |
39
|
|
|
{ |
40
|
|
|
return $this->repository->find($id); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function fetchById($id) |
47
|
|
|
{ |
48
|
|
|
$entity = $this->findById($id); |
49
|
|
|
if (null === $entity) { |
50
|
|
|
throw new NoResultFoundException('No entity with id: ' . $id); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $entity; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function save(EntityInterface $entity) |
60
|
|
|
{ |
61
|
|
|
if (null === $entity->getId()) { |
62
|
|
|
$this->repository->persist($entity); |
63
|
|
|
|
64
|
|
|
return $entity; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->repository->flush($entity); |
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
|
|
|
* @return EntityRepositoryInterface |
106
|
|
|
*/ |
107
|
|
|
protected function getRepository() |
108
|
|
|
{ |
109
|
|
|
return $this->repository; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|