Completed
Push — master ( 56c59d...5628a9 )
by Philip
07:18
created

DoctrineRestResourceController::getPropertyAccessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\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
        RestRequestParserInterface $requestParser,
26
        Normalizer $normalizer,
27
        ValidatorInterface $validator,
28
        RequestStack $requestStack,
29
        MetadataFactoryInterface $metadataFactory,
30
        PropertyAccessorInterface $propertyAccessor,
31
        EntityManagerInterface $entityManager
32
    ) {
33 80
        parent::__construct(
34 80
            $requestParser,
35 80
            $normalizer,
36 80
            $validator,
37 80
            $requestStack,
38 80
            $metadataFactory,
39 80
            $propertyAccessor
40
        );
41 80
        $this->entityManager = $entityManager;
42 80
    }
43
44
    /**
45
     * @return CrudServiceInterface
46
     */
47 72
    protected function getService(): CrudServiceInterface
48
    {
49 72
        $entityClass = $this->getEntityClass();
50 72
        if (null === $entityClass) {
51
            throw new \RuntimeException('No service or entity class given');
52
        }
53 72
        $entityManager = $this->getEntityManager();
54 72
        $repository = $entityManager->getRepository($entityClass);
55 72
        if (!$repository instanceof CrudServiceInterface) {
56
            throw new \RuntimeException(
57
                'Your Entity Repository needs to be an instance of ' . CrudServiceInterface::class . '.'
58
            );
59
        }
60
61 72
        return $repository;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 72
    protected function getEntityManager()
68
    {
69 72
        return $this->entityManager;
70
    }
71
}
72