Passed
Push — master ( c12552...22ad80 )
by Gabor
04:56
created

IndexAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\StorageInterface;
17
use WebHemi\Data\Storage;
18
use WebHemi\Data\Entity;
19
use WebHemi\DateTime;
20
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
21
use WebHemi\Middleware\Action\AbstractMiddlewareAction;
22
use WebHemi\Router\ProxyInterface;
23
use WebHemi\StorageTrait;
24
25
/**
26
 * Class IndexAction.
27
 */
28
class IndexAction extends AbstractMiddlewareAction
29
{
30
    /** @var EnvironmentInterface */
31
    protected $environmentManager;
32
33
    use StorageTrait;
34
35
    /**
36
     * IndexAction constructor.
37
     *
38
     * @param EnvironmentInterface $environmentManager
39
     * @param StorageInterface[] ...$dataStorages
40
     */
41
    public function __construct(EnvironmentInterface $environmentManager, StorageInterface ...$dataStorages)
42
    {
43
        $this->environmentManager = $environmentManager;
44
        $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...
45
    }
46
47
    /**
48
     * Gets template map name or template file path.
49
     *
50
     * @return string
51
     */
52
    public function getTemplateName() : string
53
    {
54
        return 'website-index';
55
    }
56
57
    /**
58
     * Gets template data.
59
     *
60
     * @return array
61
     */
62
    public function getTemplateData() : array
63
    {
64
        $blogPosts = [];
65
66
        /** @var Entity\ApplicationEntity $applicationEntity */
67
        $applicationEntity = $this->getApplicationStorage()
68
            ->getApplicationByName($this->environmentManager->getSelectedApplication());
69
70
        /** @var Entity\Filesystem\FilesystemEntity[] $publications */
71
        $publications = $this->getFilesystemStorage()
72
            ->getPublishedDocuments($applicationEntity->getApplicationId());
73
74
        /** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */
75
        foreach ($publications as $filesystemEntity) {
76
            /** @var Entity\Filesystem\FilesystemDocumentEntity $documentEntity */
77
            $documentEntity = $this->getFilesystemDocumentStorage()
78
                ->getFilesystemDocumentById($filesystemEntity->getDocumentId());
79
80
            $documentMeta = $this->getFilesystemStorage()
81
                ->getPublicationMeta($filesystemEntity->getFilesystemId());
82
83
            $author = $this->getPublicationAuthor(
84
                $documentEntity->getAuthorId(),
85
                $applicationEntity->getApplicationId()
86
            );
87
            $author['mood'] = [];
88
89
            if (isset($documentMeta['mood_key']) && isset($documentMeta['mood_name'])) {
90
                $author['mood'] = [
91
                    $documentMeta['mood_name'],
92
                    $documentMeta['mood_key']
93
                ];
94
            }
95
96
            $blogPosts[] = [
97
                'author' => $author,
98
                'tags' => $this->getPublicationTags(
99
                    $applicationEntity->getApplicationId(),
100
                    $filesystemEntity->getFilesystemId()
101
                ),
102
                'category' => $this->getPublicationCategory(
103
                    $applicationEntity->getApplicationId(),
104
                    $filesystemEntity->getCategoryId()
105
                ),
106
                'publishedAt' => $filesystemEntity->getDatePublished(),
107
                'location' => $documentMeta['location'] ?? '',
108
                'summary' => $filesystemEntity->getDescription(),
109
                'illustration' => $documentMeta['illustration'] ?? '',
110
                'path' => $this->getPublicationPath($filesystemEntity),
111
                'title' => $filesystemEntity->getTitle(),
112
                'contentLead' => $documentEntity->getContentLead(),
113
                'contentBody' => $documentEntity->getContentBody()
114
            ];
115
        }
116
117
        return [
118
            'activeMenu' => '',
119
            'blogPosts' => $blogPosts,
120
            'fixPost' => $applicationEntity->getIntroduction(),
121
        ];
122
    }
123
124
    /**
125
     * Generates the content path.
126
     *
127
     * @param Entity\Filesystem\FilesystemEntity $filesystemEntity
128
     * @return string
129
     */
130
    protected function getPublicationPath(Entity\Filesystem\FilesystemEntity $filesystemEntity) : string
131
    {
132
        $path = $filesystemEntity->getPath().'/'.$filesystemEntity->getBaseName();
133
134
        if (strpos($path, '//') !== false) {
135
            $path = str_replace('//', '/', $path);
136
        }
137
138
        return $path;
139
    }
140
141
    /**
142
     * Collects all the tags for a filesystem record.
143
     *
144
     * @param int $applicationId
145
     * @param int $filesystemId
146
     * @return array
147
     */
148
    protected function getPublicationTags(int $applicationId, int $filesystemId) : array
149
    {
150
        $tags = [];
151
        /** @var Entity\Filesystem\FilesystemTagEntity[] $tagEntities */
152
        $tagEntities = $this->getFilesystemTagStorage()
153
            ->getFilesystemTagsByFilesystem($filesystemId);
154
155
        if ($tagEntities) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tagEntities of type WebHemi\Data\Entity\File...m\FilesystemTagEntity[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
156
            /** @var array $categoryDirectoryData */
157
            $categoryDirectoryData = $this->getFilesystemDirectoryStorage()
158
                ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_TAG);
159
160
            /** @var Entity\Filesystem\FilesystemTagEntity $tagEntity */
161
            foreach ($tagEntities as $tagEntity) {
162
                $tags[] = [
163
                    'url' => $categoryDirectoryData['uri'].'/'.$tagEntity->getName(),
164
                    'title' => $tagEntity->getTitle()
165
                ];
166
            }
167
        }
168
169
        return $tags;
170
    }
