Passed
Push — develop ( 37c69c...faa16c )
by Laurent
03:14 queued 37s
created

PutUserController::__invoke()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 28
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Infrastructure\User\Controller;
15
16
use Administration\Domain\User\Command\EditUser;
17
use Administration\Domain\User\Model\VO\UserUuid;
18
use Administration\Infrastructure\User\Form\UserType;
19
use Core\Domain\Common\Model\VO\EmailField;
20
use Core\Domain\Common\Model\VO\NameField;
21
use Core\Infrastructure\Common\MessengerCommandBus;
22
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
23
use Symfony\Component\HttpFoundation\RedirectResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
27
class PutUserController extends AbstractController
28
{
29
    private MessengerCommandBus $commandBus;
30
31
    public function __construct(MessengerCommandBus $commandBus)
32
    {
33
        $this->commandBus = $commandBus;
34
    }
35
36
    public function __invoke(Request $request, string $uuid): Response
37
    {
38
        $user = $request->get('user');
39
40
        try {
41
            $command = new EditUser(
42
                UserUuid::fromString($uuid),
43
                NameField::fromString($user['username']),
44
                EmailField::fromString($user['email']),
45
                $user['password']['first'],
46
                $user['roles']
47
            );
48
            $this->commandBus->dispatch($command);
49
        } catch (\DomainException $exception) {
50
            $form = $this->createForm(UserType::class, $user, [
51
                'action' => $this->generateUrl('admin_user_edit', ['uuid' => $uuid]),
52
                'method' => 'PUT',
53
            ]);
54
            $this->addFlash('error', $exception->getMessage());
55
56
            return $this->render('Administration/User/edit.html.twig', [
57
                'form' => $form->createView(),
58
            ]);
59
        }
60
61
        $this->addFlash('success', 'User updated started!');
62
63
        return new RedirectResponse($this->generateUrl('admin_user_index'));
64
    }
65
}
66