Passed
Push — develop ( 7d2e81...61e6f0 )
by Laurent
06:22 queued 03:53
created

PostCompanyController::__invoke()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 23
nc 3
nop 1
dl 0
loc 33
rs 9.552
c 0
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\Company\Controller;
15
16
use Administration\Domain\Company\Command\CreateCompany;
17
use Administration\Infrastructure\Company\Form\CompanyType;
18
use Core\Domain\Common\Model\VO\EmailField;
19
use Core\Domain\Common\Model\VO\NameField;
20
use Core\Domain\Common\Model\VO\PhoneField;
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 PostCompanyController 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
        $company = $request->request->get('company');
39
40
        try {
41
            $command = new CreateCompany(
42
                NameField::fromString($company['name']),
43
                $company['address'],
44
                $company['zipCode'],
45
                $company['town'],
46
                $company['country'],
47
                PhoneField::fromString($company['phone']),
48
                PhoneField::fromString($company['facsimile']),
49
                EmailField::fromString($company['email']),
50
                $company['contact'],
51
                PhoneField::fromString($company['cellphone'])
52
            );
53
            $this->commandBus->dispatch($command);
54
        } catch (\DomainException $exception) {
55
            $form = $this->createForm(CompanyType::class, $company, [
56
                'action' => $this->generateUrl('admin_company_create'),
57
            ]);
58
            $this->addFlash('error', $exception->getMessage());
59
60
            return $this->render('Administration/Company/new.html.twig', [
61
                'form' => $form->createView(),
62
                'errors' => $exception,
63
            ]);
64
        }
65
66
        $this->addFlash('success', 'Company created started!');
67
68
        return new RedirectResponse($this->generateUrl('admin_company_index'));
69
    }
70
}
71