Passed
Pull Request — master (#202)
by Zing
05:03
created

SmsSwitchConnectionCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 10
Bugs 1 Features 0
Metric Value
wmc 17
eloc 60
c 10
b 1
f 0
dl 0
loc 161
ccs 60
cts 64
cp 0.9375
rs 10

7 Methods

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