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

OptimusGenerate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

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 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