1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\Service\CrudServiceInterface; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
7
|
|
|
|
8
|
|
|
abstract class CrudServiceRestResourceController extends AbstractRestResourceController |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
6 |
|
protected function listEntities(int $page = 1, int $perPage = 50) |
14
|
|
|
{ |
15
|
6 |
|
return $this->getService()->findAllPaginated($page, $perPage); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
56 |
|
protected function fetchEntity($id) |
22
|
|
|
{ |
23
|
56 |
|
$entity = $this->getService()->find($id); |
24
|
56 |
|
if (null === $entity) { |
25
|
2 |
|
throw new NotFoundHttpException(); |
26
|
|
|
} |
27
|
|
|
|
28
|
56 |
|
return $entity; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
10 |
|
protected function createEntity($entity) |
35
|
|
|
{ |
36
|
10 |
|
return $this->getService()->create($entity); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
10 |
|
protected function updateEntity($entity) |
43
|
|
|
{ |
44
|
10 |
|
return $this->getService()->update($entity); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
2 |
|
protected function removeEntity($entity) |
51
|
|
|
{ |
52
|
2 |
|
$this->getService()->remove($entity); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
6 |
|
protected function listSubresource($entity, string $property, int $page = 1, int $perPage = 50) |
59
|
|
|
{ |
60
|
6 |
|
return $this->getService()->findAssociationPaginated($entity, $property, $page, $perPage); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
2 |
|
protected function createAssociation($parent, string $subresource) |
67
|
|
|
{ |
68
|
2 |
|
return $this->getService()->createAssociation($parent, $subresource); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
2 |
|
protected function createSubResource($parent, $subresource, $entity) |
75
|
|
|
{ |
76
|
2 |
|
return $this->getService()->create($entity); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
12 |
|
protected function addAssociation($parent, string $subresource, $subId) |
83
|
|
|
{ |
84
|
12 |
|
$this->getService()->addAssociation($parent, $subresource, $subId); |
85
|
12 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
12 |
|
protected function removeAssociation($parent, string $subresource, $subId = null) |
91
|
|
|
{ |
92
|
12 |
|
$this->getService()->removeAssociation($parent, $subresource, $subId); |
93
|
12 |
|
} |
94
|
|
|
|
95
|
72 |
|
protected function getServiceId() |
96
|
|
|
{ |
97
|
72 |
|
return $this->getCurrentRequest()->attributes->get('_service'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return CrudServiceInterface |
102
|
|
|
*/ |
103
|
|
|
abstract protected function getService(); |
104
|
|
|
} |
105
|
|
|
|