171
172
    /**
173
     * Gets the category for a filesystem record.
174
     *
175
     * @param int $applicationId
176
     * @param int $categoryId
177
     * @return array
178
     */
179
    protected function getPublicationCategory(int $applicationId, int $categoryId) : array
180
    {
181
        /** @var Entity\Filesystem\FilesystemCategoryEntity $categoryEntity */
182
        $categoryEntity = $this->getFilesystemCategoryStorage()
183
            ->getFilesystemCategoryById($categoryId);
184
185
        /** @var array $categoryDirectoryData */
186
        $categoryDirectoryData = $this->getFilesystemDirectoryStorage()
187
            ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_CATEGORY);
188
189
        $category = [
190
            'url' => $categoryDirectoryData['uri'].'/'.$categoryEntity->getName(),
191
            'title' => $categoryEntity->getTitle()
192
        ];
193
        return $category;
194
    }
195
196
    /**
197
     * Gets author information for a filesystem record.
198
     *
199
     * @param int $userId
200
     * @param int $applicationId
201
     * @return array
202
     */
203
    protected function getPublicationAuthor(int $userId, int $applicationId) : array
204
    {
205
        $user = $this->getUserStorage()
206
            ->getUserById($userId);
207
208
        $userMeta = $this->getUserMetaStorage()
209
            ->getUserMetaSetForUserId($userId);
210
211
        /** @var array $userDirectoryData */
212
        $userDirectoryData = $this->getFilesystemDirectoryStorage()
213
            ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_USER);
214
215
        return [
216
            'user_id' => $userId,
217
            'username' => $user->getUserName(),
218
            'url' => $userDirectoryData['uri'].'/'.$user->getUserName(),
219
            'name' => $userMeta['display_name'] ?? $user->getUserName(),
220
            'email' => ($userMeta['email_visible'] ?? 0) ? $user->getEmail() : '',
221
            'avatar' => ($userMeta['avatar_type'] ?? '') == 'gravatar'
222
                ? 'http://www.gravatar.com/avatar/'.md5(strtolower($userMeta['avatar'])).'?s=256&r=g'
223
                : $userMeta['avatar'] ?? ''
224
        ];
225
    }
226
}
227