This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProcessClientController.php
executable file
·224 lines (194 loc) · 7.1 KB
/
ProcessClientController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
* This file is part of itk-dev/kontrolgruppen.
*
* (c) 2019–2021 ITK Development
*
* This source file is subject to the MIT license.
*/
namespace Kontrolgruppen\CoreBundle\Controller;
use Doctrine\ORM\EntityManagerInterface;
use http\Exception\RuntimeException;
use Kontrolgruppen\CoreBundle\CPR\CprException;
use Kontrolgruppen\CoreBundle\CPR\CprServiceInterface;
use Kontrolgruppen\CoreBundle\Entity\AbstractProcessClient;
use Kontrolgruppen\CoreBundle\Entity\Process;
use Kontrolgruppen\CoreBundle\Entity\ProcessClientCompany;
use Kontrolgruppen\CoreBundle\Entity\ProcessClientPerson;
use Kontrolgruppen\CoreBundle\Form\ProcessClientCompanyType;
use Kontrolgruppen\CoreBundle\Form\ProcessClientPersonType;
use Kontrolgruppen\CoreBundle\Service\MenuService;
use Kontrolgruppen\CoreBundle\Service\ProcessClientManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/process/{process}/client")
*/
class ProcessClientController extends BaseController
{
/**
* @var ProcessClientManager
*/
private $processClientManager;
/**
* @var LoggerInterface
*/
private $logger;
/**
* ProcessClientController constructor.
*
* @param RequestStack $requestStack
* @param MenuService $menuService
* @param CprServiceInterface $processClientManager
* @param LoggerInterface $logger
* @param EntityManagerInterface $em
*/
public function __construct(RequestStack $requestStack, MenuService $menuService, ProcessClientManager $processClientManager, LoggerInterface $logger, EntityManagerInterface $em)
{
parent::__construct($requestStack, $menuService, $em);
$this->processClientManager = $processClientManager;
$this->logger = $logger;
}
/**
* @Route("/", name="client_show", methods={"GET","POST"})
*
* @param Request $request
* @param Process $process
*
* @return Response
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function show(Request $request, Process $process): Response
{
$client = $process->getProcessClient();
if (null === $client) {
throw new \RuntimeException('@todo create client');
}
$changeProcessStatusForm = $this->createChangeProcessStatusForm($process);
$this->handleChangeProcessStatusForm($request, $changeProcessStatusForm);
$newInfoAvailable = $this->isGranted('edit', $process) && $this->isNewClientInfoAvailable($client);
$view = $this->getView($client, 'show');
return $this->render($view, [
'menuItems' => $this->menuService->getProcessMenu($request->getPathInfo(), $process),
'client' => $client,
'canEdit' => $this->isGranted('edit', $process) && null === $process->getCompletedAt(),
'changeProcessStatusForm' => $changeProcessStatusForm->createView(),
'process' => $process,
'newClientInfoAvailable' => $newInfoAvailable,
]);
}
/**
* @Route("/edit", name="client_edit", methods={"GET","POST"})
*
* @param Request $request
* @param Process $process
*
* @return Response
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function edit(Request $request, Process $process): Response
{
$this->denyAccessUnlessGranted('edit', $process);
// Redirect to show if process is completed.
if (null !== $process->getCompletedAt()) {
return $this->redirectToRoute('client_show', [
'process' => $process->getId(),
]);
}
$client = $process->getProcessClient();
$form = $this->createClientForm($client);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
return $this->redirectToRoute('client_show', [
'process' => $process->getId(),
]);
}
$view = $this->getView($client, 'edit');
return $this->render($view, [
'menuItems' => $this->menuService->getProcessMenu($request->getPathInfo(), $process),
'canEdit' => $this->isGranted('edit', $process) && null === $process->getCompletedAt(),
'client' => $client,
'form' => $form->createView(),
'process' => $process,
]);
}
/**
* @Route("/update", name="client_update", methods={"GET"})
*
* @param Request $request
* @param Process $process
* @param TranslatorInterface $translator
*
* @return Response
*/
public function update(Request $request, Process $process, TranslatorInterface $translator): Response
{
$this->denyAccessUnlessGranted('edit', $process);
$client = $process->getProcessClient();
try {
$client = $this->processClientManager->populateClient($client);
$this->addFlash('success', $translator->trans('client.show.client_updated'));
} catch (CprException $e) {
$this->addFlash('danger', $translator->trans('client.show.client_not_updated'));
$this->logger->error($e);
}
$this->em->persist($client);
$this->em->flush();
return $this->redirectToRoute('client_show', ['process' => $process]);
}
/**
* Check if new info is available for a client.
*
* @param AbstractProcessClient $client
*
* @return bool True if new info is available
*/
private function isNewClientInfoAvailable(AbstractProcessClient $client): bool
{
try {
return $this->processClientManager->isNewClientInfoAvailable($client);
} catch (CprException $e) {
$this->logger->error($e);
}
return false;
}
/**
* Get view for a client action.
*
* @param AbstractProcessClient $client
* @param string $action
*
* @return string The view
*/
private function getView(AbstractProcessClient $client, string $action): string
{
return '@KontrolgruppenCore/client/'.$client->getType().'/'.$action.'.html.twig';
}
/**
* Create client form.
*
* @param AbstractProcessClient $client
*
* @return FormInterface The client form
*/
private function createClientForm(AbstractProcessClient $client): FormInterface
{
if ($client instanceof ProcessClientCompany) {
return $this->createForm(ProcessClientCompanyType::class, $client);
}
if ($client instanceof ProcessClientPerson) {
return $this->createForm(ProcessClientPersonType::class, $client);
}
throw new RuntimeException(sprintf('Unknown client type: %s', $client::class));
}
}