|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lamoda\TacticianQueue\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
use JMS\Serializer\Context; |
|
8
|
|
|
use JMS\Serializer\GraphNavigator; |
|
9
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
10
|
|
|
use JMS\Serializer\JsonDeserializationVisitor; |
|
11
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
12
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
13
|
|
|
|
|
14
|
|
|
final class CommandSerializationHandler implements SubscribingHandlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Serializer */ |
|
17
|
|
|
private $serializer; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Serializer $serializer) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->serializer = $serializer; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function serialize(JsonSerializationVisitor $visitor, $command, array $type, Context $context): array |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
'command' => get_class($command), |
|
28
|
|
|
'properties' => json_decode($this->serializer->serialize($command, 'json'), true), |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function deserialize(JsonDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
return $this->serializer->deserialize(json_encode($data['properties']), $data['command'], 'json'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function getSubscribingMethods() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
[ |
|
41
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
42
|
|
|
'format' => 'json', |
|
43
|
|
|
'type' => 'tactician_command', |
|
44
|
|
|
'method' => 'serialize', |
|
45
|
|
|
], |
|
46
|
|
|
[ |
|
47
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
|
48
|
|
|
'format' => 'json', |
|
49
|
|
|
'type' => 'tactician_command', |
|
50
|
|
|
'method' => 'deserialize', |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.