Passed
Pull Request — master (#80)
by Chris
02:34
created

Factory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 0
cts 22
cp 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A basketWithDiscount() 0 10 1
A createTicketReservations() 0 15 4
A __construct() 0 4 1
A basket() 0 5 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
    public function __construct(GeneratorInterface $ticketIdGenerator, Configuration $configuration)
25
    {
26
        $this->ticketIdGenerator = $ticketIdGenerator;
27
        $this->configuration = $configuration;
28
    }
29
30
    public function basket(TicketReservationRequest ...$reservationRequests): Basket
31
    {
32
        $tickets = $this->createTicketReservations($reservationRequests);
0 ignored issues
show
Bug introduced by
$reservationRequests of type array<integer,Conference...cketReservationRequest> is incompatible with the type ConferenceTools\Tickets\...icketReservationRequest expected by parameter $reservationRequests of ConferenceTools\Tickets\...ateTicketReservations(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $tickets = $this->createTicketReservations(/** @scrutinizer ignore-type */ $reservationRequests);
Loading history...
33
34
        return Basket::fromReservations($this->configuration, ...$tickets);
35
    }
36
37
    public function basketWithDiscount(
38
        DiscountCode $discountCode,
39
        TicketReservationRequest ...$reservationRequests
40
    ): Basket {
41
        $tickets = $this->createTicketReservations($reservationRequests);
0 ignored issues
show
Bug introduced by
$reservationRequests of type array<integer,Conference...cketReservationRequest> is incompatible with the type ConferenceTools\Tickets\...icketReservationRequest expected by parameter $reservationRequests of ConferenceTools\Tickets\...ateTicketReservations(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $tickets = $this->createTicketReservations(/** @scrutinizer ignore-type */ $reservationRequests);
Loading history...
42
43
        return Basket::fromReservationsWithDiscount(
44
            $this->configuration,
45
            $discountCode,
46
            ...$tickets
47
        );
48
    }
49
50
    /**
51
     * @return TicketReservation[]
52
     */
53
    private function createTicketReservations(TicketReservationRequest ...$reservationRequests): array
54
    {
55
        $tickets = [];
56
        foreach ($reservationRequests as $reservationRequest) {
57
            for ($i = 0; $i < $reservationRequest->getQuantity(); $i++) {
58
                $tickets[] = new TicketReservation($reservationRequest->getTicketType(),
59
                    $this->ticketIdGenerator->generateIdentity());
60
            }
61
        }
62
63
        if (count($tickets) === 0) {
64
            throw new \RuntimeException('Must specify at least 1 ticket for purchase');
65
        }
66
67
        return $tickets;
68
    }
69
}
70