BaseRouting   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 65
c 3
b 0
f 0
dl 0
loc 228
rs 10
wmc 30

17 Methods

Rating   Name   Duplication   Size   Complexity  
A dashboardRouting() 0 11 3
A valuelistsRouting() 0 4 2
A imageRouting() 0 4 2
A apiRouting() 0 5 1
A imagesApiRouting() 0 6 2
A sitemapRouting() 0 4 2
A documentsApiRouting() 0 6 2
A __construct() 0 5 1
A route() 0 13 1
A documentRouting() 0 4 2
A filesRouting() 0 4 2
A logOffRouting() 0 8 2
A configurationRouting() 0 4 2
A setUserRights() 0 3 1
A redirectRouting() 0 4 2
A searchRouting() 0 3 1
A filesApiRouting() 0 6 2
1
<?php
2
/**
3
 * User: Jens
4
 * Date: 4-9-2017
5
 * Time: 20:18
6
 */
7
8
namespace CloudControl\Cms\components\cms;
9
10
11
use CloudControl\Cms\cc\Request;
12
use CloudControl\Cms\cc\ResponseHeaders;
13
use CloudControl\Cms\components\CmsComponent;
14
use CloudControl\Cms\search\Search;
15
16
class BaseRouting extends CmsRouting
17
{
18
    protected $userRights;
19
    /**
20
     * @var Request
21
     */
22
    protected $request;
23
    /**
24
     * @var string
25
     */
26
    protected $relativeCmsUri;
27
    /**
28
     * @var CmsComponent
29
     */
30
    protected $cmsComponent;
31
32
    /**
33
     * CmsRouting constructor.
34
     *
35
     * @param Request $request
36
     * @param string $relativeCmsUri
37
     * @param CmsComponent $cmsComponent
38
     */
39
    public function __construct(Request $request, $relativeCmsUri, CmsComponent $cmsComponent)
40
    {
41
        $this->request = $request;
42
        $this->relativeCmsUri = $relativeCmsUri;
43
        $this->cmsComponent = $cmsComponent;
44
    }
45
46
    /**
47
     * @param $userRights
48
     */
49
    public function setUserRights($userRights)
50
    {
51
        $this->userRights = $userRights;
52
    }
53
54
    /**
55
     * Call the different routing methods
56
     * @throws \Exception
57
     */
58
    public function route()
59
    {
60
        $this->dashboardRouting($this->relativeCmsUri);
61
        $this->logOffRouting($this->request, $this->relativeCmsUri);
62
        $this->apiRouting($this->relativeCmsUri);
63
        $this->documentRouting($this->userRights, $this->relativeCmsUri);
64
        $this->valuelistsRouting($this->userRights, $this->relativeCmsUri);
65
        $this->sitemapRouting($this->userRights, $this->relativeCmsUri);
66
        $this->redirectRouting($this->userRights, $this->relativeCmsUri);
67
        $this->imageRouting($this->userRights, $this->relativeCmsUri);
68
        $this->filesRouting($this->userRights, $this->relativeCmsUri);
69
        $this->configurationRouting($this->userRights, $this->relativeCmsUri);
70
        $this->searchRouting($this->relativeCmsUri);
71
    }
72
73
    /**
74
     * @param string $relativeCmsUri
75
     * @throws \Exception
76
     */
77
    protected function dashboardRouting($relativeCmsUri)
78
    {
79
        if ($relativeCmsUri === '' || $relativeCmsUri === '/') {
80
            $this->cmsComponent->subTemplate = 'dashboard';
81
            $this->cmsComponent->setParameter('activityLog',
82
                $this->cmsComponent->storage->getActivityLog()->getActivityLog());
83
            $documentCount = $this->cmsComponent->storage->getDocuments()->getTotalDocumentCount();
84
            $indexer = new Search($this->cmsComponent->storage);
85
            $indexedDocuments = $indexer->getIndexedDocuments();
86
            $this->cmsComponent->setParameter(CmsConstants::PARAMETER_SEARCH_NEEDS_UPDATE,
87
                $documentCount !== $indexedDocuments);
88
        }
89
    }
90
91
    /**
92
     * @param Request $request
93
     * @param string $relativeCmsUri
94
     */
95
    protected function logOffRouting($request, $relativeCmsUri)
96
    {
97
        if ($relativeCmsUri === '/log-off') {
98
            $this->cmsComponent->storage->getActivityLog()->add('logged off', 'user');
99
            $_SESSION[CmsConstants::SESSION_PARAMETER_CLOUD_CONTROL] = null;
100
            unset($_SESSION[CmsConstants::SESSION_PARAMETER_CLOUD_CONTROL]);
101
            header('Location: ' . $request::$subfolders . $this->cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX));
102
            exit;
103
        }
104
    }
105
106
    /**
107
     * @param string $relativeCmsUri
108
     * @throws \Exception
109
     */
110
    protected function apiRouting($relativeCmsUri)
111
    {
112
        $this->imagesApiRouting($relativeCmsUri);
113
        $this->filesApiRouting($relativeCmsUri);
114
        $this->documentsApiRouting($relativeCmsUri);
115
    }
