Passed
Push — dependabot/github_actions/code... ( 53093b...339e79 )
by
unknown
09:36 queued 04:41
created

SmsSwitchConnectionCommand::isConfirmed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\LaravelSms\Commands;
6
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Str;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class SmsSwitchConnectionCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'sms:connection';
21
22
    /**
23
     * Get the console command arguments.
24
     *
25
     * @return \Symfony\Component\Console\Input\InputArgument[]
26
     */
27 3
    protected function getArguments(): array
28
    {
29 3
        return [new InputArgument('connection', InputArgument::REQUIRED, 'Which connection to use')];
30
    }
31
32
    /**
33
     * Get the console command options.
34
     *
35
     * @return \Symfony\Component\Console\Input\InputOption[]
36
     */
37 3
    protected function getOptions(): array
38
    {
39
        return [
40 3
            new InputOption(
41
                'show',
42
                's',
43
                InputOption::VALUE_NONE,
44
                'Display the sms default connection instead of modifying files'
45
            ),
46 3
            new InputOption(
47
                'always-no',
48
                null,
49
                InputOption::VALUE_NONE,
50
                'Skip generating sms default connection if it already exists'
51
            ),
52 3
            new InputOption(
53
                'force',
54
                'f',
55
                InputOption::VALUE_NONE,
56
                'Skip confirmation when overwriting an existing sms default connection'
57
            ),
58
        ];
59
    }
60
61
    /**
62
     * The console command description.
63
     *
64
     * @var string
65
     */
66
    protected $description = 'Set the sms default connection used to send message';
67
68
    /**
69
     * Execute the console command.
70
     */
71 3
    public function handle(): void
72
    {
73 3
        $connection = $this->argument('connection');
74 3
        if (! is_string($connection)) {
75 1
            return;
76
        }
77
78 3
        if ($this->option('show')) {
79 1
            $this->comment('SMS_CONNECTION=' . $connection);
80
81 1
            return;
82
        }
83
84 3
        $path = $this->envPath();
85 3
        if (! file_exists($path)) {
86 1
            $this->displayConnection($connection);
87
88 1
            return;
89
        }
90
91 3
        if ($this->putEnvToFile($connection, $path)) {
92 3
            $this->displayConnection($connection);
93
        }
94
    }
95
96
    /**
97
     * put default sms connection to the .env file path.
98
     *
99
     * @param string $connection the default sms connection
100
     * @param string $path the .env file path.
101
     */
102 3
    protected function putEnvToFile(string $connection, string $path): bool
103
    {
104
        /** @var string $contents */
105 3
        $contents = file_get_contents($path);
106
107 3
        if (! Str::contains($contents, 'SMS_CONNECTION')) {
108
            // create new entry
109 3
            file_put_contents($path, PHP_EOL . sprintf('SMS_CONNECTION=%s', $connection) . PHP_EOL, FILE_APPEND);
110
111 3
            return true;
112
        }
113
114 3
        if ($this->option('always-no')) {
115 1
            $this->comment('Sms default connection already exists. Skipping...');
116
117 1
            return false;
118
        }
119
120 2
        if (! $this->isConfirmed()) {
121 1
            $this->comment('Phew... No changes were made to your sms default connection.');
122
123 1
            return false;
124
        }
125
126 2
        file_put_contents(
127
            $path,
128 2
            str_replace('SMS_CONNECTION=' . Config::get('sms.default'), 'SMS_CONNECTION=' . $connection, $contents)
129
        );
130
131 2
        return true;
132
    }
133
134
    /**
135
     * Display the key.
136
     */
137 3
    protected function displayConnection(string $connection): void
138
    {
139 3
        Config::set('sms.default', $connection);
140
141 3
        $this->info(sprintf('sms default connection switch to [%s] successfully.', $connection));
142
    }
143
144
    /**
145
     * Check if the modification is confirmed.
146
     */
147 2
    protected function isConfirmed(): bool
148
    {
149 2
        if ($this->option('force')) {
150 1
            return true;
151
        }
152
153 1
        return $this->confirm(
154
            'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?'
155
        );
156
    }
157
158
    /**
159
     * Get the .env file path.
160
     */
161 3
    protected function envPath(): string
162
    {
163 3
        if (method_exists($this->laravel, 'environmentFilePath')) {
164 3
            return $this->laravel->environmentFilePath();
165
        }
166
167
        return $this->laravel->basePath('.env');
168
    }
169
}
170