Passed
Pull Request — master (#82)
by Chris
06:20 queued 02:55
created

DiscountCodeAvailability::reindex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 9.6666
c 0
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
    public function __construct(RepositoryInterface $repository, FilterInterface ...$filters)
32
    {
33
        $this->filters = $filters;
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * @return TicketCounter[]|Collection
39
     */
40
    public function fetchAllAvailableDiscountCodes()
41
    {
42
        $tickets = $this->repository->matching(new Criteria());
43
44
        return $this->reindex($this->filterSet($tickets));
45
    }
46
47
    private function filterSet(Collection $tickets): Collection
48
    {
49
        foreach ($this->filters as $filter) {
50
            /** @var FilterInterface $tickets */
51
            $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
        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
    private function reindex(Collection $discountCodes): Collection
58
    {
59
        $result = [];
60
        foreach($discountCodes as $discountCode) {
61
            /** @var DiscountCode $discountCode */
62
            $result[$discountCode->getCode()] = $discountCode;
63
        }
64
65
        return new ArrayCollection($result);
66
    }
67
68
    public function isAvailable(DiscountCode $discountCode)
69
    {
70
        $discountCodes = $this->fetchAllAvailableDiscountCodes();
71
        return isset($discountCodes[$discountCode->getCode()]);
72
    }
73
}