|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Component\Ship\System; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use JsonMapper\JsonMapperInterface; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Stu\Component\Ship\System\Data\ShipSystemDataFactoryInterface; |
|
9
|
|
|
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface; |
|
10
|
|
|
use Stu\Orm\Entity\ShipInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SystemDataDeserializer implements SystemDataDeserializerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
6 |
|
public function __construct( |
|
16
|
|
|
private ShipSystemDataFactoryInterface $shipSystemDataFactory, |
|
17
|
|
|
private JsonMapperInterface $jsonMapper |
|
18
|
|
|
) { |
|
19
|
6 |
|
} |
|
20
|
|
|
|
|
21
|
4 |
|
public function getSpecificShipSystem( |
|
22
|
|
|
ShipInterface $ship, |
|
23
|
|
|
ShipSystemTypeEnum $systemType, |
|
24
|
|
|
string $className, |
|
25
|
|
|
Collection $shipSystemDataCache, |
|
26
|
|
|
ShipWrapperFactoryInterface $shipWrapperFactory |
|
27
|
|
|
) { |
|
28
|
|
|
if ( |
|
29
|
4 |
|
$systemType !== ShipSystemTypeEnum::SYSTEM_HULL |
|
30
|
4 |
|
&& !$ship->hasShipSystem($systemType) |
|
31
|
|
|
) { |
|
32
|
1 |
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
//add system to cache if not already deserialized |
|
36
|
3 |
|
if (!$shipSystemDataCache->containsKey($systemType->value)) { |
|
37
|
3 |
|
$systemData = $this->shipSystemDataFactory->createSystemData( |
|
38
|
3 |
|
$systemType, |
|
39
|
3 |
|
$shipWrapperFactory |
|
40
|
3 |
|
); |
|
41
|
3 |
|
$systemData->setShip($ship); |
|
42
|
|
|
|
|
43
|
3 |
|
$data = $systemType === ShipSystemTypeEnum::SYSTEM_HULL ? null : $ship->getShipSystem($systemType)->getData(); |
|
44
|
|
|
|
|
45
|
3 |
|
if ($data === null) { |
|
46
|
2 |
|
$shipSystemDataCache->set($systemType->value, $systemData); |
|
47
|
|
|
} else { |
|
48
|
1 |
|
$shipSystemDataCache->set( |
|
49
|
1 |
|
$systemType->value, |
|
50
|
1 |
|
$this->jsonMapper->mapObjectFromString( |
|
51
|
1 |
|
$data, |
|
52
|
1 |
|
$systemData |
|
53
|
1 |
|
) |
|
54
|
1 |
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
//load deserialized system from cache |
|
59
|
3 |
|
$cacheItem = $shipSystemDataCache->get($systemType->value); |
|
60
|
3 |
|
if (!$cacheItem instanceof $className) { |
|
61
|
|
|
throw new RuntimeException('this should not happen'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
return $cacheItem; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|