Passed
Push — develop ( 78e2d5...9c7a10 )
by Laurent
02:42 queued 01:21
created

EditSupplierHandler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
F updateSupplier() 0 43 14
A __construct() 0 3 1
A __invoke() 0 11 2
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\Supplier\Handler;
15
16
use Administration\Domain\Protocol\Repository\SupplierRepositoryProtocol;
17
use Administration\Domain\Supplier\Command\EditSupplier;
18
use Administration\Domain\Supplier\Model\Supplier;
19
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol;
20
21
class EditSupplierHandler implements CommandHandlerProtocol
22
{
23
    private SupplierRepositoryProtocol $repository;
24
25
    public function __construct(SupplierRepositoryProtocol $repository)
26
    {
27
        $this->repository = $repository;
28
    }
29
30
    public function __invoke(EditSupplier $command): void
31
    {
32
        $supplierToUpdate = $this->repository->findOneByUuid($command->uuid()->toString());
33
34
        if (null === $supplierToUpdate) {
35
            throw new \DomainException('Supplier provided does not exist!');
36
        }
37
38
        $supplier = $this->updateSupplier($command, $supplierToUpdate);
39
40
        $this->repository->add($supplier);
41
    }
42
43
    private function updateSupplier(EditSupplier $command, Supplier $supplier): Supplier
44
    {
45
        if ($supplier->name() !== $command->name()) {
0 ignored issues
show
introduced by
The condition $supplier->name() !== $command->name() is always true.
Loading history...
46
            $supplier->renameSupplier($command->name());
47
        }
48
        if ($supplier->address() !== $command->address()
49
            || ($supplier->zipCode() !== $command->zipCode())
50
            || ($supplier->town() !== $command->town())
51
            || ($supplier->country() !== $command->country())
52
        ) {
53
            $supplier->rewriteAddress([
54
                $command->address(),
55
                $command->zipCode(),
56
                $command->town(),
57
                $command->country(),
58
            ]);
59
        }
60
        if ($supplier->phone() !== $command->phone()) {
0 ignored issues
show
introduced by
The condition $supplier->phone() !== $command->phone() is always true.
Loading history...
61
            $supplier->changePhoneNumber($command->phone());
62
        }
63
        if ($supplier->facsimile() !== $command->facsimile()) {
0 ignored issues
show
introduced by
The condition $supplier->facsimile() !== $command->facsimile() is always true.
Loading history...
64
            $supplier->changeFacsimileNumber($command->facsimile());
65
        }
66
        if ($supplier->email() !== $command->email()) {
0 ignored issues
show
introduced by
The condition $supplier->email() !== $command->email() is always true.
Loading history...
67
            $supplier->rewriteEmail($command->email());
68
        }
69
        if ($supplier->contact() !== $command->contact()) {
70
            $supplier->renameContact($command->contact());
71
        }
72
        if ($supplier->cellPhone() !== $command->cellPhone()) {
0 ignored issues
show
introduced by
The condition $supplier->cellPhone() !== $command->cellPhone() is always true.
Loading history...
73
            $supplier->changeCellphoneNumber($command->cellPhone());
74
        }
75
        if ($supplier->familyLog() !== $command->familyLog()) {
0 ignored issues
show
introduced by
The condition $supplier->familyLog() !== $command->familyLog() is always true.
Loading history...
76
            $supplier->reassignFamilyLog($command->familyLog());
77
        }
78
        if ($supplier->delayDelivery() !== $command->delayDelivery()) {
79
            $supplier->changeDelayDelivery($command->delayDelivery());
80
        }
81
        if ($supplier->orderDays() !== $command->orderDays()) {
82
            $supplier->reassignOrderDays($command->orderDays());
83
        }
84
85
        return $supplier;
86
    }
87
}
88