Completed
Push — master ( 8724ba...8ef377 )
by Morgan
05:57
created

DeployInitCommand::getRandomKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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 10
        $ssl = config('auto-deploy.require-ssl') ? 'https' : 'http';
35
        $msg = "Here is the information you'll need to set up your webhooks:\n\n".
36 10
               "  Payload URL: <comment>$ssl://$url/%s</comment>\n".
37 10
               "  Secret Key:  <comment>%s</comment>\n\n".
38 10
               "You can display this information again by running `php artisan deploy:info`\n";
39 10
        $conf = 'Are you sure you want to overwrite the existing keys?';
40 10
        $show = $this->option('show');
41 10
        $over = $this->option('force');
42 10
        $file = file_exists($path) ? file_get_contents($path) : '';
43 10
        $set = preg_match('/^(?=.*\bAUTODEPLOY_SECRET=(\S+)\b)(?=.*\bAUTODEPLOY_ROUTE=(\S+)\b).*$/s', $file, $keys);
44
45
        // Step 0: Handle edge cases
46 10
        if ($show and $over) {
47 2
            return $this->error("You can't use --force and --show together!");
48
        }
49
50 8
        if ($show and !$set) {
51
            if ($this->confirm("You don't have any values to show yet. Would you like to create them now?", true)) {
52
                $show = false;
53
            } else {
54
                return;
55
            }
56
        }
57
58
        // Step 1: Retrieve or Generate?
59 8
        if (($set and $show) || ($set and !$show and !$over and !$this->confirm($conf))) {
60
            // Retrieve
61 4
            $secret = $keys[1];
62 4
            $route = $keys[2];
63 2
        } else {
64
            // Generate
65 4
            $secret = $this->getRandomKey($this->laravel['config']['app.cipher']);
66 4
            $route = $this->getRandomKey($this->laravel['config']['app.cipher']);
67 4
            if (!$show) {
68 4
                if ($file) {
69 2
                    if ($set) {
70
                        // Replace existing keys
71 2
                        file_put_contents(
72 1
                            $path,
73 1
                            preg_replace(
74 2
                                '/AUTODEPLOY_SECRET=\S+\n/',
75 2
                                "AUTODEPLOY_SECRET=$secret\n",
76
                                $file
77 1
                            )
78 1
                        );
79 2
                        file_put_contents(
80 1
                            $path,
81 1
                            preg_replace(
82 2
                                '/AUTODEPLOY_ROUTE=\S+\n/',
83 2
                                "AUTODEPLOY_ROUTE=$route\n",
84 1
                                file_get_contents($path)
85 1
                            )
86 1
                        );
87 1
                    } else {
88
                        // Append to existing environment variables
89 1
                        file_put_contents($path, $file."\n\nAUTODEPLOY_SECRET=$secret\nAUTODEPLOY_ROUTE=$route\n");
90
                    }
91 1
                } else {
92
                    // Create new .env file
93 2
                    file_put_contents($path, "AUTODEPLOY_SECRET=$secret\nAUTODEPLOY_ROUTE=$route\n");
94
                }
95 2
            }
96
        }
97
98
        // Step 2: Set the values in the global config
99 8
        $this->laravel['config']['auto-deploy.secret'] = $secret;
100 8
        $this->laravel['config']['auto-deploy.route'] = $route;
101
102
        // Step 3: Display the keys
103 8
        return $this->line(sprintf($msg, $route, $secret));
104
    }
105
106
    /**
107
     * Generate random keys for the auto deploy package.
108
     *
109
     * @param string $cipher
110
     *
111
     * @return string
112
     */
113 4
    protected function getRandomKey($cipher)
114
    {
115 4
        if ($cipher === 'AES-128-CBC') {
116
            return Str::random(16);
117
        }
118
119 4
        return Str::random(32);
120
    }
121
}
122