Completed
Push — feature/fixing_cost ( 2c76a0...f58af6 )
by Laurent
01:38
created

FlightDamageRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Infrastructure\Damage\Repository;
5
6
use FlightLog\Domain\Damage\AuthorId;
7
use FlightLog\Domain\Damage\DamageAmount;
8
use FlightLog\Domain\Damage\DamageId;
9
use FlightLog\Domain\Damage\FlightDamage;
10
use FlightLog\Domain\Damage\FlightId;
11
use FlightLog\Infrastructure\Common\Repository\AbstractDomainRepository;
12
13
final class FlightDamageRepository extends AbstractDomainRepository
14
{
15
16
    /**
17
     * @param \DoliDB $db
18
     */
19
    public function __construct(\DoliDB $db)
20
    {
21
        parent::__construct($db, 'bbc_flight_damages');
22
    }
23
24
    /**
25
     * @param FlightDamage $flightDamage
26
     *
27
     * @throws \Exception
28
     */
29
    public function save(FlightDamage $flightDamage)
30
    {
31
        $fields = [
32
            'flight_id' => $flightDamage->getFlightId()->getId(),
33
            'billed' => $flightDamage->isBilled(),
34
            'amount' => $flightDamage->amount()->getValue(),
35
            'author_id' => $flightDamage->getAuthor()->getId()
36
        ];
37
38
        if($flightDamage->getId()){
39
            $this->update($flightDamage->getId()->getId(), $fields);
40
            return;
41
        }
42
43
        $this->write($fields);
44
    }
45
46
    /**
47
     * @param DamageId $id
48
     *
49
     * @return FlightDamage
50
     *
51
     * @throws \Exception
52
     */
53
    public function getById(DamageId $id){
54
        $damage = $this->get($id->getId());
55
56
        if(null === $damage){
57
            throw new \Exception('Damage not found');
58
        }
59
60
        return FlightDamage::load(
61
            FlightId::create($damage['flight_id']),
62
            new DamageAmount($damage['amount']),
63
            $damage['billed'],
64
            AuthorId::create($damage['author_id']),
65
            DamageId::create($damage['rowid'])
66
        );
67
    }
68
}