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