AbstractController::getDoctrine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Dominikzogg\EnergyCalculator\Controller;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Dominikzogg\EnergyCalculator\Entity\User;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
14
15
abstract class AbstractController
16
{
17
    /**
18
     * @var ManagerRegistry
19
     */
20
    protected $doctrine;
21
22
    /**
23
     * @var \Twig_Environment
24
     */
25
    protected $twig;
26
27
    /**
28
     * @var FormFactoryInterface
29
     */
30
    protected $formFactory;
31
32
    /**
33
     * @var AuthorizationCheckerInterface
34
     */
35
    protected $authorizationChecker;
36
37
    /**
38
     * @var TokenStorageInterface
39
     */
40
    protected $tokenStorage;
41
42
    /**
43
     * @return User
44
     * @throws \Exception
45
     */
46
    protected function getUser()
47
    {
48
        if (null === $token = $this->tokenStorage->getToken()) {
49
            throw new \Exception("No user token!");
50
        }
51
52
        $user = $token->getUser();
53
54
        if(!$user instanceof User) {
55
            throw new \Exception("Invalid user token!");
56
        }
57
58
        $user = $this->doctrine->getManager()->getRepository(get_class($user))->find($user->getId());
59
60
        if(null === $user) {
61
            throw new \Exception("User not found in database!");
62
        }
63
64
        return $user;
65
    }
66
67
    /**
68
     * @return ManagerRegistry
69
     */
70
    protected function getDoctrine()
71
    {
72
        return $this->doctrine;
73
    }
74
75
    /**
76
     * @param  string             $class
77
     * @return ObjectManager|null
78
     */
79
    protected function getManagerForClass($class)
80
    {
81
        return $this->doctrine->getManagerForClass($class);
82
    }
83
84
    /**
85
     * @param  string           $class
86
     * @return ObjectRepository
87
     */
88
    protected function getRepositoryForClass($class)
89
    {
90
        return $this->getManagerForClass($class)->getRepository($class);
91
    }
92
93
    /**
94
     * @param $view
95
     * @param  array    $parameters
96
     * @return Response
97
     */
98
    protected function render($view, array $parameters = array())
99
    {
100
        return new Response($this->twig->render($view, $parameters));
101
    }
102
103
    /**
104
     * @param  string $type
105
     * @param  null   $data
106
     * @param  array  $options
107
     * @return FormInterface
108
     */
109
    protected function createForm($type = 'form', $data = null, array $options = array())
110
    {
111
        return $this->formFactory->create($type, $data, $options);
112
    }
113
}