Passed
Push — master ( cb50cd...7264d5 )
by Chris
37s
created

TicketAvailability::reindex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Service\TicketAvailability;
4
5
use Carnage\Cqrs\Persistence\ReadModel\RepositoryInterface;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\Common\Collections\Criteria;
9
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter;
10
use ConferenceTools\Tickets\Domain\ValueObject\TicketType;
11
use ConferenceTools\Tickets\Domain\Service\TicketAvailability\Filters\FilterInterface;
12
13
class TicketAvailability
14
{
15
    /**
16
     * @var FilterInterface[]
17
     */
18
    private $filters;
19
20
    /**
21
     * @var RepositoryInterface
22
     */
23
    private $repository;
24
25
    /**
26
     * TicketAvailability constructor.
27
     * @param RepositoryInterface $repository
28
     * @param FilterInterface[] $filters
29
     */
30 4
    public function __construct(RepositoryInterface $repository, FilterInterface ...$filters)
31
    {
32 4
        $this->filters = $filters;
33 4
        $this->repository = $repository;
34 4
    }
35
36
    /**
37
     * @return TicketCounter[]|Collection
38
     */
39 4
    public function fetchAllAvailableTickets()
40
    {
41 4
        $tickets = $this->repository->matching(new Criteria());
42
43 4
        return $this->reindex($this->filterSet($tickets));
44
    }
45
46 3
    public function isAvailable(TicketType $ticketType, int $quantity)
47
    {
48 3
        $tickets = $this->fetchAllAvailableTickets();
49 3
        return isset($tickets[$ticketType->getIdentifier()]) &&
50 3
            $tickets[$ticketType->getIdentifier()]->getRemaining() >= $quantity;
51
    }
52
53 4
    private function filterSet(Collection $tickets): Collection
54
    {
55 4
        foreach ($this->filters as $filter) {
56
            /** @var FilterInterface $tickets */
57 4
            $tickets = $filter->filter($tickets);
0 ignored issues
show
Bug introduced by
$tickets of type ConferenceTools\Tickets\...Filters\FilterInterface is incompatible with the type Doctrine\Common\Collections\Collection expected by parameter $tickets of ConferenceTools\Tickets\...lterInterface::filter(). ( Ignorable by Annotation )

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

57
            $tickets = $filter->filter(/** @scrutinizer ignore-type */ $tickets);
Loading history...
58
        }
59
60 4
        return $tickets;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tickets could return the type ConferenceTools\Tickets\...Filters\FilterInterface which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection. Consider adding an additional type-check to rule them out.
Loading history...
61
    }
62
63 4
    private function reindex(Collection $tickets): Collection
64
    {
65 4
        $result = [];
66 4
        foreach($tickets as $ticket) {
67
            /** @var TicketCounter $ticket */
68 4
            $result[$ticket->getTicketType()->getIdentifier()] = $ticket;
69
        }
70
71 4
        return new ArrayCollection($result);
72
    }
73
}