1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebHemi. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.1 |
6
|
|
|
* |
7
|
|
|
* @copyright 2012 - 2017 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\Directory; |
15
|
|
|
|
16
|
|
|
use RuntimeException; |
17
|
|
|
use WebHemi\Data\Entity; |
18
|
|
|
use WebHemi\Middleware\Action\Website\IndexAction; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class CategoryAction. |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
class CategoryAction extends IndexAction |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $templateName = 'website-post-list'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Gets template map name or template file path. |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getTemplateName() : string |
34
|
|
|
{ |
35
|
|
|
return $this->templateName; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Gets template data. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getTemplateData() : array |
44
|
|
|
{ |
45
|
|
|
$blogPosts = []; |
46
|
|
|
$parameters = $this->getRoutingParameters(); |
47
|
|
|
/** @var string $category */ |
48
|
|
|
$category = $parameters['uri_parameter'] ?? null; |
49
|
|
|
|
50
|
|
|
if (!$category) { |
51
|
|
|
throw new RuntimeException('Forbidden', 403); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @var Entity\ApplicationEntity $applicationEntity */ |
55
|
|
|
$applicationEntity = $this->getApplicationStorage() |
56
|
|
|
->getApplicationByName($this->environmentManager->getSelectedApplication()); |
57
|
|
|
|
58
|
|
|
$categoryEntity = $this->getFilesystemCategoryStorage() |
59
|
|
|
->getFilesystemCategoryByApplicationAndName( |
60
|
|
|
$applicationEntity->getApplicationId(), |
61
|
|
|
$category |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
if (!$categoryEntity) { |
65
|
|
|
throw new RuntimeException('Not Found', 404); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @var Entity\Filesystem\FilesystemEntity[] $publications */ |
69
|
|
|
$publications = $this->getFilesystemStorage() |
70
|
|
|
->getPublishedDocuments( |
71
|
|
|
$applicationEntity->getApplicationId(), |
72
|
|
|
[ |
73
|
|
|
'fk_category = ?' => (int)$categoryEntity->getFilesystemCategoryId(), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if (!$publications) { |
|
|
|
|
78
|
|
|
$this->templateName = 'website-post-list-empty'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */ |
82
|
|
|
foreach ($publications as $filesystemEntity) { |
83
|
|
|
$blogPosts[] = $this->getBlobPostData($applicationEntity, $filesystemEntity); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return [ |
87
|
|
|
'page' => [ |
88
|
|
|
'title' => $categoryEntity->getTitle(), |
89
|
|
|
'type' => 'Categories', |
90
|
|
|
], |
91
|
|
|
'activeMenu' => $category, |
92
|
|
|
'blogPosts' => $blogPosts, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.