|
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( |
|
|
|
|
|
|
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
|
|
|
|
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.