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
use Exception;
20
21
/**
22
 * Dashboard controller.
23
 */
24
class DashboardController extends AppController
25
{
26
    use SchemaTrait;
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function initialize(): void
32
    {
33
        parent::initialize();
34
35
        // force `GET /home` reload
36
        $this->Modules->setConfig('clearHomeCache', true);
37
    }
38
39
    /**
40
     * Displays dashboard.
41
     *
42
     * @return void
43
     */
44
    public function index(): void
45
    {
46
        $this->getRequest()->allowMethod(['get']);
47
48
        /** @var \Authentication\Identity $user */
49
        $user = $this->Authentication->getIdentity();
50
        $this->set(
51
            'jobsAllow',
52
            (array)Hash::extract($this->getMeta($user), 'resources./async_jobs.hints.allow'),
53
        );
54
55
        // set modules counters
56
        $counters = Configure::read('UI.modules.counters', ['trash']);
57
        if ($counters === 'none') {
58
            return;
59
        }
60
        $counters = is_array($counters) || in_array($counters, ['none', 'all']) ? $counters : ['trash'];
61
        $modules = array_keys((array)$this->viewBuilder()->getVar('modules'));
62
        $modules = $counters === 'all' ? $modules : array_intersect($modules, $counters);
63
        foreach ($modules as $name) {
64
            if (CacheTools::existsCount($name)) {
65
                continue;
66
            }
67
            $endpoint = $name === 'tags' ? '/model/tags' : $name;
68
            $options = $name === 'tags' ? ['page_size' => 1] : ['limit' => 1, 'page_size' => 1, 'fields' => 'id'];
69
            try {
70
                $response = $this->apiClient->get($endpoint, $options);
71
                CacheTools::setModuleCount($response, $name);
72
            } catch (Exception $e) {
73
                CacheTools::setModuleCount(['meta' => ['pagination' => ['count' => '-']]], $name);
74
            }
75
        }
76
    }
77
78
    /**
79
     * Render flash messages without layout for fetch requests.
80
     *
81
     * @return void
82
     */
83
    public function messages(): void
84
    {
85
        $this->getRequest()->allowMethod(['get']);
86
        $this->viewBuilder()->disableAutoLayout();
87
        $this->render('/Element/Dashboard/messages');
88
    }
89
}
90