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

EditCompanyHandler::updateCompany()   B

Complexity

Conditions 11
Paths 128

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 128
nop 2
dl 0
loc 34
rs 7.0833
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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()) {
0 ignored issues
show
introduced by
The condition $company->name() !== $command->name() is always true.
Loading history...
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()) {
0 ignored issues
show
introduced by
The condition $company->phone() !== $command->phone() is always true.
Loading history...
60
            $company->changePhoneNumber($command->phone());
61
        }
62
        if ($company->facsimile() !== $command->facsimile()) {
0 ignored issues
show
introduced by
The condition $company->facsimile() !== $command->facsimile() is always true.
Loading history...
63
            $company->changeFacsimileNumber($command->facsimile());
64
        }
65
        if ($company->email() !== $command->email()) {
0 ignored issues
show
introduced by
The condition $company->email() !== $command->email() is always true.
Loading history...
66
            $company->rewriteEmail($command->email());
67
        }
68
        if ($company->contact() !== $command->contact()) {
69
            $company->renameContact($command->contact());
70
        }
71
        if ($company->cellPhone() !== $command->cellPhone()) {
0 ignored issues
show
introduced by
The condition $company->cellPhone() !== $command->cellPhone() is always true.
Loading history...
72
            $company->changeCellphoneNumber($command->cellPhone());
73
        }
74
75
        return $company;
76
    }
77
}
78