1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Service\Normalizer; |
7
|
|
|
use Dontdrinkandroot\RestBundle\Service\RestRequestParserInterface; |
8
|
|
|
use Dontdrinkandroot\Service\CrudServiceInterface; |
9
|
|
|
use Metadata\MetadataFactoryInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
13
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Philip Washington Sorst <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class DoctrineRestResourceController extends CrudServiceRestResourceController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Normalizer |
22
|
|
|
*/ |
23
|
|
|
private $normalizer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ValidatorInterface |
27
|
|
|
*/ |
28
|
|
|
private $validator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RestRequestParserInterface |
32
|
|
|
*/ |
33
|
|
|
private $requestParser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RequestStack |
37
|
|
|
*/ |
38
|
|
|
private $requestStack; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MetadataFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $metadataFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var PropertyAccessorInterface |
47
|
|
|
*/ |
48
|
|
|
private $propertyAccessor; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var AuthorizationCheckerInterface |
52
|
|
|
*/ |
53
|
|
|
private $authorizationChecker; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var EntityManagerInterface |
57
|
|
|
*/ |
58
|
|
|
private $entityManager; |
59
|
|
|
|
60
|
80 |
|
public function __construct( |
61
|
|
|
EntityManagerInterface $entityManager, |
62
|
|
|
RestRequestParserInterface $requestParser, |
63
|
|
|
Normalizer $normalizer, |
64
|
|
|
ValidatorInterface $validator, |
65
|
|
|
RequestStack $requestStack, |
66
|
|
|
MetadataFactoryInterface $metadataFactory, |
67
|
|
|
PropertyAccessorInterface $propertyAccessor |
68
|
|
|
) { |
69
|
80 |
|
$this->normalizer = $normalizer; |
70
|
80 |
|
$this->validator = $validator; |
71
|
80 |
|
$this->requestParser = $requestParser; |
72
|
80 |
|
$this->requestStack = $requestStack; |
73
|
80 |
|
$this->metadataFactory = $metadataFactory; |
74
|
80 |
|
$this->propertyAccessor = $propertyAccessor; |
75
|
80 |
|
$this->entityManager = $entityManager; |
76
|
80 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return CrudServiceInterface |
80
|
|
|
*/ |
81
|
72 |
|
protected function getService(): CrudServiceInterface |
82
|
|
|
{ |
83
|
72 |
|
$entityClass = $this->getEntityClass(); |
84
|
72 |
|
if (null === $entityClass) { |
85
|
|
|
throw new \RuntimeException('No service or entity class given'); |
86
|
|
|
} |
87
|
72 |
|
$entityManager = $this->getEntityManager(); |
88
|
72 |
|
$repository = $entityManager->getRepository($entityClass); |
89
|
72 |
|
if (!$repository instanceof CrudServiceInterface) { |
90
|
|
|
throw new \RuntimeException( |
91
|
|
|
'Your Entity Repository needs to be an instance of ' . CrudServiceInterface::class . '.' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
72 |
|
return $repository; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
62 |
|
protected function getNormalizer() |
102
|
|
|
{ |
103
|
62 |
|
return $this->normalizer; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
24 |
|
protected function getValidator() |
110
|
|
|
{ |
111
|
24 |
|
return $this->validator; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
24 |
|
protected function getRequestParser() |
118
|
|
|
{ |
119
|
24 |
|
return $this->requestParser; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
80 |
|
protected function getRequestStack() |
126
|
|
|
{ |
127
|
80 |
|
return $this->requestStack; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
80 |
|
protected function getMetadataFactory() |
134
|
|
|
{ |
135
|
80 |
|
return $this->metadataFactory; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
protected function getPropertyAccessor() |
142
|
|
|
{ |
143
|
|
|
return $this->propertyAccessor; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
46 |
|
protected function getAuthorizationChecker(): ?AuthorizationCheckerInterface |
150
|
|
|
{ |
151
|
46 |
|
return $this->authorizationChecker; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
156
|
|
|
*/ |
157
|
76 |
|
public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): void |
158
|
|
|
{ |
159
|
76 |
|
$this->authorizationChecker = $authorizationChecker; |
160
|
76 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
72 |
|
protected function getEntityManager() |
166
|
|
|
{ |
167
|
72 |
|
return $this->entityManager; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|