AppController::actionClearAssets()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
3
namespace app\commands;
4
5
/**
6
 * @link http://www.diemeisterei.de/
7
 *
8
 * @copyright Copyright (c) 2014 diemeisterei GmbH, Stuttgart
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use dektrium\user\Finder;
15
use mikehaertl\shellcommand\Command;
16
use yii\console\Controller;
17
18
/**
19
 * Task runner command for development.
20
 *
21
 * @author Tobias Munk <[email protected]>
22
 */
23
class AppController extends Controller
24
{
25
    public $defaultAction = 'version';
26
27
    /**
28
     * Displays application version from git describe and writes it to `version`.
29
     */
30
    public function actionVersion($alias = '@root/version')
0 ignored issues
show
Unused Code introduced by
The parameter $alias is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        $this->stdout(\Yii::$app->id.' version '.APP_VERSION);
33
        $this->stdout("\n");
34
    }
35
36
    /**
37
     * Setup admin user (create, update password, confirm).
38
     */
39
    public function actionSetupAdminUser()
40
    {
41
        $finder = \Yii::$container->get(Finder::className());
42
        $admin = $finder->findUserByUsername('admin');
43
        if ($admin === null) {
44
            $email = $this->prompt(
45
                'E-Mail for application admin user:',
46
                ['default' => getenv('APP_ADMIN_EMAIL')]
47
            );
48
            $this->action('user/create', [$email, 'admin']);
49
            $password = $this->prompt(
50
                'Password for application admin user:',
51
                ['default' => getenv('APP_ADMIN_PASSWORD')]
52
            );
53
        } else {
54
            $password = $this->prompt(
55
                'Update password for application admin user (leave empty to skip):'
56
            );
57
        }
58
        if ($password) {
59
            $this->action('user/password', ['admin', $password]);
60
        }
61
        sleep(1); // confirmation may not succeed without a short pause
62
        $this->action('user/confirm', ['admin']);
63
    }
64
65
    /**
66
     * Clear [application]/web/assets folder.
67
     */
68
    public function actionClearAssets()
69
    {
70
        $assets = \Yii::getAlias('@web/assets');
71
72
        // Matches from 7-8 char folder names, the 8. char is optional
73
        $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]$"';
74
75
        // create $cmd command
76
        $cmd = 'cd "'.$assets.'" && ls | grep -e '.$matchRegex.' | xargs rm -rf ';
77
78
        // Set command
79
        $command = new Command($cmd);
80
81
        // Prompt user
82
        $delete = $this->confirm("\nDo you really want to delete web assets?", ['default' => true]);
0 ignored issues
show
Documentation introduced by
array('default' => true) is of type array<string,boolean,{"default":"boolean"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
        if ($delete) {
85
            // Try to execute $command
86
            if ($command->execute()) {
87
                echo "Web assets have been deleted.\n\n";
88
            } else {
89
                echo "\n".$command->getError()."\n";
90
                echo $command->getStdErr();
91
            }
92
        }
93
    }
94
95
    /**
96
     * Generate application and required vendor documentation.
97
     */
98
    public function actionGenerateDocs()
99
    {
100
        if ($this->confirm('Regenerate documentation files into ./docs-html', true)) {
101
102
            // array with commands
103
            $commands[] = 'vendor/bin/apidoc guide --interactive=0 docs web/apidocs';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$commands was never initialized. Although not strictly required by PHP, it is generally a good practice to add $commands = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
104
            $commands[] = 'vendor/bin/apidoc api --interactive=0 --exclude=runtime/,tests/,vendor/ . web/apidocs';
105
            $commands[] = 'vendor/bin/apidoc guide --interactive=0 docs web/apidocs';
106
107
            foreach ($commands as $command) {
108
                $cmd = new Command($command);
109
                if ($cmd->execute()) {
110
                    echo $cmd->getOutput();
111
                } else {
112
                    echo $cmd->getOutput();
113
                    echo $cmd->getStdErr();
114
                    echo $cmd->getError();
115
                }
116
            }
117
        }
118
    }
119
120
    protected function action($command, $params = [])
121
    {
122
        echo "\nRunning action '$command'...\n";
123
        \Yii::$app->runAction($command, $params);
124
    }
125
}
126