|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LoginCidadao\BadgesControlBundle\Event; |
|
4
|
|
|
|
|
5
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
7
|
|
|
use JMS\Serializer\EventDispatcher\PreSerializeEvent; |
|
8
|
|
|
use JMS\Serializer\GenericSerializationVisitor; |
|
9
|
|
|
use LoginCidadao\APIBundle\Service\VersionService; |
|
10
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
11
|
|
|
use LoginCidadao\BadgesControlBundle\Handler\BadgesHandler; |
|
12
|
|
|
|
|
13
|
|
|
class SerializationSubscriber implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var BadgesHandler */ |
|
16
|
|
|
protected $handler; |
|
17
|
|
|
|
|
18
|
|
|
/** @var VersionService */ |
|
19
|
|
|
private $versionService; |
|
20
|
|
|
|
|
21
|
6 |
|
public function __construct(BadgesHandler $handler, VersionService $versionService) |
|
22
|
|
|
{ |
|
23
|
6 |
|
$this->handler = $handler; |
|
24
|
6 |
|
$this->versionService = $versionService; |
|
25
|
6 |
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public static function getSubscribedEvents() |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
[ |
|
31
|
1 |
|
'event' => 'serializer.pre_serialize', |
|
32
|
|
|
'method' => 'onPreSerialize', |
|
33
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
34
|
|
|
], |
|
35
|
|
|
[ |
|
36
|
|
|
'event' => 'serializer.post_serialize', |
|
37
|
|
|
'method' => 'onPostSerialize', |
|
38
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
39
|
|
|
], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
public function onPreSerialize(PreSerializeEvent $event) |
|
44
|
|
|
{ |
|
45
|
3 |
|
$person = $event->getObject(); |
|
46
|
3 |
|
if ($person instanceof PersonInterface) { |
|
47
|
1 |
|
$this->handler->evaluate($person); |
|
48
|
|
|
} |
|
49
|
3 |
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
public function onPostSerialize(ObjectEvent $event) |
|
52
|
|
|
{ |
|
53
|
4 |
|
$person = $event->getObject(); |
|
54
|
|
|
|
|
55
|
4 |
|
if (!$person instanceof PersonInterface) { |
|
56
|
2 |
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if (version_compare($this->getApiVersion(), '2', '>=')) { |
|
60
|
1 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** @var GenericSerializationVisitor $visitor */ |
|
64
|
1 |
|
$visitor = $event->getVisitor(); |
|
65
|
|
|
|
|
66
|
1 |
|
$badges = []; |
|
67
|
1 |
|
foreach ($person->getBadges() as $badge) { |
|
68
|
1 |
|
$key = "{$badge->getNamespace()}.{$badge->getName()}"; |
|
69
|
1 |
|
$badges[$key] = $badge->getData(); |
|
70
|
|
|
} |
|
71
|
1 |
|
$visitor->addData('badges', $badges); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
2 |
|
private function getApiVersion() |
|
78
|
|
|
{ |
|
79
|
2 |
|
$version = $this->versionService->getVersionFromRequest(); |
|
80
|
|
|
|
|
81
|
2 |
|
return $this->versionService->getString($version); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|