GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FlushCacheAction::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\backend\actions;
4
5
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
6
use yii\base\Action;
7
use yii\base\Module;
8
use yii\caching\Cache;
9
10
class FlushCacheAction extends Action
11
{
12
13
    public $view = 'message';
14
15
    /**
16
     * Recursive flush all app cache
17
     * @param null|Module $current Current Module
18
     * @return string execute message
19
     */
20
    protected function flushCache(Module $current = null)
21
    {
22
        $message = '';
23
        if ($current === null) {
24
            $current = \Yii::$app;
25
        }
26
        $modules = $current->getModules();
27
        foreach ($modules as $moduleName => $module) {
28
            if (is_array($module)) {
29
                $module = $current->getModule($moduleName, true);
30
            }
31
            if ($module instanceof Module) {
32
                $message .= $this->flushCache($module);
33
            }
34
        }
35
        $components = $current->getComponents();
36
        foreach ($components as $componentName => $component) {
37
            if (is_array($component)) {
38
                $component = $current->get($componentName);
39
            }
40
            if ($component instanceof Cache) {
41
                $message .= $component->flush() ?
42
                    '<p>' . \Yii::t(
43
                        'app',
44
                        '{currentModuleName} {componentName} is flushed',
45
                        [
46
                            'currentModuleName' => $current->className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
                            'componentName' => $component->className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
48
                        ]
49
                    ) . '</p>' :
50
                    '';
51
            }
52
        }
53
        return $message;
54
    }
55
56
    /**
57
     * Flush webroot/assets/
58
     * @return string execute message
59
     */
60
    protected function flushAssets()
61
    {
62
        $message = '';
63
        $except = [\Yii::getAlias('@webroot/assets/.gitignore'), \Yii::getAlias('@webroot/assets/index.html')];
64
        $dir = \Yii::getAlias('@webroot/assets');
65
        $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
66
        /* @var RecursiveDirectoryIterator[] $files */
67
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
68
        $hasErrors = false;
69
        if (stristr(PHP_OS, 'WIN') === false) {
70
            foreach ($files as $file) {
71
                if (!in_array($file->getRealPath(), $except)) {
72
                    if ($file->isDir() && $file->isLink() === false) {
73
                        $result = @rmdir($file->getRealPath());
74
                    } elseif ($file->isLink() === true) {
75
                        $result = @unlink($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename());
76
                    } else {
77
                        $result = @unlink($file->getRealPath());
78
                    }
79
                    if (!$result) {
80
                        $hasErrors = true;
81
                    }
82
                }
83
            }
84
        }
85
        $message .= $hasErrors
86
            ? '<p>' . \Yii::t('app', 'Some assets are not flushed') . '</p>'
87
            : '<p>' . \Yii::t('app', 'Assets are flushed') . '</p>';
88
        return $message;
89
    }
90
91
    public function run()
92
    {
93
        $message = $this->flushCache();
94
        $message .= $this->flushAssets();
95
        return $this->controller->renderPartial($this->view, ['message' => $message]);
96
    }
97
}