Passed
Push — develop ( 0d66a7...37c69c )
by Laurent
05:34 queued 02:39
created

PostSettingsController::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 29
rs 9.6666
c 1
b 0
f 0
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\Settings\Controller;
15
16
use Administration\Application\Settings\ReadModel\Settings;
17
use Administration\Domain\Settings\Command\ConfigureSettings;
18
use Administration\Domain\Settings\Model\VO\Currency;
19
use Administration\Domain\Settings\Model\VO\Locale;
20
use Administration\Infrastructure\Settings\Form\Type\SettingsType;
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 PostSettingsController 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): Response
37
    {
38
        $settings = $request->request->get('settings');
39
        $command = new ConfigureSettings(
40
            Locale::fromString($settings['locale']),
41
            Currency::fromString($settings['currency'])
42
        );
43
44
        try {
45
            $this->commandBus->dispatch($command);
46
        } catch (\RuntimeException $exception) {
47
            $newSettings = new Settings(
48
                $command->currency()->getValue(),
49
                $command->locale()->getValue()
50
            );
51
            $form = $this->createForm(SettingsType::class, $newSettings, [
52
                'action' => $this->generateUrl('admin_settings_configure'),
53
            ]);
54
            $this->addFlash('errors', $exception->getMessage());
55
56
            return $this->render('Administration/Settings/new.html.twig', [
57
                'form' => $form->createView(),
58
                'errors' => $exception,
59
            ]);
60
        }
61
62
        $this->addFlash('success', 'Settings created started!');
63
64
        return new RedirectResponse($this->generateUrl('admin_index'));
65
    }
66
}
67