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

DoctrineRestResourceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 55
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A getService() 0 16 3
A getEntityManager() 0 4 1
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