Completed
Pull Request — develop (#147)
by Wachter
14:07
created

ArticlePageSubscriber   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 34
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 10 1
A addTitleOnPostSerialize() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Document\Serializer;
13
14
use JMS\Serializer\EventDispatcher\Events;
15
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
16
use JMS\Serializer\EventDispatcher\ObjectEvent;
17
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
18
19
/**
20
 * Extends serialization for article-pages.
21
 */
22
class ArticlePageSubscriber implements EventSubscriberInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public static function getSubscribedEvents()
28
    {
29
        return [
30
            [
31
                'event' => Events::POST_SERIALIZE,
32
                'format' => 'json',
33
                'method' => 'addTitleOnPostSerialize',
34
            ],
35
        ];
36
    }
37
38
    /**
39
     * Append title to result.
40
     *
41
     * @param ObjectEvent $event
42
     */
43
    public function addTitleOnPostSerialize(ObjectEvent $event)
44
    {
45
        $articlePage = $event->getObject();
46
        $visitor = $event->getVisitor();
47
        $context = $event->getContext();
48
49
        if (!$articlePage instanceof ArticlePageDocument) {
50
            return;
51
        }
52
53
        $visitor->addData('title', $context->accept($articlePage->getParent()->getTitle()));
54
    }
55
}
56