DoctrineRestResourceController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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