Completed
Push — feature/fixing_cost ( ef981e )
by Laurent
01:53
created

CreateDamageCommandHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 8 1
A linkSupplierInvoice() 0 10 2
1
<?php
2
3
4
namespace FlightLog\Application\Damage\Command;
5
6
7
use FlightLog\Domain\Damage\AuthorId;
8
use FlightLog\Domain\Damage\DamageAmount;
9
use FlightLog\Domain\Damage\FlightDamage;
10
use FlightLog\Domain\Damage\FlightId;
11
use FlightLog\Infrastructure\Damage\Repository\FlightDamageRepository;
12
13
final class CreateDamageCommandHandler
14
{
15
    /**
16
     * @var FlightDamageRepository
17
     */
18
    private $damageRepository;
19
20
    /**
21
     * @var \DoliDB
22
     */
23
    private $db;
24
25
    /**
26
     * @param \DoliDB $db
27
     * @param FlightDamageRepository $damageRepository
28
     */
29
    public function __construct(\DoliDB $db, FlightDamageRepository $damageRepository)
30
    {
31
        $this->damageRepository = $damageRepository;
32
        $this->db = $db;
33
    }
34
35
36
    /**
37
     * @param CreateDamageCommand $command
38
     *
39
     * @throws \Exception
40
     */
41
    public function __invoke(CreateDamageCommand $command)
42
    {
43
        //Create the damage in the DB
44
        $damage = FlightDamage::damage(FlightId::create($command->getFlightId()), new DamageAmount($command->getAmount()), AuthorId::create($command->getAuthorId()));
45
        $this->damageRepository->save($damage);
46
47
        $this->linkSupplierInvoice($command->getFlightId(), $command->getBillId());
48
    }
49
50
    /**
51
     * @param int $flightId
52
     * @param int $invoiceId
53
     *
54
     * @throws \Exception
55
     */
56
    private function linkSupplierInvoice($flightId, $invoiceId)
57
    {
58
        if($invoiceId <= 0){
59
            return;
60
        }
61
62
        $flight = new \Bbcvols($this->db);
63
        $flight->fetch($flightId);
64
        $flight->add_object_linked('invoice_supplier', $invoiceId);
65
    }
66
67
}