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\EditCompany; |
17
|
|
|
use Administration\Domain\Company\Model\Company; |
18
|
|
|
use Administration\Domain\Protocol\Repository\CompanyRepositoryProtocol; |
19
|
|
|
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol; |
20
|
|
|
|
21
|
|
|
final class EditCompanyHandler implements CommandHandlerProtocol |
22
|
|
|
{ |
23
|
|
|
private CompanyRepositoryProtocol $repository; |
24
|
|
|
|
25
|
|
|
public function __construct(CompanyRepositoryProtocol $repository) |
26
|
|
|
{ |
27
|
|
|
$this->repository = $repository; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function __invoke(EditCompany $command): void |
31
|
|
|
{ |
32
|
|
|
$companyToUpdate = $this->repository->findOneByUuid($command->uuid()); |
33
|
|
|
|
34
|
|
|
if (null === $companyToUpdate) { |
35
|
|
|
throw new \DomainException('Company provided does not exist !'); |
36
|
|
|
} |
37
|
|
|
$company = $this->updateCompany($command, $companyToUpdate); |
38
|
|
|
|
39
|
|
|
$this->repository->add($company); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function updateCompany(EditCompany $command, Company $company): Company |
43
|
|
|
{ |
44
|
|
|
if ($company->name() !== $command->name()) { |
|
|
|
|
45
|
|
|
$company->renameCompany($command->name()); |
46
|
|
|
} |
47
|
|
|
if (($company->address() !== $command->address()) |
48
|
|
|
|| ($company->zipCode() !== $command->zipCode()) |
49
|
|
|
|| ($company->town() !== $command->town()) |
50
|
|
|
|| ($company->country() !== $command->country()) |
51
|
|
|
) { |
52
|
|
|
$company->rewriteAddress([ |
53
|
|
|
$command->address(), |
54
|
|
|
$command->zipCode(), |
55
|
|
|
$command->town(), |
56
|
|
|
$command->country(), |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
if ($company->phone() !== $command->phone()) { |
|
|
|
|
60
|
|
|
$company->changePhoneNumber($command->phone()); |
61
|
|
|
} |
62
|
|
|
if ($company->facsimile() !== $command->facsimile()) { |
|
|
|
|
63
|
|
|
$company->changeFacsimileNumber($command->facsimile()); |
64
|
|
|
} |
65
|
|
|
if ($company->email() !== $command->email()) { |
|
|
|
|
66
|
|
|
$company->rewriteEmail($command->email()); |
67
|
|
|
} |
68
|
|
|
if ($company->contact() !== $command->contact()) { |
69
|
|
|
$company->renameContact($command->contact()); |
70
|
|
|
} |
71
|
|
|
if ($company->cellPhone() !== $command->cellPhone()) { |
|
|
|
|
72
|
|
|
$company->changeCellphoneNumber($command->cellPhone()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $company; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|