Completed
Push — standalone ( a782f9...7d7a9d )
by Philip
04:00
created

getEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Controller;
4
5
use Dontdrinkandroot\Service\CrudServiceInterface;
6
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
7
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
8
9
class ContainerAwareRestResourceController extends AbstractRestResourceController implements ContainerAwareInterface
10
{
11
    use ContainerAwareTrait;
12
13
    /**
14
     * @return CrudServiceInterface
15
     */
16 72
    protected function getService(): CrudServiceInterface
17
    {
18 72
        $serviceId = $this->getServiceId();
19 72
        if (null === $serviceId) {
20 70
            $entityClass = $this->getEntityClass();
21 70
            if (null === $entityClass) {
22
                throw new \RuntimeException('No service or entity class given');
23
            }
24 70
            $entityManager = $this->getEntityManager();
25 70
            $repository = $entityManager->getRepository($entityClass);
26 70
            if (!$repository instanceof CrudServiceInterface) {
27
                throw new \RuntimeException(
28
                    'Your Entity Repository needs to be an instance of ' . CrudServiceInterface::class . '.'
29
                );
30
            }
31
32 70
            return $repository;
33
        } else {
34
            /** @var CrudServiceInterface $service */
35 2
            $service = $this->container->get($serviceId);
36
37 2
            return $service;
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 62
    protected function getNormalizer()
45
    {
46 62
        return $this->container->get('ddr_rest.normalizer');
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 24
    protected function getValidator()
53
    {
54 24
        return $this->container->get('validator');
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 24
    protected function getRequestParser()
61
    {
62 24
        return $this->container->get('ddr.rest.parser.request');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 80
    protected function getRequestStack()
69
    {
70 80
        return $this->container->get('request_stack');
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 80
    protected function getMetadataFactory()
77
    {
78 80
        return $this->container->get('ddr_rest.metadata.factory');
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function getPropertyAccessor()
85
    {
86
        return $this->container->get('property_accessor');
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 46
    protected function getAuthorizationChecker()
93
    {
94 46
        return $this->container->get('security.authorization_checker');
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 70
    protected function getEntityManager()
101
    {
102 70
        return $this->container->get('doctrine.orm.entity_manager');
103
    }
104
}
105