Completed
Pull Request — master (#1146)
by Giovanni
06:29
created

CustomPropertyListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Functional\app\SerializerWithListener\EventListener;
4
5
6
use Elastica\Document;
7
use FOS\ElasticaBundle\Event\TransformEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
/**
11
 * @author Giovanni Albero <[email protected]>
12
 */
13
class CustomPropertyListener implements EventSubscriberInterface
14
{
15
    public function addCustomProperty(TransformEvent $event)
16
    {
17
        /** @var Document $document */
18
        $document = $event->getDocument();
19
20
        $data = $document->getData();
21
22
        if (is_string($data)) {
23
            $unserializeData = json_decode($data, true);
24
            $unserializeData['field1'] = 'post_persister';
25
            $document->setData(json_encode($unserializeData));
26
        }
27
    }
28
29
    public static function getSubscribedEvents()
30
    {
31
        return array (
32
            TransformEvent::POST_TRANSFORM => 'addCustomProperty',
33
        );
34
    }
35
}