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

ArticleSubscriber::resolveContentOnPostSerialize()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.2
cc 4
eloc 11
nc 3
nop 1
crap 20
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\ArticleDocument;
18
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
19
use Sulu\Component\Content\Compat\StructureManagerInterface;
20
21
/**
22
 * Extends serialization for articles.
23
 */
24
class ArticleSubscriber implements EventSubscriberInterface
25
{
26
    use ArticleTypeTrait;
27
28
    /**
29
     * @var StructureManagerInterface
30
     */
31
    private $structureManager;
32
33
    /**
34
     * @param StructureManagerInterface $structureManager
35
     */
36
    public function __construct(StructureManagerInterface $structureManager)
37
    {
38
        $this->structureManager = $structureManager;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public static function getSubscribedEvents()
45
    {
46
        return [
47
            [
48
                'event' => Events::POST_SERIALIZE,
49
                'format' => 'json',
50
                'method' => 'addTypeOnPostSerialize',
51 30
            ],
52
        ];
53
    }
54
55
    /**
56 30
     * Append type to result.
57 30
     *
58 30
     * @param ObjectEvent $event
59 30
     */
60
    public function addTypeOnPostSerialize(ObjectEvent $event)
61
    {
62
        $article = $event->getObject();
63
        $visitor = $event->getVisitor();
64 1
        $context = $event->getContext();
65
66
        if (!($article instanceof ArticleDocument)) {
67
            return;
68 1
        }
69 1
70 1
        $structure = $this->structureManager->getStructure($article->getStructureType(), 'article');
71
        $visitor->addData('articleType', $context->accept($this->getType($structure->getStructure())));
72
    }
73
}
74