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

AnomalyProcessingEventStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 43
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processEvent() 0 3 1
A getAnomaly() 0 11 2
A getEntitiesToLock() 0 16 2
A __construct() 0 5 1
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