Passed
Push — dev ( eeaa0f...91a85a )
by Janko
26:10
created

EventQueue::getNextEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Component\Event;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\Common\Collections\Order;
9
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
10
use RuntimeException;
11
use Stu\Orm\Entity\EventInterface;
12
use Stu\Orm\Repository\EventRepositoryInterface;
13
14
class EventQueue implements EventQueueInterface
15
{
16
    /** @var null|ArrayCollection<int, EventInterface> */
17
    private ?Collection $events = null;
18
19 6
    public function __construct(private EventRepositoryInterface $eventRepository)
20
    {
21 6
    }
22
23 3
    #[Override]
24
    public function getSize(): int
25
    {
26 3
        return $this->getEvents()->count();
27
    }
28
29 3
    #[Override]
30
    public function getNextEvent(): EventInterface
31
    {
32 3
        $next = $this->getEvents()->matching(
0 ignored issues
show
Bug introduced by
The method matching() does not exist on Doctrine\Common\Collections\Collection. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Collections\AbstractLazyCollection. Are you sure you never get one of those? ( Ignorable by Annotation )

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

32
        $next = $this->getEvents()->/** @scrutinizer ignore-call */ matching(
Loading history...
33 3
            Criteria::create()
34 3
                ->orderBy([
35 3
                    'priority' => Order::Descending,
36 3
                    'retries' => Order::Descending
37 3
                ])
38 3
        )->first();
39
40 3
        if (!$next) {
41 1
            throw new RuntimeException('isEmpty should be called first');
42
        }
43
44 2
        return $next;
45
    }
46
47 1
    #[Override]
48
    public function addEvent(EventInterface $event): void
49
    {
50 1
        $this->getEvents()->add($event);
51
    }
52
53 1
    #[Override]
54
    public function removeEvent(EventInterface $event): void
55
    {
56 1
        $this->getEvents()->removeElement($event);
57
    }
58
59
    public function clear(): void
60
    {
61
        $this->getEvents()->clear();
62
    }
63
64
    /** @return ArrayCollection<int, EventInterface> */
65 6
    private function getEvents(): Collection
66
    {
67 6
        if ($this->events === null) {
68 6
            $this->events = $this->eventRepository->getEventsByPriority();
69
        }
70
71 6
        return $this->events;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->events could return the type null which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection. Consider adding an additional type-check to rule them out.
Loading history...
72
    }
73
}
74