RbacController::beforeAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\console\{Controller, ExitCode};
8
use yii\helpers\Console;
9
10
/**
11
 * Command for update permissions
12
 * 
13
 * @see config/rbac/permissions.php
14
 */
15
class RbacController extends Controller
16
{
17
    /**
18
     * @var authManager
19
     */
20
    private $auth;
21
    /**
22
     * @var string Config file path
23
     */
24
    public $path;
25 1
26
    public function options($actionId = '')
27 1
    {
28
        return ['path'];
29
    }
30 3
31
    public function beforeAction($action)
32 3
    {
33 3
        if (parent::beforeAction($action)) {
34 1
            if (empty($this->path)) {
35
                throw new InvalidConfigException('`path` should be specified');
36
            }
37 2
        }
38
        return true;
39
    }
40 4
41
    public function init()
42 4
    {
43 4
        $this->auth = Yii::$app->authManager;
44 4
        Yii::$app->cache->delete('rbac-permissions');
45
    }
46 2
47
    public function actionUp(): int
48 2
    {
49 2
        $currentPermissions = $this->auth->getPermissions();
50
        $newPermissions = require Yii::getAlias($this->path);
51 2
52 2
        $this->cleanUnusedPermissions($currentPermissions, $newPermissions);
53
        $this->updatePermissions($currentPermissions, $newPermissions);
54 2
55 2
        $this->stdout("Done!\n", Console::FG_GREEN);
56
        return ExitCode::OK;
57 2
    }
58
59 2
    private function cleanUnusedPermissions(array $currentPermissions, array $newPermissions): void
60 2
    {
61 2
        foreach ($currentPermissions as $currentPermission) {
62
            if (!isset($newPermissions[$currentPermission->name])) {
63
                $this->auth->remove($currentPermission);
64 2
            }
65
        }
66 2
    }
67
68 2
    private function updatePermissions(array $currentPermissions, array $newPermissions): void
69 2
    {
70 2
        foreach ($newPermissions as $name => $description) {
71 1
            $isNew = !isset($currentPermissions[$name]);
72 1
            if ($isNew) {
73
                $this->add($name, $description);
74 2
                continue;
75
            }
76 2
            $this->update($name, $description);
77
        }
78 1
    }
79
80 1
    private function add(string $name, string $description): void
81 1
    {
82 1
        $permission = $this->auth->createPermission($name);
83 1
        $permission->description = $description;
84 1
        $this->auth->add($permission);
85
        $this->stdout("Added: $name\n", Console::FG_GREEN);
86 2
    }
87
88 2
    private function update(string $name, string $description): void
89 2
    {
90 2
        $permission = $this->auth->getPermission($name);
91 2
        $permission->description = $description;
92 2
        $this->auth->update($name, $permission);
93
        $this->stdout("Updated: $name\n", Console::FG_GREEN);
94
    }
95
}
96