1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\RestMetadataFactory; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Service\Normalizer; |
7
|
|
|
use Dontdrinkandroot\Service\CrudServiceInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
10
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
11
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
12
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Philip Washington Sorst <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractCrudServiceRestResourceController extends AbstractRestResourceController |
18
|
|
|
{ |
19
|
80 |
|
public function __construct( |
20
|
|
|
ValidatorInterface $validator, |
21
|
|
|
RequestStack $requestStack, |
22
|
|
|
RestMetadataFactory $metadataFactory, |
23
|
|
|
PropertyAccessorInterface $propertyAccessor, |
24
|
|
|
SerializerInterface $serializer |
25
|
|
|
) { |
26
|
80 |
|
parent::__construct( |
27
|
80 |
|
$validator, |
28
|
80 |
|
$requestStack, |
29
|
80 |
|
$metadataFactory, |
30
|
80 |
|
$propertyAccessor, |
31
|
80 |
|
$serializer |
32
|
|
|
); |
33
|
80 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
6 |
|
protected function listEntities(int $page = 1, int $perPage = 50) |
39
|
|
|
{ |
40
|
6 |
|
return $this->getService()->findAllPaginated($page, $perPage); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
56 |
|
protected function fetchEntity($id) |
47
|
|
|
{ |
48
|
56 |
|
$entity = $this->getService()->find($id); |
49
|
56 |
|
if (null === $entity) { |
50
|
2 |
|
throw new NotFoundHttpException(); |
51
|
|
|
} |
52
|
|
|
|
53
|
56 |
|
return $entity; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
10 |
|
protected function createEntity($entity) |
60
|
|
|
{ |
61
|
10 |
|
return $this->getService()->create($entity); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
10 |
|
protected function updateEntity($entity) |
68
|
|
|
{ |
69
|
10 |
|
return $this->getService()->update($entity); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
2 |
|
protected function removeEntity($entity) |
76
|
|
|
{ |
77
|
2 |
|
$this->getService()->remove($entity); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
6 |
|
protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50) |
84
|
|
|
{ |
85
|
6 |
|
return $this->getService()->findAssociationPaginated($entity, $subresource, $page, $perPage); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
2 |
|
protected function buildAssociation($parent, string $subresource, $entity) |
92
|
|
|
{ |
93
|
2 |
|
return $this->getService()->createAssociation($parent, $subresource, $entity); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
2 |
|
protected function createAssociation($associatedEntity) |
100
|
|
|
{ |
101
|
2 |
|
return $this->getService()->create($associatedEntity); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
12 |
|
protected function addAssociation($parent, string $subresource, $subId) |
108
|
|
|
{ |
109
|
12 |
|
$this->getService()->addAssociation($parent, $subresource, $subId); |
110
|
12 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
12 |
|
protected function removeAssociation($parent, string $subresource, $subId = null) |
116
|
|
|
{ |
117
|
12 |
|
$this->getService()->removeAssociation($parent, $subresource, $subId); |
118
|
12 |
|
} |
119
|
|
|
|
120
|
|
|
protected function getServiceId() |
121
|
|
|
{ |
122
|
|
|
return $this->getCurrentRequest()->attributes->get('_service'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return CrudServiceInterface |
127
|
|
|
*/ |
128
|
|
|
abstract protected function getService(); |
129
|
|
|
} |
130
|
|
|
|