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

CreateCompanyHandler::createCompany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.8666
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\Domain\Company\Handler;
15
16
use Administration\Domain\Company\Command\CreateCompany;
17
use Administration\Domain\Company\Model\Company;
18
use Administration\Domain\Protocol\Repository\CompanyRepositoryProtocol;
19
use Core\Domain\Common\Model\VO\ContactUuid;
20
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol;
21
22
final class CreateCompanyHandler implements CommandHandlerProtocol
23
{
24
    private CompanyRepositoryProtocol $repository;
25
26
    public function __construct(CompanyRepositoryProtocol $repository)
27
    {
28
        $this->repository = $repository;
29
    }
30
31
    public function __invoke(CreateCompany $command): void
32
    {
33
        if ($this->repository->companyExist()) {
34
            throw new \DomainException('A company is already create.');
35
        }
36
        if ($this->repository->existsWithName($command->name()->getValue())) {
37
            throw new \DomainException("Company with name: {$command->name()->getValue()} already exists.");
38
        }
39
40
        $company = $this->createCompany($command);
41
42
        $this->repository->add($company);
43
    }
44
45
    public function createCompany(CreateCompany $command): Company
46
    {
47
        return Company::create(
48
            ContactUuid::generate(),
49
            $command->name(),
50
            $command->address(),
51
            $command->zipCode(),
52
            $command->town(),
53
            $command->country(),
54
            $command->phone(),
55
            $command->facsimile(),
56
            $command->email(),
57
            $command->contact(),
58
            $command->cellPhone()
59
        );
60
    }
61
}
62