|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Component\Anomaly; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Stu\Component\Anomaly\Type\AnomalyHandlerInterface; |
|
9
|
|
|
use Stu\Orm\Entity\AnomalyInterface; |
|
10
|
|
|
use Stu\Orm\Repository\AnomalyRepositoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
final class AnomalyHandling implements AnomalyHandlingInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private AnomalyRepositoryInterface $anomalyRepository; |
|
15
|
|
|
|
|
16
|
|
|
/** @var array<AnomalyHandlerInterface> */ |
|
17
|
|
|
private array $handlerList; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param array<AnomalyHandlerInterface> $handlerList |
|
21
|
|
|
*/ |
|
22
|
3 |
|
public function __construct( |
|
23
|
|
|
AnomalyRepositoryInterface $anomalyRepository, |
|
24
|
|
|
array $handlerList |
|
25
|
|
|
) { |
|
26
|
3 |
|
$this->anomalyRepository = $anomalyRepository; |
|
27
|
3 |
|
$this->handlerList = $handlerList; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function processExistingAnomalies(): void |
|
31
|
|
|
{ |
|
32
|
3 |
|
foreach ($this->anomalyRepository->findAll() as $anomaly) { |
|
33
|
3 |
|
if (!array_key_exists($anomaly->getAnomalyType()->getId(), $this->handlerList)) { |
|
34
|
1 |
|
throw new RuntimeException(sprintf('no handler defined for type: %d', $anomaly->getAnomalyType()->getId())); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
$handler = $this->handlerList[$anomaly->getAnomalyType()->getId()]; |
|
38
|
|
|
|
|
39
|
2 |
|
$handler->handleShipTick($anomaly); |
|
40
|
2 |
|
$this->decreaseLifespan($anomaly, $handler); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function createNewAnomalies(): void |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($this->handlerList as $handler) { |
|
47
|
|
|
$handler->checkForCreation(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
private function decreaseLifespan(AnomalyInterface $anomaly, AnomalyHandlerInterface $handler): void |
|
52
|
|
|
{ |
|
53
|
2 |
|
$remainingTicks = $anomaly->getRemainingTicks(); |
|
54
|
|
|
|
|
55
|
2 |
|
if ($remainingTicks === 1) { |
|
56
|
1 |
|
$handler->letAnomalyDisappear($anomaly); |
|
57
|
1 |
|
$this->anomalyRepository->delete($anomaly); |
|
58
|
|
|
} else { |
|
59
|
1 |
|
$anomaly->setRemainingTicks($remainingTicks - 1); |
|
60
|
1 |
|
$this->anomalyRepository->save($anomaly); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|