Passed
Push — feature/configure_company ( 5f97ab...38f251 )
by Laurent
03:30 queued 10s
created

PostCompanyController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 36
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 27 2
A __construct() 0 3 1
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 Infrastructure\Administration\Company\Controller;
15
16
use Domain\Administration\Company\Command\CreateCompany;
17
use Domain\Common\Model\VO\EmailField;
18
use Domain\Common\Model\VO\NameField;
19
use Domain\Common\Model\VO\PhoneField;
20
use Infrastructure\Common\MessengerCommandBus;
21
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
22
use Symfony\Component\HttpFoundation\JsonResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
25
class PostCompanyController extends AbstractController
26
{
27
    private MessengerCommandBus $commandBus;
28
29
    public function __construct(MessengerCommandBus $commandBus)
30
    {
31
        $this->commandBus = $commandBus;
32
    }
33
34
    public function __invoke(Request $request): JsonResponse
35
    {
36
        $company = $request->request->get('create_company');
37
38
        if (empty($company['name'])) {
39
            return new JsonResponse([
40
                'error' => 'Company \'name\' cannot be empty.',
41
            ], JsonResponse::HTTP_BAD_REQUEST);
42
        }
43
44
        $command = new CreateCompany(
45
            NameField::fromString($company['name']),
46
            $company['address'],
47
            $company['zipCode'],
48
            $company['town'],
49
            $company['country'],
50
            PhoneField::fromString($company['phone']),
51
            PhoneField::fromString($company['facsimile']),
52
            EmailField::fromString($company['email']),
53
            $company['contact'],
54
            PhoneField::fromString($company['gsm'])
55
        );
56
        $this->commandBus->dispatch($command);
57
58
        return new JsonResponse([
59
            'status' => 'Company created started!',
60
        ], JsonResponse::HTTP_ACCEPTED);
61
    }
62
}
63