CommandUtilTrait::runInProduction()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 14
cts 14
cp 1
crap 4
rs 9.8333
1
<?php
2
/**
3
 * CommandUtilTrait.
4
 */
5
6
namespace Bmatovu\MtnMomo\Traits;
7
8
/**
9
 * Console commands utilities.
10
 */
11
trait CommandUtilTrait
12
{
13
    /**
14
     * Write a string as standard output.
15
     *
16
     * @see \Illuminate\Console\Concerns\InteractsWithIO::line
17
     *
18
     * @param  string  $string
19
     * @param  string|null  $style
20
     * @param  int|string|null  $verbosity
21
     *
22
     * @return void
23
     */
24
    abstract public function line($string, $style = null, $verbosity = null);
25
26
    /**
27
     * Warn user running command in production.
28
     *
29
     * @param  string $warning
30
     *
31
     * @return bool
32
     */
33 14
    protected function runInProduction($warning = 'Application In Production!')
34
    {
35 14
        if ($this->getLaravel()->environment() != 'production') {
0 ignored issues
show
Bug introduced by
It seems like getLaravel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        if ($this->/** @scrutinizer ignore-call */ getLaravel()->environment() != 'production') {
Loading history...
36 11
            return true;
37
        }
38
39 3
        if ($this->option('force')) {
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if ($this->/** @scrutinizer ignore-call */ option('force')) {
Loading history...
40 1
            return true;
41
        }
42
43 2
        $this->comment(str_repeat('*', strlen($warning) + 12));
0 ignored issues
show
Bug introduced by
It seems like comment() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->/** @scrutinizer ignore-call */ 
44
               comment(str_repeat('*', strlen($warning) + 12));
Loading history...
44 2
        $this->comment('*     '.$warning.'     *');
45 2
        $this->comment(str_repeat('*', strlen($warning) + 12));
46 2
        $this->output->writeln('');
47
48 2
        $confirmed = $this->confirm('Do you really wish to proceed?');
0 ignored issues
show
Bug introduced by
It seems like confirm() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        $confirmed = $this->confirm('Do you really wish to proceed?');
Loading history...
49
50 2
        if (! $confirmed) {
51 1
            $this->comment('Command Cancelled!');
52
53 1
            return false;
54
        }
55
56 1
        return true;
57
    }
58
59
    /**
60
     * Print formatted labels.
61
     *
62
     * @param  string  $title
63
     * @param  array|string  $body
64
     *
65
     * @return void
66
     */
67 9
    protected function printLabels($title, $body = null)
68
    {
69 9
        $this->line("<options=bold>{$title}</>");
70
71 9
        if (is_null($body)) {
72 9
            return;
73
        }
74
75 7
        $body = is_array($body) ? $body : [$body];
76
77 7
        foreach ($body as $content) {
78 7
            $this->line("{$content}");
79
        }
80
81 7
        $this->output->writeln('');
82
    }
83
84
    /**
85
     * Write | replace setting in .env file.
86
     *
87
     * @param  string $name ENV_VALUE, like; `APP_NAME`
88
     * @param  string $key Compose setting name, like `app.name`
89
     * @param  string $value Setting value
90
     *
91
     * @return void
92
     */
93 12
    protected function updateSetting($name, $key, $value)
94
    {
95
        // Update in memory.
96 12
        $this->laravel['config']->set([$key => $value]);
97
98 12
        if ($this->option('no-write')) {
99 1
            return;
100
        }
101
102
        // Update in file.
103 11
        $env = environment_file_path();
104
105 11
        $name = strtoupper($name);
106
107 11
        $pattern = "/^{$name}=[\"']?.*/m";
108
109 11
        if (preg_match($pattern, file_get_contents($env))) {
110 1
            file_put_contents($env, preg_replace($pattern, "{$name}=\"{$value}\"", file_get_contents($env)));
111
        } else {
112 11
            $setting = "\r\n{$name}=\"{$value}\"";
113 11
            file_put_contents($env, file_get_contents($env).$setting);
114
        }
115
    }
116
}
117