|
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
|
|
|
|