Test Setup Failed
Push — master ( e2ccdd...da0315 )
by Gerrit
09:14
created

DefaultControllerHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 7
cbo 12
dl 0
loc 141
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0
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\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
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
use Symfony\Component\HttpFoundation\RequestStack;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Contracts\EventDispatcher\Event;
31
use stdClass;
32
33
/**
34
 * The default implementation of the controller-helper.
35
 */
36
final class DefaultControllerHelper implements ControllerHelperInterface
37
{
38
39
    private EntityManagerInterface $entityManager;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
40
41
    private Environment $twig;
42
43
    private AuthorizationCheckerInterface $authorization;
44
45
    private UrlGeneratorInterface $urlGenerator;
46
47
    private Session $session;
48
49
    private LoggerInterface $logger;
50
51
    private EventDispatcherInterface $eventDispatcher;
52
53
    private RequestStack $requestStack;
54
55
    public function __construct(
56
        EntityManagerInterface $entityManager,
57
        Environment $twig,
58
        AuthorizationCheckerInterface $authorization,
59
        UrlGeneratorInterface $urlGenerator,
60
        Session $session,
61
        LoggerInterface $logger,
62
        EventDispatcherInterface $eventDispatcher,
63
        RequestStack $requestStack
64
    ) {
65
        $this->entityManager = $entityManager;
66
        $this->twig = $twig;
67
        $this->authorization = $authorization;
68
        $this->urlGenerator = $urlGenerator;
69
        $this->session = $session;
70
        $this->logger = $logger;
71
        $this->eventDispatcher = $eventDispatcher;
72
        $this->requestStack = $requestStack;
73
    }
74
75
    public function renderTemplate(string $templatePath, array $arguments = array()): Response
76
    {
77
        return new Response($this->twig->render($templatePath, $arguments));
78 15
    }
79
80
    public function findEntity(string $entityClass, string $id)
81
    {
82
        return $this->entityManager->find($entityClass, $id);
83
    }
84
85
    public function findEntities(string $entityClass, array $criteria): array
86
    {
87
        /** @var ObjectRepository $repository */
88 15
        $repository = $this->entityManager->getRepository($entityClass);
89 15
90 15
        return $repository->findBy($criteria);
91 15
    }
92 15
93 15
    public function persistEntity($entity): void
94 15
    {
95 15
        $this->entityManager->persist($entity);
96 15
    }
97
98 1
    public function removeEntity($entity): void
99
    {
100 1
        $this->entityManager->remove($entity);
101
    }
102
103 1
    public function flushORM(): void
104
    {
105 1
        $this->entityManager->flush();
106
    }
107
108 1
    public function handleException(Throwable $exception): void
109
    {
110
        $this->logger->log("error", (string)$exception);
111 1
    }
112
113 1
    public function addFlashMessage(string $message, string $type = "default"): void
114
    {
115
        $this->session->getFlashBag()->add($type, $message);
116 1
    }
117
118 1
    public function redirectToRoute(string $route, array $parameters = array(), int $status = 301): Response
119 1
    {
120
        /** @var string $url */
121 1
        $url = $this->urlGenerator->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL);
122
123 1
        return new RedirectResponse($url, $status);
124 1
    }
125
126 1
    public function getRequestStack(): RequestStack
127
    {
128 1
        return $this->requestStack;
129 1
    }
130
131 1
    public function getCurrentRequest(): ?Request
132
    {
133 1
        return $this->requestStack->getCurrentRequest();
134 1
    }
135
136 1
    public function denyAccessUnlessGranted(string $attribute, $subject): void
137
    {
138 1
        if (!$this->authorization->isGranted($attribute, $subject)) {
139 1
            $exception = new AccessDeniedException('Access Denied.');
140
            $exception->setSubject($subject);
141 2
            $exception->setAttributes($attribute);
142
143
            throw $exception;
144 2
        }
145
    }
146 2
147
    public function dispatchEvent(string $eventName, object $event = null): object
148
    {
149 1
        return $this->eventDispatcher->dispatch($event ?? new stdClass(), $eventName);
150
    }
151 1
152
}
153