Completed
Pull Request — develop (#423)
by
unknown
13:26
created

SingleArticleSelectionContentType   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 1
dl 11
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A getContentData() 0 13 2
A preResolve() 0 9 2

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) Sulu 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\Content;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
16
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
17
use Sulu\Component\Content\Compat\PropertyInterface;
18
use Sulu\Component\Content\PreResolvableContentTypeInterface;
19
use Sulu\Component\Content\SimpleContentType;
20
21
/**
22
 * Provides article_selection content-type.
23
 */
24
class SingleArticleSelectionContentType extends SimpleContentType implements PreResolvableContentTypeInterface
25
{
26
    use ArticleViewDocumentIdTrait;
27
28
    /**
29
     * @var Manager
30
     */
31
    private $searchManager;
32
33
    /**
34
     * @var ReferenceStoreInterface
35
     */
36
    private $referenceStore;
37
38
    /**
39
     * @var string
40
     */
41
    private $articleDocumentClass;
42
43
    /**
44
     * @param Manager $searchManager
45
     * @param ReferenceStoreInterface $referenceStore
46
     * @param string $articleDocumentClass
47
     */
48 View Code Duplication
    public function __construct(
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...
49
        Manager $searchManager,
50
        ReferenceStoreInterface $referenceStore,
51
        $articleDocumentClass
52
    ) {
53
        parent::__construct('Article', []);
54
55
        $this->searchManager = $searchManager;
56
        $this->referenceStore = $referenceStore;
57
        $this->articleDocumentClass = $articleDocumentClass;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getContentData(PropertyInterface $property)
64
    {
65
        $uuid = $property->getValue();
66
67
        if (null === $uuid) {
68
            return null;
69
        }
70
71
        $repository = $this->searchManager->getRepository($this->articleDocumentClass);
72
        $locale = $property->getStructure()->getLanguageCode();
73
74
        return $repository->find($this->getViewDocumentId($uuid, $locale)) ?? null;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function preResolve(PropertyInterface $property)
81
    {
82
        $uuid = $property->getValue();
83
        if (null === $uuid) {
84
            return;
85
        }
86
87
        $this->referenceStore->add($uuid);
88
    }
89
}
90