Passed
Push — master ( 3fba2a...ce9f9d )
by Zing
05:53 queued 12s
created

SmsSwitchConnectionCommand::handle()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 42
ccs 22
cts 22
cp 1
rs 8.9457
cc 6
nc 6
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\LaravelSms\Commands;
6
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Str;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class SmsSwitchConnectionCommand extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'sms:connection';
20
21
    /**
22
     * Get the console command arguments.
23
     *
24
     * @return array
25
     */
26 4
    protected function getArguments()
27
    {
28
        return [
29 4
            new InputArgument('connection', InputArgument::REQUIRED, 'Which connection to use'),
30
        ];
31
    }
32
33
    /**
34
     * Get the console command options.
35
     *
36
     * @return array
37
     */
38 4
    protected function getOptions()
39
    {
40
        return [
41 4
            new InputOption('show', 's', InputOption::VALUE_NONE, 'Display the sms default connection instead of modifying files'),
42 4
            new InputOption('always-no', null, InputOption::VALUE_NONE, 'Skip generating sms default connection if it already exists'),
43 4
            new InputOption('force', 'f', InputOption::VALUE_NONE, 'Skip confirmation when overwriting an existing sms default connection'),
44
        ];
45
    }
46
47
    /**
48
     * The console command description.
49
     *
50
     * @var string
51
     */
52
    protected $description = 'Set the sms default connection used to send message';
53
54
    /**
55
     * Execute the console command.
56
     *
57
     * @return void
58
     */
59 4
    public function handle()
60
    {
61 4
        $connection = $this->argument('connection');
62
63 4
        if ($this->option('show')) {
64 2
            $this->comment('SMS_CONNECTION=' . $connection);
0 ignored issues
show
Bug introduced by
Are you sure $connection of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

64
            $this->comment('SMS_CONNECTION=' . /** @scrutinizer ignore-type */ $connection);
Loading history...
65
66 2
            return;
67
        }
68
69 4
        if (file_exists($path = $this->envPath()) === false) {
70 2
            return $this->displayConnection($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type string[]; however, parameter $key of Zing\LaravelSms\Commands...nd::displayConnection() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
            return $this->displayConnection(/** @scrutinizer ignore-type */ $connection);
Loading history...
Bug introduced by
Are you sure the usage of $this->displayConnection($connection) targeting Zing\LaravelSms\Commands...nd::displayConnection() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        }
72
73 4
        if (Str::contains(file_get_contents($path), 'SMS_CONNECTION') === false) {
74
            // create new entry
75 4
            file_put_contents($path, PHP_EOL . "SMS_CONNECTION={$connection}" . PHP_EOL, FILE_APPEND);
76
        } else {
77 4
            if ($this->option('always-no')) {
78 2
                $this->comment('Sms default connection already exists. Skipping...');
79
80 2
                return;
81
            }
82
83 2
            if ($this->isConfirmed() === false) {
84 2
                $this->comment('Phew... No changes were made to your sms default connection.');
85
86 2
                return;
87
            }
88
89
            // update existing entry
90 2
            file_put_contents(
91 2
                $path,
92 2
                str_replace(
93 2
                    'SMS_CONNECTION=' . $this->laravel['config']['sms.default'],
94 2
                    'SMS_CONNECTION=' . $connection,
95 2
                    file_get_contents($path)
96
                )
97
            );
98
        }//end if
99
100 4
        $this->displayConnection($connection);
101 4
    }
102
103
    /**
104
     * Display the key.
105
     *
106
     * @param string $key
107
     *
108
     * @return void
109
     */
110 4
    protected function displayConnection($key): void
111
    {
112 4
        $this->laravel['config']['sms.default'] = $key;
113
114 4
        $this->info("sms default connection switch to [{$key}] successfully.");
115 4
    }
116
117
    /**
118
     * Check if the modification is confirmed.
119
     *
120
     * @return bool
121
     */
122 2
    protected function isConfirmed()
123
    {
124 2
        return $this->option('force') ? true : $this->confirm(
125 2
            'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?'
126
        );
127
    }
128
129
    /**
130
     * Get the .env file path.
131
     *
132
     * @return string
133
     */
134 4
    protected function envPath()
135
    {
136 4
        if (method_exists($this->laravel, 'environmentFilePath')) {
137 2
            return $this->laravel->environmentFilePath();
138
        }
139
140 2
        return $this->laravel->basePath('.env');
141
    }
142
}
143