Completed
Push — master ( 25f226...cc0a72 )
by Morgan
03:23
created

DeployInitCommand::fire()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 63
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 7.0022

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 63
ccs 27
cts 28
cp 0.9643
rs 7.2689
cc 7
eloc 35
nc 8
nop 0
crap 7.0022

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Morphatic\AutoDeploy\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
8
class DeployInitCommand extends Command
9
{
10
    /**
11
     * The options available with this command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'deploy:init
16
                            {--show : Only display the current values (do NOT use with --force)}
17
                            {--force : Force current values to be overwritten}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Generate a secret key and route name for automated deployments';
25
26
    /**
27
     * Execute the console command.
28
     */
29 10
    public function handle()
30
    {
31
        // Set necessary variables
32 10
        $path = base_path('.env');
33 10
        $url = parse_url(config('app.url'), PHP_URL_HOST);
34
        $msg = "Here is the information you'll need to set up your webhooks:\n\n".
35 10
               "  Payload URL: <comment>https://$url/%s</comment>\n".
36 10
               "  Secret Key:  <comment>%s</comment>\n\n".
37 10
               "You can display this information again by running `php artisan deploy:info`\n";
38 10
        $conf = 'Are you sure you want to overwrite the existing keys?';
39 10
        $show = $this->option('show');
40 10
        $over = $this->option('force');
41 10
        $file = file_exists($path) ? file_get_contents($path) : '';
42 10
        $set = preg_match('/^(?=.*\bAUTODEPLOY_SECRET=(\S+)\b)(?=.*\bAUTODEPLOY_ROUTE=(\S+)\b).*$/s', $file, $keys);
43
44
        // Step 0: Handle edge cases
45 10
        if ($show and $over) {
46 2
            return $this->error("You can't use --force and --show together!");
47
        }
48
49 8
        if ($show and !$set) {
50
            if ($this->confirm("You don't have any values to show yet. Would you like to create them now?", true)) {
51
                $show = false;
52
            } else {
53
                return;
54
            }
55
        }
56
57
        // Step 1: Retrieve or Generate?
58 8
        if (($set and $show) || ($set and !$show and !$over and !$this->confirm($conf))) {
59
            // Retrieve
60 4
            $secret = $keys[1];
61 4
            $route = $keys[2];
62 2
        } else {
63
            // Generate
64 4
            $secret = $this->getRandomKey($this->laravel['config']['app.cipher']);
65 4
            $route = $this->getRandomKey($this->laravel['config']['app.cipher']);
66 4
            if (!$show) {
67 4
                if ($file) {
68 2
                    if ($set) {
69
                        // Replace existing keys
70 2
                        file_put_contents(
71 1
                            $path,
72 1
                            preg_replace(
73 2
                                '/AUTODEPLOY_SECRET=\S+\n/',
74 2
                                "AUTODEPLOY_SECRET=$secret\n",
75
                                $file
76 1
                            )
77 1
                        );
78 2
                        file_put_contents(
79 1
                            $path,
80 1
                            preg_replace(
81 2
                                '/AUTODEPLOY_ROUTE=\S+\n/',
82 2
                                "AUTODEPLOY_ROUTE=$route\n",
83 1
                                file_get_contents($path)
84 1
                            )
85 1
                        );
86 1
                    } else {
87
                        // Append to existing environment variables
88 1
                        file_put_contents($path, $file."\n\nAUTODEPLOY_SECRET=$secret\nAUTODEPLOY_ROUTE=$route\n");
89
                    }
90 1
                } else {
91
                    // Create new .env file
92 2
                    file_put_contents($path, "AUTODEPLOY_SECRET=$secret\nAUTODEPLOY_ROUTE=$route\n");
93
                }
94 2
            }
95
        }
96
97
        // Step 2: Set the values in the global config
98 8
        $this->laravel['config']['auto-deploy.secret'] = $secret;
99 8
        $this->laravel['config']['auto-deploy.route'] = $route;
100
101
        // Step 3: Display the keys
102 8
        return $this->line(sprintf($msg, $route, $secret));
103
    }
104
105
    /**
106
     * Generate random keys for the auto deploy package.
107
     *
108
     * @param string $cipher
109
     *
110
     * @return string
111
     */
112 4
    protected function getRandomKey($cipher)
113
    {
114 4
        if ($cipher === 'AES-128-CBC') {
115
            return Str::random(16);
116
        }
117
118 4
        return Str::random(32);
119
    }
120
}
121