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

getEntitiesToLock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 6
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Component\Event\Strategy;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use JsonMapper\JsonMapperInterface;
8
use RuntimeException;
9
use Stu\Component\Anomaly\AnomalyHandlingInterface;
10
use Stu\Component\Event\Payload\PayloadWithIdAndTurn;
11
use Stu\Orm\Entity\AnomalyInterface;
12
use Stu\Orm\Entity\EventInterface;
13
use Stu\Orm\Entity\ShipInterface;
14
use Stu\Orm\Entity\UserInterface;
15
use Stu\Orm\Repository\AnomalyRepositoryInterface;
16
17
class AnomalyProcessingEventStrategy implements EventStrategyInterface
18
{
19
    public function __construct(
20
        private AnomalyRepositoryInterface $anomalyRepository,
21
        private AnomalyHandlingInterface $anomalyHandling,
22
        private JsonMapperInterface $jsonMapper
23
    ) {
24
    }
25
26
    public function getEntitiesToLock(EventInterface $event): Collection
27
    {
28
        $users = $this->getAnomaly($event)
29
            ->getLocation()
30
            ->getShips()
31
            ->map(fn (ShipInterface $ship) => $ship->getUser())
32
            ->filter(fn (UserInterface $user) => !$user->isVacationRequestOldEnough());
33
34
        $result = [];
35
36
        foreach ($users as $user) {
37
38
            $result[$user->getId()] = $user;
39
        }
40
41
        return new ArrayCollection($result);
42
    }
43
44
    public function processEvent(EventInterface $event): void
45
    {
46
        $this->anomalyHandling->processExistingAnomaly($this->getAnomaly($event));
47
    }
48
49
    private function getAnomaly(EventInterface $event): AnomalyInterface
50
    {
51
        /** @var PayloadWithIdAndTurn */
52
        $payload = $event->getPayloadAsObject($this->jsonMapper);
53
54
        $anomaly = $this->anomalyRepository->find($payload->id);
55
        if ($anomaly === null) {
56
            throw new RuntimeException('no existing anomaly found for id %d', $payload->id);
57
        }
58
59
        return $anomaly;
60
    }
61
}
62