Passed
Push — master ( 0456bd...25822b )
by Mihail
07:10
created

ActionIndex::index()   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 47
nc 40
nop 0
dl 0
loc 69
rs 8.223
c 0
b 0
f 0

How to fix   Long Method   

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
namespace Apps\Controller\Admin\Main;
4
5
use Apps\Model\Install\Main\EntityCheck;
6
use Extend\Version;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Helper\Environment;
10
use Ffcms\Core\Helper\FileSystem\Directory;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
use Ffcms\Yandex\Metrika\Client;
14
15
/**
16
 * Trait ActionIndex
17
 * @package Apps\Controller\Admin\Main
18
 * @property Request $request
19
 * @property Response $response
20
 * @property View $view
21
 */
22
trait ActionIndex
23
{
24
    /**
25
     * Index page of admin dashboard
26
     * @return string|null
27
     * @throws \Psr\Cache\InvalidArgumentException
28
     */
29
    public function index(): ?string
30
    {
31
        // get cached statistics
32
        $rootSize = App::$Cache->getItem('root.size');
33
        $loadAvg = App::$Cache->getItem('load.avarage');
34
        if (!$rootSize->isHit()) {
35
            $calcSize = round(Directory::size('/') / (1024*1000), 2) . ' mb';
36
            $rootSize->set($calcSize);
37
            $rootSize->expiresAfter(86400);
38
            App::$Cache->save($rootSize);
39
        }
40
        if (!$loadAvg->isHit()) {
41
            $loadAvg->set(Environment::loadAverage());
42
            $loadAvg->expiresAfter(300);
43
            App::$Cache->save($loadAvg);
44
        }
45
46
        // prepare system statistic
47
        $stats = [
48
            'ff_version' => Version::VERSION . ' (' . Version::DATE . ')',
49
            'php_version' => Environment::phpVersion() . ' (' . Environment::phpSAPI() . ')',
50
            'os_name' => Environment::osName(),
51
            'database_name' => App::$Database->connection()->getDatabaseName() . ' (' . App::$Database->connection()->getDriverName() . ')',
52
            'file_size' => $rootSize->get(),
53
            'load_avg' => $loadAvg->get()
54
        ];
55
        // check directory chmods and other environment features
56
        $model = new EntityCheck();
57
58
        // work with yandex statistics
59
        $yandexCfg = App::$Properties->getAll('Yandex');
60
        $tokenLifetime = $yandexCfg['oauth']['expires'];
61
        $tokenActive = ($tokenLifetime && time() < $tokenLifetime);
62
63
        $visits = null;
64
        $sources = null;
65
        if ($tokenActive) {
66
            // initialize yandex.api client
67
            $client = new Client($yandexCfg['oauth']['token'], $yandexCfg['metrika']['id']);
68
69
            // get visit statistics
70
            $visits = App::$Cache->getItem('metrika.visits');
71
            if (!$visits->isHit()) {
72
                $calcVisit = $client->getVisits30days();
0 ignored issues
show
Bug introduced by zenn
The method getVisits30days() does not exist on Ffcms\Yandex\Metrika\Client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
                /** @scrutinizer ignore-call */ 
73
                $calcVisit = $client->getVisits30days();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
                $visits->set($calcVisit)
74
                    ->expiresAfter(3600); // update 1 times at hour
75
                App::$Cache->save($visits);
76
            }
77
            $visits = $visits->get();
78
79
            // get source distribution statistics
80
            $sources = App::$Cache->getItem('metrika.sources');
81
            if (!$sources->isHit()) {
82
                $calcSources = $client->getSourcesSummary30days();
0 ignored issues
show
Bug introduced by zenn
The method getSourcesSummary30days() does not exist on Ffcms\Yandex\Metrika\Client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                /** @scrutinizer ignore-call */ 
83
                $calcSources = $client->getSourcesSummary30days();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
                $sources->set($calcSources)
84
                    ->expiresAfter(3600);
85
                App::$Cache->save($sources);
86
            }
87
            $sources = array_slice($sources->get(), 0, 5);
88
        }
89
90
        // render view output
91
        return $this->view->render('main/index', [
92
            'stats' => $stats,
93
            'check' => $model,
94
            'tokenActive' => $tokenActive,
95
            'yandexCfg' => $yandexCfg,
96
            'visits' => $visits,
97
            'sources' => $sources
98
        ]);
99
    }
100
}
101