Completed
Push — master ( 3400f2...a755ea )
by Gerrit
10:47
created

DefaultControllerHelper::flushORM()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Controllers;
14
15
use Throwable;
16
use Twig_Environment;
17
use Psr\Log\LoggerInterface;
18
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Doctrine\Common\Persistence\ObjectRepository;
21
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
22
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
23
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24
use Symfony\Component\HttpFoundation\RedirectResponse;
25
use Symfony\Component\HttpFoundation\Session\Session;
26
use Symfony\Component\HttpFoundation\Response;
27
28
/**
29
 * The default implementation of the controller-helper.
30
 */
31
final class DefaultControllerHelper implements ControllerHelperInterface
32
{
33
34
    /**
35
     * @var EntityManagerInterface
36
     */
37
    private $entityManager;
38
39
    /**
40
     * @var Twig_Environment
41
     */
42
    private $twig;
43
44
    /**
45
     * @var AuthorizationCheckerInterface
46
     */
47
    private $authorization;
48
49
    /**
50
     * @var UrlGeneratorInterface
51
     */
52
    private $urlGenerator;
53
54
    /**
55
     * @var Session
56
     */
57
    private $session;
58
59
    /**
60
     * @var LoggerInterface
61
     */
62
    private $logger;
63
64 8
    public function __construct(
65
        EntityManagerInterface $entityManager,
66
        Twig_Environment $twig,
67
        AuthorizationCheckerInterface $authorization,
68
        UrlGeneratorInterface $urlGenerator,
69
        Session $session,
70
        LoggerInterface $logger
71
    ) {
72 8
        $this->entityManager = $entityManager;
73 8
        $this->twig = $twig;
74 8
        $this->authorization = $authorization;
75 8
        $this->urlGenerator = $urlGenerator;
76 8
        $this->session = $session;
77 8
        $this->logger = $logger;
78 8
    }
79
80
    public function renderTemplate(string $templatePath, array $arguments = array()): Response
81
    {
82
        return new Response($this->twig->render($templatePath, $arguments));
83
    }
84
85
    public function findEntity(string $entityClass, string $id)
86
    {
87
        return $this->entityManager->find($entityClass, $id);
88
    }
89
90
    public function findEntities(string $entityClass, array $criteria): array
91
    {
92
        /** @var ObjectRepository $repository */
93
        $repository = $this->entityManager->getRepository($entityClass);
94
95
        return $repository->findBy($criteria);
96
    }
97
98 1
    public function persistEntity($entity): void
99
    {
100 1
        $this->entityManager->persist($entity);
101 1
    }
102
103 1
    public function removeEntity($entity): void
104
    {
105 1
        $this->entityManager->remove($entity);
106 1
    }
107
108 1
    public function flushORM(): void
109
    {
110 1
        $this->entityManager->flush();
111 1
    }
112
113 1
    public function handleException(Throwable $exception): void
114
    {
115 1
        $this->logger->log("error", (string)$exception);
116 1
    }
117
118 1
    public function addFlashMessage(string $message, string $type = "default"): void
119
    {
120 1
        $this->session->getFlashBag()->add($type, $message);
121 1
    }
122
123 2
    public function redirectToRoute(string $route, array $parameters = array(), int $status = 301): Response
124
    {
125
        /** @var string $url */
126 2
        $url = $this->urlGenerator->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL);
127
128 2
        return new RedirectResponse($url, $status);
129
    }
130
131 1
    public function denyAccessUnlessGranted(string $attribute, $subject): void
132
    {
133 1
        if (!$this->authorization->isGranted($attribute, $subject)) {
134 1
            $exception = new AccessDeniedException('Access Denied.');
135 1
            $exception->setSubject($subject);
136 1
            $exception->setAttributes($attribute);
137
138 1
            throw $exception;
139
        }
140
    }
141
142
}
143