|
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\Renderer\Helper; |
|
15
|
|
|
|
|
16
|
|
|
use WebHemi\Data\StorageInterface; |
|
17
|
|
|
use WebHemi\Data\Storage; |
|
18
|
|
|
use WebHemi\Data\Entity; |
|
19
|
|
|
use WebHemi\Environment\ServiceInterface as EnvironmentInterface; |
|
20
|
|
|
use WebHemi\Renderer\HelperInterface; |
|
21
|
|
|
use WebHemi\Router\ProxyInterface; |
|
22
|
|
|
use WebHemi\StorageTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class GetCategoriesHelper |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
class GetCategoriesHelper implements HelperInterface |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
/** @var EnvironmentInterface */ |
|
30
|
|
|
private $environmentManager; |
|
31
|
|
|
|
|
32
|
|
|
use StorageTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* GetCategoriesHelper constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param EnvironmentInterface $environmentManager |
|
38
|
|
|
* @param StorageInterface[] ...$dataStorages |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(EnvironmentInterface $environmentManager, StorageInterface ...$dataStorages) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->environmentManager = $environmentManager; |
|
43
|
|
|
$this->addStorageInstances($dataStorages); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Should return the name of the helper. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getName() : string |
|
52
|
|
|
{ |
|
53
|
|
|
return 'getCategories'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Should return the name of the helper. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getDefinition() : string |
|
62
|
|
|
{ |
|
63
|
|
|
return '{{ getCategories(order_by, limit) }}'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets helper options for the render. |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
* @codeCoverageIgnore - empty array |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getOptions() : array |
|
73
|
|
|
{ |
|
74
|
|
|
return []; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Should return a description text. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function getDescription() : string |
|
83
|
|
|
{ |
|
84
|
|
|
return 'Returns the categories for the current application.'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* A renderer helper should be called with its name. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __invoke() : array |
|
93
|
|
|
{ |
|
94
|
|
|
$categories = []; |
|
95
|
|
|
|
|
96
|
|
|
/** @var Storage\ApplicationStorage $applicationStorage */ |
|
97
|
|
|
$applicationStorage = $this->getApplicationStorage(); |
|
98
|
|
|
/** @var Storage\Filesystem\FilesystemCategoryStorage $categoryStorage */ |
|
99
|
|
|
$categoryStorage = $this->getFilesystemCategoryStorage(); |
|
100
|
|
|
/** @var Storage\Filesystem\FilesystemDirectoryStorage $directoryStorage */ |
|
101
|
|
|
$directoryStorage = $this->getFilesystemDirectoryStorage(); |
|
102
|
|
|
|
|
103
|
|
|
if (!$applicationStorage || !$categoryStorage || !$directoryStorage) { |
|
104
|
|
|
return []; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** @var Entity\ApplicationEntity $application */ |
|
108
|
|
|
$application = $applicationStorage |
|
109
|
|
|
->getApplicationByName($this->environmentManager->getSelectedApplication()); |
|
110
|
|
|
$applicationId = $application->getKeyData(); |
|
111
|
|
|
|
|
112
|
|
|
/** @var array $categoryDirectoryData */ |
|
113
|
|
|
$categoryDirectoryData = $directoryStorage |
|
114
|
|
|
->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_CATEGORY); |
|
115
|
|
|
|
|
116
|
|
|
/** @var Entity\Filesystem\FilesystemCategoryEntity[] $categoryList */ |
|
117
|
|
|
$categoryList = $categoryStorage |
|
118
|
|
|
->getFilesystemCategorysByApplication($applicationId); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($categoryList as $categoryEntity) { |
|
121
|
|
|
$categories[] = [ |
|
122
|
|
|
'path' => $categoryDirectoryData['uri'], |
|
123
|
|
|
'name' => $categoryEntity->getName(), |
|
124
|
|
|
'title' => $categoryEntity->getTitle() |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $categories; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
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.