Completed
Pull Request — develop (#177)
by Wachter
11:57
created

ArticleSelectionContentType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getContentData() 0 23 5
A getTemplate() 0 4 1
A preResolve() 0 11 3
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\Content;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
16
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
17
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
18
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
19
use Sulu\Component\Content\Compat\PropertyInterface;
20
use Sulu\Component\Content\PreResolvableContentTypeInterface;
21
use Sulu\Component\Content\SimpleContentType;
22
23
/**
24
 * Provides article_selection content-type.
25
 */
26
class ArticleSelectionContentType extends SimpleContentType implements PreResolvableContentTypeInterface
27
{
28
    use ArticleViewDocumentIdTrait;
29
30
    /**
31
     * @var Manager
32
     */
33
    private $searchManager;
34
35
    /**
36
     * @var ReferenceStoreInterface
37
     */
38
    private $referenceStore;
39
40
    /**
41
     * @var string
42
     */
43
    private $articleDocumentClass;
44
45
    /**
46
     * @var string
47
     */
48
    private $template;
49
50
    /**
51
     * @param Manager $searchManager
52
     * @param ReferenceStoreInterface $referenceStore
53
     * @param string $articleDocumentClass
54
     * @param string $template
55
     */
56
    public function __construct(Manager $searchManager, ReferenceStoreInterface $referenceStore, $articleDocumentClass, $template)
57
    {
58
        parent::__construct('Article', []);
59
60
        $this->searchManager = $searchManager;
61
        $this->referenceStore = $referenceStore;
62
        $this->articleDocumentClass = $articleDocumentClass;
63
        $this->template = $template;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getContentData(PropertyInterface $property)
70
    {
71
        $value = $property->getValue();
72
        if ($value === null || !is_array($value) || count($value) === 0) {
73
            return [];
74
        }
75
76
        $locale = $property->getStructure()->getLanguageCode();
77
78
        $repository = $this->searchManager->getRepository($this->articleDocumentClass);
79
        $search = $repository->createSearch();
80
        $search->addQuery(new IdsQuery($this->getViewDocumentIds($value, $locale)));
81
82
        $result = [];
83
        /** @var ArticleViewDocumentInterface $articleDocument */
84
        foreach ($repository->findDocuments($search) as $articleDocument) {
85
            $result[array_search($articleDocument->getUuid(), $value, false)] = $articleDocument;
86
        }
87
88
        ksort($result);
89
90
        return array_values($result);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getTemplate()
97
    {
98
        return $this->template;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function preResolve(PropertyInterface $property)
105
    {
106
        $uuids = $property->getValue();
107
        if (!is_array($uuids)) {
108
            return;
109
        }
110
111
        foreach ($uuids as $uuid) {
112
            $this->referenceStore->add($uuid);
113
        }
114
    }
115
}
116