GiveAwayBookInReservation::giveAway()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 12
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
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