Completed
Push — master ( f0d1a4...bf72bf )
by Elf
09:50
created

GenerateOptimusCommand::printValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Services\Optimus;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Jenssegers\Optimus\Energon;
8
9
class GenerateOptimusCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'support:generate-optimus
17
        {--show : Display the values instead of modifying files}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Generate Optimus values';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return mixed
30
     */
31
    public function handle()
32
    {
33
        $values = array_combine(['prime', 'inverse', 'random'], Energon::generate());
34
35
        if ($this->option('show')) {
36
            return $this->printValues($values);
37
        }
38
39
        $this->setValuesInEnvironmentFile($values);
40
41
        $this->laravel['config']['support.optimus'] = $values;
42
43
        $this->printValues($values);
44
45
        $this->info('Optimus values set successfully.');
46
    }
47
48
    /**
49
     * Print Optimus values.
50
     *
51
     * @param  array  $values
52
     */
53
    protected function printValues($values)
54
    {
55
        $this->table(array_keys($values), [array_values($values)]);
56
    }
57
58
    /**
59
     * Set Optimus values in the environment file.
60
     *
61
     * @param  array  $values
62
     */
63
    protected function setValuesInEnvironmentFile($values)
64
    {
65
        $content = file_get_contents($this->laravel->environmentFilePath());
1 ignored issue
show
Bug introduced by
The method environmentFilePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
66
67
        foreach ($values as $key => $value) {
68
            $name = 'OPTIMUS_'.strtoupper($key);
69
            $text = "{$name}={$value}";
70
71
            $content = preg_replace("#{$name}=.*#", $text, $content, -1, $replaceCount);
72
73
            if (0 === $replaceCount) {
74
                $content .= $text.PHP_EOL;
75
            }
76
        }
77
78
        file_put_contents($this->laravel->environmentFilePath(), $content);
1 ignored issue
show
Bug introduced by
The method environmentFilePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
79
    }
80
}
81