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

CreateSupplierHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
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\CreateSupplier;
18
use Administration\Domain\Supplier\Model\Supplier;
19
use Core\Domain\Common\Model\VO\ContactUuid;
20
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol;
21
22
class CreateSupplierHandler implements CommandHandlerProtocol
23
{
24
    private SupplierRepositoryProtocol $repository;
25
26
    public function __construct(SupplierRepositoryProtocol $repository)
27
    {
28
        $this->repository = $repository;
29
    }
30
31
    public function __invoke(CreateSupplier $command): void
32
    {
33
        if ($this->repository->existsWithName($command->name()->getValue())) {
34
            throw new \DomainException("Supplier with name: {$command->name()->getValue()} already exists.");
35
        }
36
37
        $supplier = $this->createSupplier($command);
38
39
        $this->repository->add($supplier);
40
    }
41
42
    private function createSupplier(CreateSupplier $command): Supplier
43
    {
44
        return Supplier::create(
45
            ContactUuid::generate(),
46
            $command->name(),
47
            $command->address(),
48
            $command->zipCode(),
49
            $command->town(),
50
            $command->country(),
51
            $command->phone(),
52
            $command->facsimile(),
53
            $command->email(),
54
            $command->contact(),
55
            $command->cellPhone(),
56
            $command->familyLog(),
57
            $command->delayDelivery(),
58
            $command->orderDays()
59
        );
60
    }
61
}
62