Completed
Push — feature/pilot_information ( 13ff1e...cc067b )
by Laurent
01:46
created

PilotRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Infrastructure\Pilot\Repository;
5
6
7
use FlightLog\Domain\Pilot\Pilot;
8
use FlightLog\Domain\Pilot\ValueObject\PilotId;
9
use FlightLog\Infrastructure\Common\Repository\AbstractDomainRepository;
10
11
final class PilotRepository extends AbstractDomainRepository
12
{
13
    public function __construct(\DoliDB $db)
14
    {
15
        parent::__construct($db, 'llx_bbc_pilots');
16
    }
17
18
    public function save(Pilot $pilot)
19
    {
20
        if ($this->exist($pilot->id())) {
21
            $this->update($pilot->id()->getId(), $pilot->state(), 'user_id');
22
            return;
23
        }
24
25
        $this->write($pilot->state());
26
    }
27
28
    public function exist(PilotId $pilotId): bool
29
    {
30
        return null !== $this->get($pilotId->getId(), 'user_id');
31
    }
32
33
    public function getById(PilotId $id): Pilot
34
    {
35
        $pilot = $this->get($id->getId(), 'user_id');
36
        if (null === $pilot) {
37
            throw new \Exception('Pilot not found');
38
        }
39
40
        return Pilot::fromState($pilot);
41
    }
42
}