InstallLaravelVtuDotNG::isConfirmed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created By: Henry Ejemuta
4
 * Project: laravel-vtung
5
 * Class Name: InstallLaravelVtuDotNG.php
6
 * Date Created: 9/27/20
7
 * Time Created: 6:00 PM
8
 */
9
10
namespace HenryEjemuta\LaravelVtuDotNG\Console;
11
12
use Illuminate\Console\Command;
13
use Illuminate\Support\Str;
14
15
class InstallLaravelVtuDotNG extends Command
16
{
17
    protected $signature = 'vtung:init';
18
19
    protected $description = 'Install Laravel VtuDotNG package';
20
21
    public function handle()
22
    {
23
        $this->info('Installing Laravel VtuDotNG by Henry Ejemuta...');
24
25
        $this->info('Publishing configuration...');
26
27
        $this->call('vendor:publish', [
28
            '--provider' => "HenryEjemuta\LaravelVtuDotNG\VtuDotNGServiceProvider",
29
            '--tag' => "config"
30
        ]);
31
32
        $this->info('Configuration Published!');
33
34
        $this->info('Checking for environmental variable file (.env)');
35
        if (file_exists($path = $this->envPath()) === false) {
36
            $this->info('Environmental variable file (.env) not found!');
37
        } else {
38
            if ($this->isConfirmed() === false) {
39
                $this->comment('Phew... No changes were made to your .env file');
40
                return;
41
            }
42
            $this->info('Now writing .env file with VTU.ng Environmental variable declaration for you to modify...');
43
44
            $this->writeChanges($path, "VTU_DOT_NG_BASE_URL", "base_url", 'https://vtu.ng/wp-json/api/v1/');
45
            $this->writeChanges($path, "VTU_DOT_NG_API_KEY", "api_key", "");
46
            $this->writeChanges($path, "VTU_DOT_NG_USERNAME", "username", "");
47
            $this->writeChanges($path, "VTU_DOT_NG_PASSWORD", "password", "");
48
49
        }
50
51
        $this->info('Laravel VTU.ng Package Installation Complete!');
52
    }
53
54
    private function writeChanges($path, string $key, string $configKey, $value){
55
        if (Str::contains(file_get_contents($path), "$key") === false) {
56
            $this->info("Now writing .env with $key=$value ...");
57
            file_put_contents($path, PHP_EOL."$key=$value".PHP_EOL, FILE_APPEND);
58
        }else{
59
            $this->info("Now updating $key value in your .env to $value ...");
60
            // update existing entry
61
            file_put_contents($path, str_replace(
62
                "$key=".$this->laravel['config']["vtung.$configKey"],
63
                "$key=$value", file_get_contents($path)
64
            ));
65
        }
66
    }
67
68
69
    /**
70
     * Get the .env file path.
71
     *
72
     * @return string
73
     */
74
    protected function envPath()
75
    {
76
        if (method_exists($this->laravel, 'environmentFilePath')) {
77
            return $this->laravel->environmentFilePath();
0 ignored issues
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...
78
        }
79
80
        // check if laravel version Less than 5.4.17
81
        if (version_compare($this->laravel->version(), '5.4.17', '<')) {
82
            return $this->laravel->basePath() . DIRECTORY_SEPARATOR . '.env';
83
        }
84
85
        return $this->laravel->basePath('.env');
86
    }
87
88
    /**
89
     * Check if the modification is confirmed.
90
     *
91
     * @return bool
92
     */
93
    protected function isConfirmed()
94
    {
95
        return $this->confirm(
96
            'If your VTU.ng details are set within your .env file they would be overridden. Are you sure you want to override them if exists?'
97
        );
98
    }
99
}
100