AddInsuranceCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Madkom\RegistryApplication\Application\CarManagement\Command\Insurance;
4
5
use Madkom\RegistryApplication\Application\CarManagement\Command\CommandInterface;
6
use Madkom\RegistryApplication\Application\CarManagement\DocumentDTO;
7
use Madkom\RegistryApplication\Application\CarManagement\InsuranceDTO;
8
use Madkom\RegistryApplication\Domain\CarManagement\DocumentFactory;
9
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\InsuranceFactory;
10
use Madkom\RegistryApplication\Infrastructure\CarManagement\CarInMemoryRepository;
11
12
/**
13
 * Class AddInsuranceCommand.
14
 */
15
final class AddInsuranceCommand implements CommandInterface
16
{
17
    /** @var \Madkom\RegistryApplication\Application\CarManagement\InsuranceDTO $insuranceDTO */
18
    private $insuranceDTO;
19
20
    /** @var \Madkom\RegistryApplication\Infrastructure\CarManagement\CarInMemoryRepository $repository */
21
    private $repository;
22
23
    /** @var \Madkom\RegistryApplication\Domain\CarManagement\Car $car */
24
    private $car;
25
26
    private $documentDTO;
27
28
    private $documentFile;
29
30
    private function __construct()
31
    {
32
    }
33
34
    public static function addWithFile(
35
        CarInMemoryRepository $repository,
36
        $carId,
37
        InsuranceDTO $insuranceDTO,
38
        DocumentDTO $documentDTO
39
    ) {
40
        $command = new self();
41
        $carInsuranceDocument = new DocumentFactory();
42
43
        $command->repository = $repository;
44
        $command->insuranceDTO = $insuranceDTO;
45
        $command->documentDTO = $documentDTO;
46
        $command->car = $command->repository->find($carId);
47
48
        $command->documentFile = $carInsuranceDocument->create(DocumentFactory::INSURANCE_DOCUMENT,
49
                                                                $command->documentDTO->docId,
50
                                                                $command->documentDTO->title,
51
                                                                $documentDTO->description,
52
                                                                $documentDTO->source
53
        );
54
55
        return $command;
56
    }
57
58
    public static function add(CarInMemoryRepository $repository, $carId, InsuranceDTO $dto)
59
    {
60
        $command = new self();
61
62
        $command->repository = $repository;
63
        $command->insuranceDTO = $dto;
64
        $command->car = $command->repository->find($carId);
65
66
        return $command;
67
    }
68
69
    public function execute()
70
    {
71
        $insurance = $this->insuranceDTO;
72
        $insuranceFactory = new InsuranceFactory();
73
        $newInsurance = $insuranceFactory->create($insurance->insuranceId,
74
                                                        $insurance->type,
75
                                                        new \DateTime($insurance->dateFrom),
76
                                                        new \DateTime($insurance->dateTo),
77
                                                        $insurance->insurerId
78
        );
79
80
        $this->car->addInsurance($newInsurance);
81
82
        if ($this->documentFile) {
83
            $this->car->addInsuranceDocument($newInsurance->getId(), $this->documentFile);
84
        }
85
86
        $this->repository->add($this->car);
87
    }
88
}
89