Test Failed
Push — master ( b1cfa1...c8f509 )
by Tobias
11:38
created

AppController::actionVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace app\commands;
4
5
use dektrium\user\models\User;
6
use mikehaertl\shellcommand\Command;
7
use yii\console\Controller;
8
use yii\helpers\VarDumper;
9
10
/**
11
 * @link http://www.diemeisterei.de/
12
 *
13
 * @copyright Copyright (c) 2016 diemeisterei GmbH, Stuttgart
14
 *
15
 * For the full copyright and license information, please view the LICENSE
16
 * file that was distributed with this source code
17
 */
18
class AppController extends Controller
19
{
20
    public $defaultAction = 'version';
21
22
    /**
23
     * Shows application configuration
24
     * Note that this action shows console configuration.
25
     *
26
     * @param null $key configuration section
27
     */
28
    public function actionConfig($key = null)
0 ignored issues
show
Coding Style introduced by
actionConfig uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
    {
30
        // get config from global variable (TODO)
31
        $data = $GLOBALS['config'];
32
        if ($key) {
33
            $keys = explode('.', $key);
34
            if (isset($keys[0])) {
35
                $data = $GLOBALS['config'][$keys[0]];
36
            }
37
            if (isset($keys[1])) {
38
                $data = $GLOBALS['config'][$keys[0]][$keys[1]];
39
            }
40
        }
41
        $this->stdout(VarDumper::dumpAsString($data));
42
    }
43
44
    /**
45
     * Shows application environment variables.
46
     */
47
    public function actionEnv()
0 ignored issues
show
Coding Style introduced by
actionEnv uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
    {
49
        $env = $_ENV;
50
        ksort($env);
51
        $this->stdout(VarDumper::dumpAsString($env));
52
    }
53
54
    /**
55
     * Displays application version from APP_VERSION constant.
56
     */
57
    public function actionVersion($alias = '@app/version')
58
    {
59
        $this->stdout('Application Version: ');
60
        $this->stdout(getenv('APP_NAME').' ');
61
        $this->stdout(APP_VERSION);
62
        echo "\n";
63
    }
64
65
    /**
66
     * Initializes application.
67
     */
68
    public function actionSetup()
69
    {
70
        $this->stdout("\n");
71
        $this->stdout("phd application setup\n");
72
        $this->stdout("=====================\n");
73
        $this->stdout("Initializing application\n");
74
75
        $this->interactive = (bool)getenv('APP_INTERACTIVE');
76
77
        $this->stdout("\nDatabase\n");
78
        $this->stdout("--------\n");
79
        $this->run('db/create');
80
        $this->run('migrate/up', ['interactive' => (bool)getenv('APP_INTERACTIVE')]);
81
82
        $this->stdout("\nUser\n");
83
        $this->stdout("----\n");
84
        $adminPassword = $this->prompt(
85
            'Enter admin password',
86
            [
87
                'default' => getenv('APP_ADMIN_PASSWORD') ?: \Yii::$app->security->generateRandomString(8),
88
            ]
89
        );
90
        $this->run('user/create', [getenv('APP_ADMIN_EMAIL'), 'admin', $adminPassword]);
91
92
        $this->stdout("\n\nDone.\n");
93
    }
94
95
    /**
96
     * Clean cache, assets and audit tables
97
     */
98
    public function actionCleanup()
99
    {
100
        $this->stdout("\nCleanup\n");
101
        $this->stdout("-------\n");
102
        $this->run('cache/flush-all');
103
        $this->run('audit/cleanup', ['age' => 30, 'interactive' => (bool)getenv('APP_INTERACTIVE')]);
104
        $this->run('app/clear-assets', ['interactive' => (bool)getenv('APP_INTERACTIVE')]);
105
    }
106
107
    /**
108
     * Clear [application]/web/assets folder.
109
     */
110
    public function actionClearAssets()
111
    {
112
        $assets = \Yii::getAlias('@web/assets');
113
114
        // Matches from 7-8 char folder names, the 8. char is optional
115
        $matchRegex = '"^[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]\?[a-z0-9]$"';
116
117
        // create $cmd command
118
        $cmd = 'cd "'.$assets.'" && ls | grep -e '.$matchRegex.' | xargs rm -rf ';
119
120
        // Set command
121
        $command = new Command($cmd);
122
123
        // Prompt user
124
        $delete = $this->confirm("\nDo you really want to delete web assets?", ['default' => true]);
125
126
        if ($delete) {
127
            // Try to execute $command
128
            if ($command->execute()) {
129
                echo "Web assets have been deleted.\n\n";
130
            } else {
131
                echo "\n".$command->getError()."\n";
132
                echo $command->getStdErr();
133
            }
134
        }
135
    }
136
137
    /**
138
     * Assign role to user
139
     *
140
     * @param $roleName
141
     * @param $userName
142
     */
143
    public function actionAssign($roleName, $userName)
144
    {
145
        $userModel = new User();
146
        $user = $userModel->finder->findUserByUsername($userName);
147
        $manager = \Yii::$app->authManager;
148
        $role = $manager->getRole($roleName);
149
        $manager->assign($role, $user->id);
150
        $this->stdout('Role has been assigned');
151
        $this->stdout("\n\nDone.\n");
152
    }
153
154
    /**
155
     * Revoke role from user
156
     *
157
     * @param $roleName
158
     * @param $userName
159
     */
160
    public function actionRevoke($roleName, $userName)
161
    {
162
        $userModel = new User();
163
        $user = $userModel->finder->findUserByUsername($userName);
164
        $manager = \Yii::$app->authManager;
165
        $role = $manager->getRole($roleName);
166
        $manager->revoke($role, $user->id);
167
        $this->stdout('Role has been revoked');
168
        $this->stdout("\n\nDone.\n");
169
    }
170
}
171