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