Total Complexity | 5 |
Total Lines | 37 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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 |
||
59 | ); |
||
60 | } |
||
61 | } |
||
62 |