DashboardController::index()   B
last analyzed

Complexity

Conditions 10
Paths 113

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 113
nop 0
dl 0
loc 30
rs 7.5583
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Controller;
14
15
use App\Utility\CacheTools;
16
use App\Utility\SchemaTrait;
17
use Cake\Core\Configure;
18
use Cake\Utility\Hash;
19
20
/**
21
 * Dashboard controller.
22
 */
23
class DashboardController extends AppController
24
{
25
    use SchemaTrait;
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function initialize(): void
31
    {
32
        parent::initialize();
33
34
        // force `GET /home` reload
35
        $this->Modules->setConfig('clearHomeCache', true);
36
    }
37
38
    /**
39
     * Displays dashboard.
40
     *
41
     * @return void
42
     */
43
    public function index(): void
44
    {
45
        $this->getRequest()->allowMethod(['get']);
46
47
        /** @var \Authentication\Identity $user */
48
        $user = $this->Authentication->getIdentity();
49
        $this->set(
50
            'jobsAllow',
51
            (array)Hash::extract($this->getMeta($user), 'resources./async_jobs.hints.allow')
52
        );
53
54
        // set modules counters
55
        $counters = Configure::read('UI.modules.counters', ['trash']);
56
        if ($counters === 'none') {
57
            return;
58
        }
59
        $counters = is_array($counters) || in_array($counters, ['none', 'all']) ? $counters : ['trash'];
60
        $modules = array_keys((array)$this->viewBuilder()->getVar('modules'));
61
        $modules = $counters === 'all' ? $modules : array_intersect($modules, $counters);
62
        foreach ($modules as $name) {
63
            if (CacheTools::existsCount($name)) {
64
                continue;
65
            }
66
            $endpoint = $name === 'tags' ? '/model/tags' : $name;
67
            $options = $name === 'tags' ? ['page_size' => 1] : ['limit' => 1, 'page_size' => 1, 'fields' => 'id'];
68
            try {
69
                $response = $this->apiClient->get($endpoint, $options);
70
                CacheTools::setModuleCount($response, $name);
71
            } catch (\Exception $e) {
72
                CacheTools::setModuleCount(['meta' => ['pagination' => ['count' => '-']]], $name);
73
            }
74
        }
75
    }
76
77
    /**
78
     * Render flash messages without layout for fetch requests.
79
     *
80
     * @return void
81
     */
82
    public function messages(): void
83
    {
84
        $this->getRequest()->allowMethod(['get']);
85
        $this->viewBuilder()->disableAutoLayout();
86
        $this->render('/Element/Dashboard/messages');
87
    }
88
}
89