Passed
Push — master ( 8e073b...65ec1f )
by Gabor
03:13
created

IndexAction::getPublicationCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
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;
15
16
use WebHemi\Data\Entity;
17
use WebHemi\Data\Storage;
18
use WebHemi\Data\StorageInterface;
19
use WebHemi\Data\Traits\StorageInjectorTrait;
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
 * @method Storage\ApplicationStorage getApplicationStorage()
28
 * @method Storage\Filesystem\FilesystemCategoryStorage getFilesystemCategoryStorage()
29
 * @method Storage\Filesystem\FilesystemDirectoryStorage getFilesystemDirectoryStorage()
30
 * @method Storage\Filesystem\FilesystemDocumentStorage getFilesystemDocumentStorage()
31
 * @method Storage\Filesystem\FilesystemStorage getFilesystemStorage()
32
 * @method Storage\Filesystem\FilesystemTagStorage getFilesystemTagStorage()
33
 * @method Storage\User\UserStorage getUserStorage()
34
 * @method Storage\User\UserMetaStorage getUserMetaStorage()
35
 */
36
class IndexAction extends AbstractMiddlewareAction
37
{
38
    /** @var EnvironmentInterface */
39
    protected $environmentManager;
40
41
    use StorageInjectorTrait;
42
43
    /**
44
     * IndexAction constructor.
45
     *
46
     * @param EnvironmentInterface $environmentManager
47
     * @param StorageInterface[] ...$dataStorages
48
     */
49
    public function __construct(EnvironmentInterface $environmentManager, StorageInterface ...$dataStorages)
50
    {
51
        $this->environmentManager = $environmentManager;
52
        $this->addStorageInstances($dataStorages);
0 ignored issues
show
Documentation introduced by
$dataStorages is of type array<integer,array<inte...ata\StorageInterface>>>, but the function expects a array<integer,object<Web...Data\StorageInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55
    /**
56
     * Gets template map name or template file path.
57
     *
58
     * @return string
59
     */
60
    public function getTemplateName() : string
61
    {
62
        return 'website-index';
63
    }
64
65
    /**
66
     * Gets template data.
67
     *
68
     * @return array
69
     */
70
    public function getTemplateData() : array
71
    {
72
        $blogPosts = [];
73
74
        /** @var Entity\ApplicationEntity $applicationEntity */
75
        $applicationEntity = $this->getApplicationStorage()
76
            ->getApplicationByName($this->environmentManager->getSelectedApplication());
77
78
        /** @var Entity\Filesystem\FilesystemEntity[] $publications */
79
        $publications = $this->getFilesystemStorage()
80
            ->getPublishedDocuments($applicationEntity->getApplicationId());
81
82
        /** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */
83
        foreach ($publications as $filesystemEntity) {
84
            $blogPosts[] = $this->getBlobPostData($applicationEntity, $filesystemEntity);
85
        }
86
87
        return [
88
            'activeMenu' => '',
89
            'application' => $this->getApplicationData($applicationEntity),
90
            'fixPost' => $applicationEntity->getIntroduction(),
91
            'blogPosts' => $blogPosts,
92
        ];
93
    }
94
95
    /**
96
     * Gets application data to render.
97
     *
98
     * @param Entity\ApplicationEntity $applicationEntity
99
     * @return array
100
     */
101
    protected function getApplicationData(Entity\ApplicationEntity $applicationEntity) : array
102
    {
103
        return [
104
            'name' => $applicationEntity->getName(),
105
            'title' => $applicationEntity->getTitle(),
106
            'introduction' => $applicationEntity->getIntroduction(),
107
            'subject' => $applicationEntity->getSubject(),
108
            'description' => $applicationEntity->getDescription(),
109
            'keywords' => $applicationEntity->getKeywords(),
110
            'coptright' => $applicationEntity->getCopyright()
111
        ];
112
    }
113
114
    /**
115
     * Collets the blog post data
116
     *
117
     * @param Entity\ApplicationEntity $applicationEntity
118
     * @param Entity\Filesystem\FilesystemEntity $filesystemEntity
119
     * @return array
120
     */
121
    protected function getBlobPostData(
122
        Entity\ApplicationEntity $applicationEntity,
123
        Entity\Filesystem\FilesystemEntity $filesystemEntity
124
    ) : array {
125
        /** @var Entity\Filesystem\FilesystemDocumentEntity $documentEntity */
126
        $documentEntity = $this->getFilesystemDocumentStorage()
127
            ->getFilesystemDocumentById($filesystemEntity->getDocumentId());
128
129
        $documentMeta = $this->getFilesystemStorage()
130
            ->getPublicationMeta($filesystemEntity->getFilesystemId());
131
132
        $author = $this->getPublicationAuthor(
133
            $documentEntity->getAuthorId(),
134
            $applicationEntity->getApplicationId()
135
        );
136
137
        return [
138
            'author' => $author,
139
            'tags' => $this->getPublicationTags(
140
                $applicationEntity->getApplicationId(),
141
                $filesystemEntity->getFilesystemId()
142
            ),
143
            'category' => $this->getPublicationCategory(
144
                $applicationEntity->getApplicationId(),
145
                $filesystemEntity->getCategoryId()
146
            ),
147
            'path' => $this->getPublicationPath($filesystemEntity),
148
            'title' => $filesystemEntity->getTitle(),
149
            'summary' => $filesystemEntity->getDescription(),
150
            'contentLead' => $documentEntity->getContentLead(),
151
            'contentBody' => $documentEntity->getContentBody(),
152
            'publishedAt' => $filesystemEntity->getDatePublished(),
153
            'meta' => $documentMeta,
154
        ];
155
    }
156
157
    /**
158
     * Gets author information for a filesystem record.
159
     *
160
     * @param int $userId
161
     * @param int $applicationId
162
     * @return array
163
     */
164 View Code Duplication
    protected function getPublicationAuthor(int $userId, int $applicationId) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
165
    {
166
        /** @var Entity\User\UserEntity $user */
167
        $user = $this->getUserStorage()
168
            ->getUserById($userId);
169
170
        /** @var array $userMeta */
171
        $userMeta = $this->getUserMetaStorage()
172
            ->getUserMetaArrayForUserId($userId);
173
174
        /** @var array $userDirectoryData */
175
        $userDirectoryData = $this->getFilesystemDirectoryStorage()
176
            ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_USER);
177
178
        return [
179
            'userId' => $userId,
180
            'userName' => $user->getUserName(),
181
            'url' => $userDirectoryData['uri'].'/'.$user->getUserName(),
182
            'meta' => $userMeta,
183
        ];
184
    }
185
186
    /**
187
     * Generates the content path.
188
     *
189
     * @param Entity\Filesystem\FilesystemEntity $filesystemEntity
190
     * @return string
191
     */
192
    protected function getPublicationPath(Entity\Filesystem\FilesystemEntity $filesystemEntity) : string
193
    {
194
        $path = $filesystemEntity->getPath().'/'.$filesystemEntity->getBaseName();
195
196
        while (strpos($path, '//') !== false) {
197
            $path = str_replace('//', '/', $path);
198
        }
199
200
        return ltrim($path, '/');
201
    }
202
203
    /**
204
     * Collects all the tags for a filesystem record.
205
     *
206
     * @param int $applicationId
207
     * @param int $filesystemId
208
     * @return array
209
     */
210
    protected function getPublicationTags(int $applicationId, int $filesystemId) : array
211
    {
212
        $tags = [];
213
        /** @var Entity\Filesystem\FilesystemTagEntity[] $tagEntities */
214
        $tagEntities = $this->getFilesystemTagStorage()
215
            ->getFilesystemTagsByFilesystem($filesystemId);
216
217
        if (!empty($tagEntities)) {
218
            /** @var array $categoryDirectoryData */
219
            $categoryDirectoryData = $this->getFilesystemDirectoryStorage()
220
                ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_TAG);
221
222
            /** @var Entity\Filesystem\FilesystemTagEntity $tagEntity */
223
            foreach ($tagEntities as $tagEntity) {
224
                $tags[] = [
225
                    'url' => $categoryDirectoryData['uri'].'/'.$tagEntity->getName(),
226
                    'name' => $tagEntity->getName(),
227
                    'title' => $tagEntity->getTitle()
228
                ];
229
            }
230
        }
231
232
        return $tags;
233
    }
234
235
    /**
236
     * Gets the category for a filesystem record.
237
     *
238
     * @param int $applicationId
239
     * @param int $categoryId
240
     * @return array
241
     */
242 View Code Duplication
    protected function getPublicationCategory(int $applicationId, int $categoryId) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
243
    {
244
        /** @var Entity\Filesystem\FilesystemCategoryEntity $categoryEntity */
245
        $categoryEntity = $this->getFilesystemCategoryStorage()
246
            ->getFilesystemCategoryById($categoryId);
247
248
        /** @var array $categoryDirectoryData */
249
        $categoryDirectoryData = $this->getFilesystemDirectoryStorage()
250
            ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_CATEGORY);
251
252
        $category = [
253
            'url' => $categoryDirectoryData['uri'].'/'.$categoryEntity->getName(),
254
            'name' => $categoryEntity->getName(),
255
            'title' => $categoryEntity->getTitle()
256
        ];
257
        return $category;
258
    }
259
}
260