Completed
Push — master ( 232c3c...5c80cd )
by Paweł
03:09
created

existsAlreadyGivenOfBook()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.2
cc 4
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Clearcode\EHLibrary\Infrastructure\Persistence;
4
5
use Clearcode\EHLibrary\Model\Reservation;
6
use Clearcode\EHLibrary\Model\ReservationDoesNotExist;
7
use Clearcode\EHLibrary\Model\ReservationRepository;
8
use Everzet\PersistedObjects\AccessorObjectIdentifier;
9
use Everzet\PersistedObjects\FileRepository;
10
use Ramsey\Uuid\UuidInterface;
11
12
class LocalReservationRepository implements ReservationRepository
13
{
14
    /** @var FileRepository */
15
    private $file;
16
17
    public function clear()
18
    {
19
        $this->file->clear();
20
    }
21
22
    /** {@inheritdoc} */
23
    public function save(Reservation $reservation)
24
    {
25
        $this->file->save($reservation);
26
    }
27
28
    /** {@inheritdoc} */
29
    public function getAll()
30
    {
31
        return $this->file->getAll();
32
    }
33
34
    /** {@inheritdoc} */
35
    public function remove(UuidInterface $reservationId)
36
    {
37
        $reservation = $this->get($reservationId);
38
39
        $this->file->remove($reservation);
40
    }
41
42
    /** {@inheritdoc} */
43
    public function get(UuidInterface $reservationId)
44
    {
45
        if (null === $reservation = $this->file->findById($reservationId)) {
46
            throw new ReservationDoesNotExist(sprintf('Reservation with id %s does not exist.', $reservationId));
47
        }
48
49
        return $reservation;
50
    }
51
52
    /** {@inheritdoc} */
53
    public function existsAlreadyGivenOfBook(UuidInterface $bookId)
54
    {
55
        /** @var Reservation $reservation */
56
        foreach ($this->file->getAll() as $reservation) {
57
            if ($reservation->bookId()->equals($bookId) && $reservation->isGivenAway()) {
58
                return true;
59
            }
60
        }
61
62
        return false;
63
    }
64
65
    public function __construct()
66
    {
67
        $this->file = new FileRepository(__DIR__.'/../../../cache/reservations.db', new AccessorObjectIdentifier('id'));
68
    }
69
}
70