1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\RestMetadataFactory; |
7
|
|
|
use Dontdrinkandroot\RestBundle\Service\Normalizer; |
8
|
|
|
use Dontdrinkandroot\Service\CrudServiceInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
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
|
|
|
class DoctrineRestResourceController extends AbstractCrudServiceRestResourceController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityManagerInterface |
21
|
|
|
*/ |
22
|
|
|
private $entityManager; |
23
|
|
|
|
24
|
80 |
|
public function __construct( |
25
|
|
|
ValidatorInterface $validator, |
26
|
|
|
RequestStack $requestStack, |
27
|
|
|
RestMetadataFactory $metadataFactory, |
28
|
|
|
PropertyAccessorInterface $propertyAccessor, |
29
|
|
|
EntityManagerInterface $entityManager, |
30
|
|
|
SerializerInterface $serializer |
31
|
|
|
) { |
32
|
80 |
|
parent::__construct( |
33
|
80 |
|
$validator, |
34
|
80 |
|
$requestStack, |
35
|
80 |
|
$metadataFactory, |
36
|
80 |
|
$propertyAccessor, |
37
|
80 |
|
$serializer |
38
|
|
|
); |
39
|
80 |
|
$this->entityManager = $entityManager; |
40
|
80 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return CrudServiceInterface |
44
|
|
|
*/ |
45
|
72 |
|
protected function getService(): CrudServiceInterface |
46
|
|
|
{ |
47
|
72 |
|
$entityClass = $this->getEntityClass(); |
48
|
72 |
|
if (null === $entityClass) { |
49
|
|
|
throw new \RuntimeException('No service or entity class given'); |
50
|
|
|
} |
51
|
72 |
|
$entityManager = $this->getEntityManager(); |
52
|
72 |
|
$repository = $entityManager->getRepository($entityClass); |
53
|
72 |
|
if (!$repository instanceof CrudServiceInterface) { |
54
|
|
|
throw new \RuntimeException( |
55
|
|
|
'Your Entity Repository needs to be an instance of ' . CrudServiceInterface::class . '.' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
72 |
|
return $repository; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
72 |
|
protected function getEntityManager() |
66
|
|
|
{ |
67
|
72 |
|
return $this->entityManager; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|