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

UserAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 109
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplateName() 0 4 1
B getTemplateData() 0 71 5
A getUser() 0 4 1
A getUserMeta() 0 4 1
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\StorageInterface;
18
use WebHemi\Data\Storage;
19
use WebHemi\Data\Entity;
20
use WebHemi\DateTime;
21
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
22
use WebHemi\Middleware\Action\Website\IndexAction;
23
use WebHemi\Middleware\Action\AbstractMiddlewareAction;
24
use WebHemi\Router\ProxyInterface;
25
use WebHemi\StorageTrait;
26
27
/**
28
 * Class UserAction
29
 */
30
class UserAction extends IndexAction
31
{
32
    /**
33
     * Gets template map name or template file path.
34
     *
35
     * @return string
36
     */
37
    public function getTemplateName() : string
38
    {
39
        return 'website-user';
40
    }
41
42
    /**
43
     * Gets template data.
44
     *
45
     * @return array
46
     */
47
    public function getTemplateData() : array
48
    {
49
        $parameters = $this->getRoutingParameters();
50
        $userName = $parameters['uri_parameter'] ?? '';
51
        $user = $this->getUser($userName);
52
        $userMeta = $this->getUserMeta((int)$user->getUserId());
53
54
        $blogPosts = [];
55
56
        /** @var Entity\ApplicationEntity $applicationEntity */
57
        $applicationEntity = $this->getApplicationStorage()
58
            ->getApplicationByName($this->environmentManager->getSelectedApplication());
59
60
        /** @var Entity\Filesystem\FilesystemEntity[] $publications */
61
        $publications = $this->getFilesystemStorage()
62
            ->getPublishedDocuments($applicationEntity->getApplicationId());
63
64
        /** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */
65
        foreach ($publications as $filesystemEntity) {
66
            /** @var Entity\Filesystem\FilesystemDocumentEntity $documentEntity */
67
            $documentEntity = $this->getFilesystemDocumentStorage()
68
                ->getFilesystemDocumentById($filesystemEntity->getDocumentId());
69
70
            // Skip publications from other users
71
            if ($documentEntity->getAuthorId() != $user->getUserId()) {
72
                continue;
73
            }
74
75
            $documentMeta = $this->getFilesystemStorage()
76
                ->getPublicationMeta($filesystemEntity->getFilesystemId());
77
78
            $author = $this->getPublicationAuthor(
79
                $documentEntity->getAuthorId(),
80
                $applicationEntity->getApplicationId()
81
            );
82
            $author['mood'] = [];
83
84
            if (isset($documentMeta['mood_key']) && isset($documentMeta['mood_name'])) {
85
                $author['mood'] = [
86
                    $documentMeta['mood_name'],
87
                    $documentMeta['mood_key']
88
                ];
89
            }
90
91
            $blogPosts[] = [
92
                'author' => $author,
93
                'tags' => $this->getPublicationTags(
94
                    $applicationEntity->getApplicationId(),
95
                    $filesystemEntity->getFilesystemId()
96
                ),
97
                'category' => $this->getPublicationCategory(
98
                    $applicationEntity->getApplicationId(),
99
                    $filesystemEntity->getCategoryId()
100
                ),
101
                'publishedAt' => $filesystemEntity->getDatePublished(),
102
                'location' => $documentMeta['location'] ?? '',
103
                'summary' => $filesystemEntity->getDescription(),
104
                'illustration' => $documentMeta['illustration'] ?? '',
105
                'path' => $this->getPublicationPath($filesystemEntity),
106
                'title' => $filesystemEntity->getTitle(),
107
                'contentLead' => $documentEntity->getContentLead(),
108
                'contentBody' => $documentEntity->getContentBody()
109
            ];
110
        }
111
112
        return [
113
            'user' => $user,
114
            'userMeta' => $userMeta,
115
            'blogPosts' => $blogPosts,
116
        ];
117
    }
118
119
    /**
120
     * @param string $userName
121
     * @return null|Entity\User\UserEntity
122
     */
123
    private function getUser(string $userName) : ? Entity\User\UserEntity
124
    {
125
        return $this->getUserStorage()->getUserByUserName($userName);
126
    }
127
128
    /**
129
     * Gets all the meta entities for a user.
130
     *
131
     * @param int $userId
132
     * @return Entity\User\UserMetaEntity[]
133
     */
134
    private function getUserMeta(int $userId) : array
135
    {
136
        return $this->getUserMetaStorage()->getUserMetaForUserId($userId, true);
137
    }
138
}
139