Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

RbacController::actionUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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