Completed
Push — feature/fixing_cost ( f58af6...414fbf )
by Laurent
01:41
created

CreateDamageCommandHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 12 2
A linkSupplierInvoice() 0 8 2
A linkFlight() 0 4 1
A insertLinks() 0 21 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
        $damage = FlightDamage::waiting(new DamageAmount($command->getAmount()), AuthorId::create($command->getAuthorId()));
44
        if(null !== $command->getFlightId()){
45
            $damage = FlightDamage::damage(FlightId::create($command->getFlightId()), new DamageAmount($command->getAmount()), AuthorId::create($command->getAuthorId()));
46
        }
47
48
        $id = $this->damageRepository->save($damage);
49
50
        $this->linkSupplierInvoice($id, $command->getBillId());
51
        $this->linkFlight($id, $command->getFlightId());
52
    }
53
54
    /**
55
     * @param int $damageId
56
     * @param int $invoiceId
57
     *
58
     * @throws \Exception
59
     */
60
    private function linkSupplierInvoice($damageId, $invoiceId)
61
    {
62
        if($invoiceId <= 0){
63
            return;
64
        }
65
66
        $this->insertLinks($damageId, $invoiceId, 'invoice_supplier');
67
    }
68
69
    /**
70
     * @param int $damageId
71
     * @param int $flightId
72
     */
73
    private function linkFlight($damageId, $flightId)
74
    {
75
        $this->insertLinks($damageId, $flightId, 'flightlog_bbcvols');
76
    }
77
78
    /**
79
     * @param int $damageId
80
     * @param int $targetId
81
     * @param string $targetType
82
     */
83
    private function insertLinks($damageId, $targetId, $targetType){
84
        $sql = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
85
        $sql .= "fk_source";
86
        $sql .= ", sourcetype";
87
        $sql .= ", fk_target";
88
        $sql .= ", targettype";
89
        $sql .= ") VALUES (";
90
        $sql .= $damageId;
91
        $sql .= ", 'flightlog_damage'";
92
        $sql .= ", ".$targetId;
93
        $sql .= ", '".$targetType."'";
94
        $sql .= ")";
95
96
        if ($this->db->query($sql))
97
        {
98
            $this->db->commit();
99
            return;
100
        }
101
102
        $this->db->rollback();
103
    }
104
105
}