1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebHemi. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.1 |
6
|
|
|
* |
7
|
|
|
* @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com) |
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* @link http://www.gixx-web.com |
11
|
|
|
*/ |
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace WebHemi\Middleware\Action\Website; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use WebHemi\Data\Entity; |
18
|
|
|
use WebHemi\Data\Storage; |
19
|
|
|
use WebHemi\Data\Storage\StorageInterface; |
20
|
|
|
use WebHemi\Environment\ServiceInterface as EnvironmentInterface; |
21
|
|
|
use WebHemi\Middleware\Action\AbstractMiddlewareAction; |
22
|
|
|
use WebHemi\Router\ProxyInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class IndexAction. |
26
|
|
|
*/ |
27
|
|
|
class IndexAction extends AbstractMiddlewareAction |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var EnvironmentInterface |
31
|
|
|
*/ |
32
|
|
|
protected $environmentManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var StorageInterface[] |
36
|
|
|
*/ |
37
|
|
|
private $dataStorages; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* IndexAction constructor. |
41
|
|
|
* |
42
|
|
|
* @param EnvironmentInterface $environmentManager |
43
|
|
|
* @param StorageInterface[] ...$dataStorages |
44
|
|
|
*/ |
45
|
|
|
public function __construct(EnvironmentInterface $environmentManager, StorageInterface ...$dataStorages) |
46
|
|
|
{ |
47
|
|
|
$this->environmentManager = $environmentManager; |
48
|
|
|
|
49
|
|
|
foreach ($dataStorages as $storage) { |
50
|
|
|
$this->dataStorages[get_class($storage)] = $storage; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns a stored storage instance. |
56
|
|
|
* |
57
|
|
|
* @param string $storageClass |
58
|
|
|
* @return StorageInterface |
59
|
|
|
*/ |
60
|
|
|
private function getStorage(string $storageClass) : StorageInterface |
61
|
|
|
{ |
62
|
|
|
if (!isset($this->dataStorages[$storageClass])) { |
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
sprintf('Storage class reference "%s" is not defined in this class.', $storageClass), |
65
|
|
|
1000 |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->dataStorages[$storageClass]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets the application storage instance. |
74
|
|
|
* |
75
|
|
|
* @return Storage\ApplicationStorage |
76
|
|
|
*/ |
77
|
|
|
protected function getApplicationStorage() : Storage\ApplicationStorage |
78
|
|
|
{ |
79
|
|
|
/** @var Storage\ApplicationStorage $storage */ |
80
|
|
|
$storage = $this->getStorage(Storage\ApplicationStorage::class); |
81
|
|
|
|
82
|
|
|
return $storage; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets the filesystem storage instance. |
87
|
|
|
* |
88
|
|
|
* @return Storage\FilesystemStorage |
89
|
|
|
*/ |
90
|
|
|
protected function getFilesystemStorage() : Storage\FilesystemStorage |
91
|
|
|
{ |
92
|
|
|
/** @var Storage\FilesystemStorage $storage */ |
93
|
|
|
$storage = $this->getStorage(Storage\FilesystemStorage::class); |
94
|
|
|
|
95
|
|
|
return $storage; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Gets the user storage instance. |
100
|
|
|
* |
101
|
|
|
* @return Storage\UserStorage |
102
|
|
|
*/ |
103
|
|
|
protected function getUserStorage() : Storage\UserStorage |
104
|
|
|
{ |
105
|
|
|
/** @var Storage\UserStorage $storage */ |
106
|
|
|
$storage = $this->getStorage(Storage\UserStorage::class); |
107
|
|
|
|
108
|
|
|
return $storage; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets template map name or template file path. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getTemplateName() : string |
117
|
|
|
{ |
118
|
|
|
return 'website-index'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Gets template data. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function getTemplateData() : array |
127
|
|
|
{ |
128
|
|
|
$blogPosts = []; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var Entity\ApplicationEntity $applicationEntity |
132
|
|
|
*/ |
133
|
|
|
$applicationEntity = $this->getApplicationStorage() |
134
|
|
|
->getApplicationByName($this->environmentManager->getSelectedApplication()); |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var Entity\EntitySet $publications |
138
|
|
|
*/ |
139
|
|
|
$publications = $this->getFilesystemStorage() |
140
|
|
|
->getFilesystemPublishedDocumentList($applicationEntity->getApplicationId()); |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @var Entity\FilesystemPublishedDocumentEntity $publishedDocumentEntity |
144
|
|
|
*/ |
145
|
|
|
foreach ($publications as $publishedDocumentEntity) { |
146
|
|
|
$blogPosts[] = $this->getBlobPostData($applicationEntity, $publishedDocumentEntity); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return [ |
150
|
|
|
'activeMenu' => '', |
151
|
|
|
'application' => $this->getApplicationData($applicationEntity), |
152
|
|
|
'fixPost' => $applicationEntity->getIntroduction(), |
153
|
|
|
'blogPosts' => $blogPosts, |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets application data to render. |
159
|
|
|
* |
160
|
|
|
* @param Entity\ApplicationEntity $applicationEntity |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
protected function getApplicationData(Entity\ApplicationEntity $applicationEntity) : array |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
'name' => $applicationEntity->getName(), |
167
|
|
|
'title' => $applicationEntity->getTitle(), |
168
|
|
|
'introduction' => $applicationEntity->getIntroduction(), |
169
|
|
|
'subject' => $applicationEntity->getSubject(), |
170
|
|
|
'description' => $applicationEntity->getDescription(), |
171
|
|
|
'keywords' => $applicationEntity->getKeywords(), |
172
|
|
|
'coptright' => $applicationEntity->getCopyright() |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Collets the blog post data |
178
|
|
|
* |
179
|
|
|
* @param Entity\ApplicationEntity $applicationEntity |
180
|
|
|
* @param Entity\FilesystemPublishedDocumentEntity $publishedDocumentEntity |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
protected function getBlobPostData( |
184
|
|
|
Entity\ApplicationEntity $applicationEntity, |
185
|
|
|
Entity\FilesystemPublishedDocumentEntity $publishedDocumentEntity |
186
|
|
|
) : array { |
187
|
|
|
/** |
188
|
|
|
* @var array $documentMeta |
189
|
|
|
*/ |
190
|
|
|
$documentMeta = $this->getFilesystemStorage() |
191
|
|
|
->getSimpleFilesystemMetaListByFilesystem($publishedDocumentEntity->getFilesystemId()); |
192
|
|
|
|
193
|
|
|
return [ |
194
|
|
|
'author' => $this->getPublicationAuthor( |
195
|
|
|
$applicationEntity->getApplicationId(), |
196
|
|
|
$publishedDocumentEntity->getAuthorId() |
197
|
|
|
), |
198
|
|
|
'tags' => $this->getPublicationTags( |
199
|
|
|
$applicationEntity->getApplicationId(), |
200
|
|
|
$publishedDocumentEntity->getFilesystemId() |
201
|
|
|
), |
202
|
|
|
'category' => $this->getPublicationCategory( |
203
|
|
|
$applicationEntity->getApplicationId(), |
204
|
|
|
$publishedDocumentEntity->getCategoryId() |
205
|
|
|
), |
206
|
|
|
'path' => $publishedDocumentEntity->getUri(), |
207
|
|
|
'title' => $publishedDocumentEntity->getTitle(), |
208
|
|
|
'summary' => $publishedDocumentEntity->getDescription(), |
209
|
|
|
'contentLead' => $publishedDocumentEntity->getContentLead(), |
210
|
|
|
'contentBody' => $publishedDocumentEntity->getContentBody(), |
211
|
|
|
'publishedAt' => $publishedDocumentEntity->getDatePublished(), |
212
|
|
|
'meta' => $documentMeta, |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Gets author information for a filesystem record. |
218
|
|
|
* |
219
|
|
|
* @param int $applicationId |
220
|
|
|
* @param int $userId |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
protected function getPublicationAuthor(int $applicationId, int $userId) : array |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
/** |
226
|
|
|
* @var Entity\UserEntity $user |
227
|
|
|
*/ |
228
|
|
|
$user = $this->getUserStorage() |
229
|
|
|
->getUserById($userId); |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @var array $userMeta |
233
|
|
|
*/ |
234
|
|
|
$userMeta = $this->getUserStorage() |
235
|
|
|
->getSimpleUserMetaListByUser($userId); |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @var Entity\FilesystemDirectoryDataEntity $userDirectoryData |
239
|
|
|
*/ |
240
|
|
|
$userDirectoryData = $this->getFilesystemStorage() |
241
|
|
|
->getFilesystemDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_USER); |
242
|
|
|
|
243
|
|
|
return [ |
244
|
|
|
'userId' => $userId, |
245
|
|
|
'userName' => $user->getUserName(), |
246
|
|
|
'url' => $userDirectoryData->getUri().'/'.$user->getUserName(), |
247
|
|
|
'meta' => $userMeta, |
248
|
|
|
]; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Collects all the tags for a filesystem record. |
253
|
|
|
* |
254
|
|
|
* @param int $applicationId |
255
|
|
|
* @param int $filesystemId |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function getPublicationTags(int $applicationId, int $filesystemId) : array |
259
|
|
|
{ |
260
|
|
|
$tags = []; |
261
|
|
|
/** |
262
|
|
|
* @var Entity\EntitySet $tagEntities |
263
|
|
|
*/ |
264
|
|
|
$tagEntities = $this->getFilesystemStorage() |
265
|
|
|
->getFilesystemTagListByFilesystem($filesystemId); |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @var Entity\FilesystemDirectoryDataEntity $categoryDirectoryData |
269
|
|
|
*/ |
270
|
|
|
$categoryDirectoryData = $this->getFilesystemStorage() |
271
|
|
|
->getFilesystemDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_TAG); |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @var Entity\FilesystemTagEntity $tagEntity |
275
|
|
|
*/ |
276
|
|
|
foreach ($tagEntities as $tagEntity) { |
277
|
|
|
$tags[] = [ |
278
|
|
|
'url' => $categoryDirectoryData->getUri().'/'.$tagEntity->getName(), |
279
|
|
|
'name' => $tagEntity->getName(), |
280
|
|
|
'title' => $tagEntity->getTitle() |
281
|
|
|
]; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $tags; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Gets the category for a filesystem record. |
289
|
|
|
* |
290
|
|
|
* @param int $applicationId |
291
|
|
|
* @param int $categoryId |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
|
View Code Duplication |
protected function getPublicationCategory(int $applicationId, int $categoryId) : array |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
/** |
297
|
|
|
* @var Entity\FilesystemCategoryEntity $categoryEntity |
298
|
|
|
*/ |
299
|
|
|
$categoryEntity = $this->getFilesystemStorage() |
300
|
|
|
->getFilesystemCategoryById($categoryId); |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @var Entity\FilesystemDirectoryDataEntity $categoryDirectoryData |
304
|
|
|
*/ |
305
|
|
|
$categoryDirectoryData = $this->getFilesystemStorage() |
306
|
|
|
->getFilesystemDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_CATEGORY); |
307
|
|
|
|
308
|
|
|
$category = [ |
309
|
|
|
'url' => $categoryDirectoryData->getUri().'/'.$categoryEntity->getName(), |
310
|
|
|
'name' => $categoryEntity->getName(), |
311
|
|
|
'title' => $categoryEntity->getTitle() |
312
|
|
|
]; |
313
|
|
|
return $category; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
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.