Passed
Push — master ( c87045...1efded )
by Chris
34s
created

DiscountCodeAvailability::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Service\Availability;
4
5
use Carnage\Cqrs\Persistence\ReadModel\RepositoryInterface;
6
use ConferenceTools\Tickets\Domain\Service\Configuration;
7
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Collection;
10
use Doctrine\Common\Collections\Criteria;
11
use ConferenceTools\Tickets\Domain\ReadModel\Counts\TicketCounter;
0 ignored issues
show
Bug introduced by
The type ConferenceTools\Tickets\...el\Counts\TicketCounter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use ConferenceTools\Tickets\Domain\ValueObject\TicketType;
13
use ConferenceTools\Tickets\Domain\Service\Availability\Filters\FilterInterface;
14
15
class DiscountCodeAvailability
16
{
17
    /**
18
     * @var FilterInterface[]
19
     */
20
    private $filters;
21
    /**
22
     * @var RepositoryInterface
23
     */
24
    private $repository;
25
26
    /**
27
     * TicketAvailability constructor.
28
     * @param RepositoryInterface $repository
29
     * @param FilterInterface[] $filters
30
     */
31 4
    public function __construct(RepositoryInterface $repository, FilterInterface ...$filters)
32
    {
33 4
        $this->filters = $filters;
34 4
        $this->repository = $repository;
35 4
    }
36
37
    /**
38
     * @return TicketCounter[]|Collection
39
     */
40 4
    public function fetchAllAvailableDiscountCodes()
41
    {
42 4
        $tickets = $this->repository->matching(new Criteria());
43
44 4
        return $this->reindex($this->filterSet($tickets));
45
    }
46
47 4
    private function filterSet(Collection $tickets): Collection
48
    {
49 4
        foreach ($this->filters as $filter) {
50
            /** @var FilterInterface $tickets */
51 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

51
            $tickets = $filter->filter(/** @scrutinizer ignore-type */ $tickets);
Loading history...
52
        }
53
54 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...
55
    }
56
57 4
    private function reindex(Collection $discountCodes): Collection
58
    {
59 4
        $result = [];
60 4
        foreach($discountCodes as $discountCode) {
61
            /** @var DiscountCode $discountCode */
62 4
            $result[$discountCode->getCode()] = $discountCode;
63
        }
64
65 4
        return new ArrayCollection($result);
66
    }
67
68 3
    public function isAvailable(DiscountCode $discountCode)
69
    {
70 3
        $discountCodes = $this->fetchAllAvailableDiscountCodes();
71 3
        return isset($discountCodes[$discountCode->getCode()]);
72
    }
73
}