116
117
    /**
118
     * @param $userRights
119
     * @param string $relativeCmsUri
120
     * @throws \Exception
121
     */
122
    protected function documentRouting($userRights, $relativeCmsUri)
123
    {
124
        if (in_array(CmsConstants::PARAMETER_DOCUMENTS, $userRights, true)) {
125
            new DocumentRouting($this->request, $relativeCmsUri, $this->cmsComponent);
126
        }
127
    }
128
129
    /**
130
     * @param $userRights
131
     * @param string $relativeCmsUri
132
     * @throws \Exception
133
     */
134
    protected function valuelistsRouting($userRights, $relativeCmsUri)
135
    {
136
        if (in_array(CmsConstants::PARAMETER_VALUELISTS, $userRights, true)) {
137
            new ValuelistRouting($this->request, $relativeCmsUri, $this->cmsComponent);
138
        }
139
    }
140
141
    /**
142
     * @param $userRights
143
     * @param string $relativeCmsUri
144
     * @throws \Exception
145
     */
146
    protected function sitemapRouting($userRights, $relativeCmsUri)
147
    {
148
        if (in_array(CmsConstants::PARAMETER_SITEMAP, $userRights, true)) {
149
            new SitemapRouting($this->request, $relativeCmsUri, $this->cmsComponent);
150
        }
151
    }
152
153
    /**
154
     * @param $userRights
155
     * @param string $relativeCmsUri
156
     * @throws \Exception
157
     */
158
    protected function redirectRouting($userRights, $relativeCmsUri)
159
    {
160
        if (in_array(CmsConstants::PARAMETER_SITEMAP, $userRights, true)) {
161
            new RedirectRouting($this->request, $relativeCmsUri, $this->cmsComponent);
162
        }
163
    }
164
165
    /**
166
     * @param $userRights
167
     * @param string $relativeCmsUri
168
     * @throws \Exception
169
     */
170
    protected function imageRouting($userRights, $relativeCmsUri)
171
    {
172
        if (in_array(CmsConstants::PARAMETER_IMAGES, $userRights, true)) {
173
            new ImagesRouting($this->request, $relativeCmsUri, $this->cmsComponent);
174
        }
175
    }
176
177
    /**
178
     * @param $userRights
179
     * @param string $relativeCmsUri
180
     * @throws \Exception
181
     */
182
    protected function filesRouting($userRights, $relativeCmsUri)
183
    {
184
        if (in_array(CmsConstants::PARAMETER_FILES, $userRights, true)) {
185
            new FilesRouting($this->request, $relativeCmsUri, $this->cmsComponent);
186
        }
187
    }
188
189
    /**
190
     * @param $userRights
191
     * @param string $relativeCmsUri
192
     * @throws \Exception
193
     */
194
    protected function configurationRouting($userRights, $relativeCmsUri)
195
    {
196
        if (in_array(CmsConstants::PARAMETER_CONFIGURATION, $userRights, true)) {
197
            new ConfigurationRouting($this->request, $relativeCmsUri, $this->cmsComponent);
198
        }
199
    }
200
201
    /**
202
     * @param string $relativeCmsUri
203
     * @throws \Exception
204
     */
205
    protected function searchRouting($relativeCmsUri)
206
    {
207
        new SearchRouting($this->request, $relativeCmsUri, $this->cmsComponent);
208
    }
209
210
    /**
211
     * @param $relativeCmsUri
212
     */
213
    protected function imagesApiRouting($relativeCmsUri)
214
    {
215
        if ($relativeCmsUri === '/images.json') {
216
            ResponseHeaders::add(ResponseHeaders::HEADER_CONTENT_TYPE, ResponseHeaders::HEADER_CONTENT_TYPE_CONTENT_APPLICATION_JSON);
217
            ResponseHeaders::sendAllHeaders();
218
            die(json_encode($this->cmsComponent->storage->getImages()->getImages()));
219
        }
220
    }
221
222
    /**
223
     * @param $relativeCmsUri
224
     */
225
    protected function filesApiRouting($relativeCmsUri)
226
    {
227
        if ($relativeCmsUri === '/files.json') {
228
            ResponseHeaders::add(ResponseHeaders::HEADER_CONTENT_TYPE, ResponseHeaders::HEADER_CONTENT_TYPE_CONTENT_APPLICATION_JSON);
229
            ResponseHeaders::sendAllHeaders();
230
            die(json_encode($this->cmsComponent->storage->getFiles()->getFiles()));
231
        }
232
    }
233
234
    /**
235
     * @param $relativeCmsUri
236
     * @throws \Exception
237
     */
238
    protected function documentsApiRouting($relativeCmsUri)
239
    {
240
        if ($relativeCmsUri === '/documents.json') {
241
            ResponseHeaders::add(ResponseHeaders::HEADER_CONTENT_TYPE, ResponseHeaders::HEADER_CONTENT_TYPE_CONTENT_APPLICATION_JSON);
242
            ResponseHeaders::sendAllHeaders();
243
            die(json_encode($this->cmsComponent->storage->getDocuments()->getDocuments()));
244
        }
245
    }
246
}