GiveAwayBookInReservation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Clearcode\EHLibrary\Application\UseCase;
4
5
use Clearcode\EHLibrary\Model\BookInReservationAlreadyGivenAway;
6
use Clearcode\EHLibrary\Model\ReservationRepository;
7
use Ramsey\Uuid\UuidInterface;
8
9
class GiveAwayBookInReservation
10
{
11
    /** @var ReservationRepository */
12
    private $reservations;
13
14
    /**
15
     * @param ReservationRepository $reservations
16
     */
17
    public function __construct(ReservationRepository $reservations)
18
    {
19
        $this->reservations = $reservations;
20
    }
21
22
    /**
23
     * @param UuidInterface $reservationId
24
     * @param \DateTime     $givenAwayAt
25
     *
26
     * @throws BookInReservationAlreadyGivenAway
27
     */
28
    public function giveAway(UuidInterface $reservationId, \DateTime $givenAwayAt)
29
    {
30
        $reservation = $this->reservations->get($reservationId);
31
32
        if ($this->reservations->existsAlreadyGivenOfBook($reservation->bookId())) {
33
            throw new BookInReservationAlreadyGivenAway(sprintf('Book with id %s in reservation with id %s was already given away.', $reservation->bookId(), $reservation->id()));
34
        }
35
36
        $reservation->giveAway($givenAwayAt);
37
38
        $this->reservations->save($reservation);
39
    }
40
}
41