Completed
Pull Request — develop (#179)
by Wachter
13:33
created

ArticleSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 39.82 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 2
cbo 8
dl 45
loc 113
ccs 16
cts 16
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscribedEvents() 0 15 1
A addTypeOnPostSerialize() 13 13 2
A addPageTitlePropertyNameOnPostSerialize() 15 15 3
A getPageTitleProperty() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Document\ArticleInterface;
19
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
20
use Sulu\Component\Content\Compat\StructureManagerInterface;
21
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
22
use Sulu\Component\Content\Metadata\PropertyMetadata;
23
24
/**
25
 * Extends serialization for articles.
26
 */
27
class ArticleSubscriber implements EventSubscriberInterface
28
{
29
    const PAGE_TITLE_TAG_NAME = 'sulu_article.page_title';
30
    const PAGE_TITLE_PROPERTY_NAME = 'pageTitle';
31
32
    use ArticleTypeTrait;
33
34
    /**
35
     * @var StructureManagerInterface
36 49
     */
37
    private $structureManager;
38 49
39 49
    /**
40
     * @var StructureMetadataFactoryInterface
41
     */
42
    private $structureMetadataFactory;
43
44 1
    /**
45
     * @param StructureManagerInterface $structureManager
46
     * @param StructureMetadataFactoryInterface $structureMetadataFactory
47
     */
48 1
    public function __construct(
49 1
        StructureManagerInterface $structureManager,
50 1
        StructureMetadataFactoryInterface $structureMetadataFactory
51
    ) {
52
        $this->structureManager = $structureManager;
53
        $this->structureMetadataFactory = $structureMetadataFactory;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public static function getSubscribedEvents()
60 49
    {
61
        return [
62 49
            [
63 49
                'event' => Events::POST_SERIALIZE,
64 49
                'format' => 'json',
65
                'method' => 'addTypeOnPostSerialize',
66 49
            ],
67 19
            [
68
                'event' => Events::POST_SERIALIZE,
69
                'format' => 'json',
70 48
                'method' => 'addPageTitlePropertyNameOnPostSerialize',
71 48
            ],
72 48
        ];
73
    }
74
75
    /**
76
     * Append type to result.
77
     *
78
     * @param ObjectEvent $event
79
     */
80 View Code Duplication
    public function addTypeOnPostSerialize(ObjectEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $article = $event->getObject();
83
        $visitor = $event->getVisitor();
84
        $context = $event->getContext();
85
86
        if (!($article instanceof ArticleDocument)) {
87
            return;
88
        }
89
90
        $structure = $this->structureManager->getStructure($article->getStructureType(), 'article');
91
        $visitor->addData('articleType', $context->accept($this->getType($structure->getStructure())));
92
    }
93
94
    /**
95
     * Append page-title-property to result.
96
     *
97
     * @param ObjectEvent $event
98
     */
99 View Code Duplication
    public function addPageTitlePropertyNameOnPostSerialize(ObjectEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $article = $event->getObject();
102
        $visitor = $event->getVisitor();
103
        $context = $event->getContext();
104
105
        if (!$article instanceof ArticleInterface) {
106
            return;
107
        }
108
109
        $property = $this->getPageTitleProperty($article);
110
        if ($property) {
111
            $visitor->addData('_pageTitlePropertyName', $context->accept($property->getName()));
112
        }
113
    }
114
115
    /**
116
     * Find page-title property.
117
     *
118
     * @param ArticleInterface $document
119
     *
120
     * @return PropertyMetadata
121
     */
122 View Code Duplication
    private function getPageTitleProperty(ArticleInterface $document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $metadata = $this->structureMetadataFactory->getStructureMetadata(
125
            'article',
126
            $document->getStructureType()
127
        );
128
129
        if ($metadata->hasPropertyWithTagName(self::PAGE_TITLE_TAG_NAME)) {
130
            return $metadata->getPropertyByTagName(self::PAGE_TITLE_TAG_NAME);
131
        }
132
133
        if ($metadata->hasProperty(self::PAGE_TITLE_PROPERTY_NAME)) {
134
            return $metadata->getProperty(self::PAGE_TITLE_PROPERTY_NAME);
135
        }
136
137
        return null;
138
    }
139
}
140