CreateSupplierHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 37
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createSupplier() 0 17 1
A __invoke() 0 9 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\Supplier\Command\CreateSupplier;
17
use Administration\Domain\Supplier\Model\Supplier;
18
use Administration\Infrastructure\Persistence\DoctrineOrm\Entities\Supplier as SupplierEntity;
19
use Administration\Infrastructure\Persistence\DoctrineOrm\Repositories\DoctrineSupplierRepository;
20
use Core\Domain\Common\Command\CommandHandlerInterface;
21
use Core\Domain\Common\Model\VO\ContactUuid;
22
23
class CreateSupplierHandler implements CommandHandlerInterface
24
{
25
    private DoctrineSupplierRepository $repository;
26
27
    public function __construct(DoctrineSupplierRepository $repository)
28
    {
29
        $this->repository = $repository;
30
    }
31
32
    public function __invoke(CreateSupplier $command): void
33
    {
34
        if ($this->repository->existsWithName($command->companyName()->getValue())) {
35
            throw new \DomainException("Supplier with name: {$command->companyName()->getValue()} already exists.");
36
        }
37
38
        $supplier = $this->createSupplier($command);
39
40
        $this->repository->save(SupplierEntity::fromModel($supplier));
41
    }
42
43
    private function createSupplier(CreateSupplier $command): Supplier
44
    {
45
        return Supplier::create(
46
            ContactUuid::generate(),
47
            $command->companyName(),
48
            $command->address(),
49
            $command->zipCode(),
50
            $command->town(),
51
            $command->country(),
52
            $command->phone(),
53
            $command->facsimile(),
54
            $command->email(),
55
            $command->contact(),
56
            $command->cellPhone(),
57
            $command->familyLog(),
58
            $command->delayDelivery(),
59
            $command->orderDays()
60
        );
61
    }
62
}
63