OptimusGenerate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 15 2
A printValues() 0 7 2
A setValuesInEnvironmentFile() 0 17 3
1
<?php
2
3
namespace App\Support\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use Jenssegers\Optimus\Energon;
8
9
class OptimusGenerate extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'optimus:generate {--show : Display the values instead of modifying files}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Set Optimus prime values';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return mixed
29
     */
30
    public function handle()
31
    {
32
        $values = array_combine(['prime', 'inverse', 'random'], Energon::generate());
33
34
        if ($this->option('show')) {
35
            return $this->printValues($values);
36
        }
37
38
        $this->setValuesInEnvironmentFile($values);
39
40
        $this->laravel['config']['optimus'] = $values;
41
42
        $this->printValues($values);
43
        $this->info('Optimus values set successfully.');
44
    }
45
46
    /**
47
     * Print Optimus values.
48
     *
49
     * @param  array  $values
50
     */
51
    protected function printValues($values, $method = 'comment')
52
    {
53
        foreach ($values as $key => $value) {
54
            $key = Str::title($key);
55
            $this->{$method}("{$key}: {$value}");
56
        }
57
    }
58
59
    /**
60
     * Set Optimus values in the environment file.
61
     *
62
     * @param  array  $values
63
     */
64
    protected function setValuesInEnvironmentFile($values)
65
    {
66
        $content = file_get_contents($this->laravel->environmentFilePath());
67
68
        foreach ($values as $key => $value) {
69
            $name = 'OPTIMUS_'.strtoupper($key);
70
            $text = "{$name}={$value}";
71
72
            $content = preg_replace("#{$name}=.*#", $text, $content, -1, $replaceCount);
73
74
            if (0 === $replaceCount) {
75
                $content .= $text.PHP_EOL;
76
            }
77
        }
78
79
        file_put_contents($this->laravel->environmentFilePath(), $content);
80
    }
81
}
82