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\RestBundle\Service\RestRequestParserInterface; |
9
|
|
|
use Dontdrinkandroot\Service\CrudServiceInterface; |
10
|
|
|
use Metadata\MetadataFactoryInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
13
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Philip Washington Sorst <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class DoctrineRestResourceController extends AbstractCrudServiceRestResourceController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EntityManagerInterface |
22
|
|
|
*/ |
23
|
|
|
private $entityManager; |
24
|
|
|
|
25
|
80 |
|
public function __construct( |
26
|
|
|
RestRequestParserInterface $requestParser, |
27
|
|
|
Normalizer $normalizer, |
28
|
|
|
ValidatorInterface $validator, |
29
|
|
|
RequestStack $requestStack, |
30
|
|
|
RestMetadataFactory $metadataFactory, |
31
|
|
|
PropertyAccessorInterface $propertyAccessor, |
32
|
|
|
EntityManagerInterface $entityManager |
33
|
|
|
) { |
34
|
80 |
|
parent::__construct( |
35
|
80 |
|
$requestParser, |
36
|
80 |
|
$normalizer, |
37
|
80 |
|
$validator, |
38
|
80 |
|
$requestStack, |
39
|
80 |
|
$metadataFactory, |
40
|
80 |
|
$propertyAccessor |
41
|
|
|
); |
42
|
80 |
|
$this->entityManager = $entityManager; |
43
|
80 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return CrudServiceInterface |
47
|
|
|
*/ |
48
|
72 |
|
protected function getService(): CrudServiceInterface |
49
|
|
|
{ |
50
|
72 |
|
$entityClass = $this->getEntityClass(); |
51
|
72 |
|
if (null === $entityClass) { |
52
|
|
|
throw new \RuntimeException('No service or entity class given'); |
53
|
|
|
} |
54
|
72 |
|
$entityManager = $this->getEntityManager(); |
55
|
72 |
|
$repository = $entityManager->getRepository($entityClass); |
56
|
72 |
|
if (!$repository instanceof CrudServiceInterface) { |
57
|
|
|
throw new \RuntimeException( |
58
|
|
|
'Your Entity Repository needs to be an instance of ' . CrudServiceInterface::class . '.' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
72 |
|
return $repository; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
72 |
|
protected function getEntityManager() |
69
|
|
|
{ |
70
|
72 |
|
return $this->entityManager; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|