Completed
Push — master ( 658cec...20b116 )
by Philip
06:38
created

DoctrineRestResourceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 53
ccs 18
cts 21
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 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\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