Passed
Push — master ( 9be0c8...8c1bed )
by Chris
02:56
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Service\Basket;
4
5
use Carnage\Cqrs\Aggregate\Identity\GeneratorInterface;
6
use ConferenceTools\Tickets\Domain\Service\Configuration;
7
use ConferenceTools\Tickets\Domain\ValueObject\Basket;
8
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode;
9
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservation;
10
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservationRequest;
11
12
class Factory
13
{
14
    /**
15
     * @var GeneratorInterface
16
     */
17
    private $ticketIdGenerator;
18
19
    /**
20
     * @var Configuration
21
     */
22
    private $configuration;
23
24
    /**
25
     * @var BasketValidator
26
     */
27
    private $basketValidator;
28
29 2
    public function __construct(
30
        GeneratorInterface $ticketIdGenerator,
31
        Configuration $configuration,
32
        BasketValidator $basketValidator
33
    ) {
34 2
        $this->ticketIdGenerator = $ticketIdGenerator;
35 2
        $this->configuration = $configuration;
36 2
        $this->basketValidator = $basketValidator;
37 2
    }
38
39 1
    public function basket(TicketReservationRequest ...$reservationRequests): Basket
40
    {
41 1
        $tickets = $this->createTicketReservations(...$reservationRequests);
42
43 1
        return Basket::fromReservations(
44 1
            $this->configuration,
45 1
            $this->basketValidator,
46 1
            ...$tickets
47
        );
48
    }
49
50 1
    public function basketWithDiscount(
51
        DiscountCode $discountCode,
52
        TicketReservationRequest ...$reservationRequests
53
    ): Basket {
54 1
        $tickets = $this->createTicketReservations(...$reservationRequests);
55
56 1
        return Basket::fromReservationsWithDiscount(
57 1
            $this->configuration,
58 1
            $this->basketValidator,
59 1
            $discountCode,
60 1
            ...$tickets
61
        );
62
    }
63
64
    /**
65
     * @return TicketReservation[]
66
     */
67 2
    private function createTicketReservations(TicketReservationRequest ...$reservationRequests): array
68
    {
69 2
        $tickets = [];
70 2
        foreach ($reservationRequests as $reservationRequest) {
71 2
            for ($i = 0; $i < $reservationRequest->getQuantity(); $i++) {
72 2
                $tickets[] = new TicketReservation($reservationRequest->getTicketType(),
73 2
                    $this->ticketIdGenerator->generateIdentity());
74
            }
75
        }
76
77 2
        return $tickets;
78
    }
79
}
80