Test Setup Failed
Push — master ( 26e42b...c0b3e2 )
by Tobias
12:19
created

AppController::actionShowEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace app\commands;
4
5
use mikehaertl\shellcommand\Command;
6
use yii\console\Controller;
7
use yii\helpers\VarDumper;
8
9
/**
10
 * @link http://www.diemeisterei.de/
11
 *
12
 * @copyright Copyright (c) 2016 diemeisterei GmbH, Stuttgart
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code
16
 */
17
class AppController extends Controller
18
{
19
    public $defaultAction = 'version';
20
21
    /**
22
     * Shows application configuration
23
     * Note that this action shows console configuration.
24
     *
25
     * @param null $key configuration section
26
     */
27
    public function actionShowConfig($key = null)
0 ignored issues
show
Coding Style introduced by
actionShowConfig 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...
28
    {
29
        // get config from global variable (TODO)
30
        $data = $GLOBALS['config'];
31
        if ($key) {
32
            $keys = explode('.', $key);
33
            if (isset($keys[0])) {
34
                $data = $GLOBALS['config'][$keys[0]];
35
            }
36
            if (isset($keys[1])) {
37
                $data = $GLOBALS['config'][$keys[0]][$keys[1]];
38
            }
39
        }
40
        $this->stdout(VarDumper::dumpAsString($data));
41
    }
42
43
    /**
44
     * Shows application environment variables.
45
     */
46
    public function actionShowEnv()
0 ignored issues
show
Coding Style introduced by
actionShowEnv 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...
47
    {
48
        $env = $_ENV;
49
        ksort($env);
50
        $this->stdout(VarDumper::dumpAsString($env));
51
    }
52
53
    /**
54
     * Displays application version from APP_VERSION constant.
55
     */
56
    public function actionVersion($alias = '@app/version')
57
    {
58
        $this->stdout('Application Version: ');
59
        $this->stdout(getenv('APP_NAME').' ');
60
        $this->stdout(APP_VERSION);
61
        echo "\n";
62
    }
63
64
    /**
65
     * Initializes application.
66
     */
67
    public function actionSetup()
68
    {
69
        $this->stdout("\n");
70
        $this->stdout("phd application setup\n");
71
        $this->stdout("=====================\n");
72
        $this->stdout("Initializing application\n");
73
74
        $this->interactive = (bool) getenv('APP_INTERACTIVE');
75
76
        $this->stdout("\nDatabase\n");
77
        $this->stdout("--------\n");
78
        $this->run('db/create');
79
        $this->run('migrate/up', ['interactive' => (bool) getenv('APP_INTERACTIVE')]);
80
81
        $this->stdout("\nCleanup\n");
82
        $this->stdout("-------\n");
83
        $this->run('cache/flush-all');
84
        $this->run('audit/cleanup', ['age' => 30, 'interactive' => (bool) getenv('APP_INTERACTIVE')]);
85
        $this->run('app/clear-assets', ['interactive' => (bool) getenv('APP_INTERACTIVE')]);
86
87
        $this->stdout("\nUser\n");
88
        $this->stdout("----\n");
89
        $adminPassword = $this->prompt(
90
            'Enter admin password',
91
            [
92
                'default' => getenv('APP_ADMIN_PASSWORD') ?: \Yii::$app->security->generateRandomString(8),
93
            ]
94
        );
95
        $this->run('user/create', [getenv('APP_ADMIN_EMAIL'), 'admin', $adminPassword]);
96
97
        $this->stdout("\n\nDone.\n");
98
    }
99
100
    /**
101
     * Clear [application]/web/assets folder.
102
     */
103
    public function actionClearAssets()
104
    {
105
        $assets = \Yii::getAlias('@web/assets');
106
107
        // Matches from 7-8 char folder names, the 8. char is optional
108
        $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]$"';
109
110
        // create $cmd command
111
        $cmd = 'cd "'.$assets.'" && ls | grep -e '.$matchRegex.' | xargs rm -rf ';
112
113
        // Set command
114
        $command = new Command($cmd);
115
116
        // Prompt user
117
        $delete = $this->confirm("\nDo you really want to delete web assets?", ['default' => true]);
118
119
        if ($delete) {
120
            // Try to execute $command
121
            if ($command->execute()) {
122
                echo "Web assets have been deleted.\n\n";
123
            } else {
124
                echo "\n".$command->getError()."\n";
125
                echo $command->getStdErr();
126
            }
127
        }
128
    }
129
}
130