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