Completed
Push — master ( 279d4e...5d09e0 )
by Elf
02:49
created

OptimusGenerate::setValuesInEnvironmentFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
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());
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...
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);
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...
80
    }
81
}
82