1 | <?php |
||
35 | */ |
||
36 | final class DefaultControllerHelper implements ControllerHelperInterface |
||
37 | { |
||
38 | |||
39 | private EntityManagerInterface $entityManager; |
||
|
|||
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 |