|
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\Document\ArticleViewDocumentInterface; |
|
20
|
|
|
use Sulu\Bundle\ArticleBundle\Document\Resolver\WebspaceResolver; |
|
21
|
|
|
use Sulu\Bundle\ArticleBundle\Metadata\StructureTagTrait; |
|
22
|
|
|
use Sulu\Component\Content\Compat\StructureManagerInterface; |
|
23
|
|
|
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; |
|
24
|
|
|
use Sulu\Component\Content\Metadata\PropertyMetadata; |
|
25
|
|
|
use Sulu\Component\Localization\Manager\LocalizationManagerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Extends serialization for articles. |
|
29
|
|
|
*/ |
|
30
|
|
|
class ArticleSubscriber implements EventSubscriberInterface |
|
31
|
|
|
{ |
|
32
|
|
|
const PAGE_TITLE_TAG_NAME = 'sulu_article.page_title'; |
|
33
|
|
|
|
|
34
|
|
|
const PAGE_TITLE_PROPERTY_NAME = 'pageTitle'; |
|
35
|
|
|
|
|
36
|
|
|
use StructureTagTrait; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var StructureManagerInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $structureManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var StructureMetadataFactoryInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $structureMetadataFactory; |
|
47
|
|
|
|
|
48
|
52 |
|
/** |
|
49
|
|
|
* @var WebspaceResolver |
|
50
|
|
|
*/ |
|
51
|
|
|
private $webspaceResolver; |
|
52
|
52 |
|
|
|
53
|
52 |
|
/** |
|
54
|
52 |
|
* @var LocalizationManagerInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $localizationManager; |
|
57
|
|
|
|
|
58
|
|
|
public function __construct( |
|
59
|
1 |
|
StructureManagerInterface $structureManager, |
|
60
|
|
|
StructureMetadataFactoryInterface $structureMetadataFactory, |
|
61
|
|
|
WebspaceResolver $webspaceResolver |
|
62
|
|
|
) { |
|
63
|
1 |
|
$this->structureManager = $structureManager; |
|
64
|
1 |
|
$this->structureMetadataFactory = $structureMetadataFactory; |
|
65
|
1 |
|
$this->webspaceResolver = $webspaceResolver; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
/** |
|
69
|
1 |
|
* {@inheritdoc} |
|
70
|
1 |
|
*/ |
|
71
|
|
|
public static function getSubscribedEvents() |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
|
|
[ |
|
75
|
|
|
'event' => Events::POST_SERIALIZE, |
|
76
|
|
|
'format' => 'json', |
|
77
|
|
|
'method' => 'addTypeOnPostSerialize', |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
52 |
|
'event' => Events::POST_SERIALIZE, |
|
81
|
|
|
'format' => 'json', |
|
82
|
52 |
|
'method' => 'addWebspaceSettingsOnPostSerialize', |
|
83
|
52 |
|
], |
|
84
|
52 |
|
[ |
|
85
|
|
|
'event' => Events::POST_SERIALIZE, |
|
86
|
52 |
|
'format' => 'json', |
|
87
|
20 |
|
'method' => 'addBrokenIndicatorOnPostSerialize', |
|
88
|
|
|
], |
|
89
|
|
|
[ |
|
90
|
51 |
|
'event' => Events::POST_SERIALIZE, |
|
91
|
51 |
|
'format' => 'json', |
|
92
|
51 |
|
'method' => 'addPageTitlePropertyNameOnPostSerialize', |
|
93
|
|
|
], |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Append type to result. |
|
99
|
52 |
|
* |
|
100
|
|
|
* @param ObjectEvent $event |
|
101
|
52 |
|
*/ |
|
102
|
52 |
View Code Duplication |
public function addTypeOnPostSerialize(ObjectEvent $event) |
|
|
|
|
|
|
103
|
52 |
|
{ |
|
104
|
|
|
$article = $event->getObject(); |
|
105
|
52 |
|
$visitor = $event->getVisitor(); |
|
106
|
12 |
|
$context = $event->getContext(); |
|
107
|
|
|
|
|
108
|
|
|
if (!($article instanceof ArticleDocument)) { |
|
109
|
51 |
|
return; |
|
110
|
51 |
|
} |
|
111
|
43 |
|
|
|
112
|
|
|
$structure = $this->structureManager->getStructure($article->getStructureType(), 'article'); |
|
113
|
51 |
|
$visitor->addData('articleType', $context->accept($this->getType($structure->getStructure()))); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Append webspace-settings to result. |
|
118
|
|
|
* |
|
119
|
|
|
* @param ObjectEvent $event |
|
120
|
|
|
*/ |
|
121
|
|
|
public function addWebspaceSettingsOnPostSerialize(ObjectEvent $event) |
|
122
|
51 |
|
{ |
|
123
|
|
|
$article = $event->getObject(); |
|
124
|
51 |
|
$visitor = $event->getVisitor(); |
|
125
|
51 |
|
$context = $event->getContext(); |
|
126
|
51 |
|
|
|
127
|
|
|
if (!($article instanceof ArticleDocument)) { |
|
128
|
|
|
return; |
|
129
|
51 |
|
} |
|
130
|
7 |
|
|
|
131
|
|
|
$visitor->addData('customizeWebspaceSettings', $context->accept(null !== $article->getMainWebspace())); |
|
|
|
|
|
|
132
|
|
|
if ($article->getMainWebspace()) { |
|
|
|
|
|
|
133
|
44 |
|
return; |
|
134
|
36 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
$visitor->setData('mainWebspace', $this->webspaceResolver->resolveMainWebspace($article)); |
|
137
|
15 |
|
$visitor->setData('additionalWebspace', $this->webspaceResolver->resolveAdditionalWebspaces($article)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Append broken-indicator to result. |
|
142
|
|
|
* |
|
143
|
|
|
* @param ObjectEvent $event |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function addBrokenIndicatorOnPostSerialize(ObjectEvent $event) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$article = $event->getObject(); |
|
148
|
|
|
$visitor = $event->getVisitor(); |
|
149
|
|
|
|
|
150
|
|
|
if (!($article instanceof ArticleViewDocumentInterface)) { |
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$structure = $this->structureManager->getStructure($article->getStructureType(), 'article'); |
|
155
|
|
|
$visitor->addData('broken', !$structure || $structure->getKey() !== $article->getStructureType()); |
|
156
|
|
|
$visitor->addData('originalStructureType', $article->getStructureType()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Append page-title-property to result. |
|
161
|
|
|
* |
|
162
|
|
|
* @param ObjectEvent $event |
|
163
|
|
|
*/ |
|
164
|
|
View Code Duplication |
public function addPageTitlePropertyNameOnPostSerialize(ObjectEvent $event) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$article = $event->getObject(); |
|
167
|
|
|
$visitor = $event->getVisitor(); |
|
168
|
|
|
$context = $event->getContext(); |
|
169
|
|
|
|
|
170
|
|
|
if (!$article instanceof ArticleInterface) { |
|
171
|
|
|
return; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$property = $this->getPageTitleProperty($article); |
|
175
|
|
|
if ($property) { |
|
176
|
|
|
$visitor->addData('_pageTitlePropertyName', $context->accept($property->getName())); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Find page-title property. |
|
182
|
|
|
* |
|
183
|
|
|
* @param ArticleInterface $document |
|
184
|
|
|
* |
|
185
|
|
|
* @return PropertyMetadata |
|
186
|
|
|
*/ |
|
187
|
|
View Code Duplication |
private function getPageTitleProperty(ArticleInterface $document) |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
$metadata = $this->structureMetadataFactory->getStructureMetadata( |
|
190
|
|
|
'article', |
|
191
|
|
|
$document->getStructureType() |
|
192
|
|
|
); |
|
193
|
|
|
|
|
194
|
|
|
if ($metadata->hasPropertyWithTagName(self::PAGE_TITLE_TAG_NAME)) { |
|
195
|
|
|
return $metadata->getPropertyByTagName(self::PAGE_TITLE_TAG_NAME); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if ($metadata->hasProperty(self::PAGE_TITLE_PROPERTY_NAME)) { |
|
199
|
|
|
return $metadata->getProperty(self::PAGE_TITLE_PROPERTY_NAME); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return null; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
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.