InstallLaravelVTPass   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 36
c 2
b 0
f 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeChanges() 0 10 2
A envPath() 0 12 3
A isConfirmed() 0 4 1
A handle() 0 30 3
1
<?php
2
/**
3
 * Created By: Henry Ejemuta
4
 * Project: laravel-vtpass
5
 * Class Name: InstallLaravelVTPass.php
6
 * Date Created: 7/13/20
7
 * Time Created: 7:26 PM
8
 */
9
10
namespace HenryEjemuta\LaravelVTPass\Console;
11
12
use Illuminate\Console\Command;
13
use Illuminate\Support\Str;
14
15
class InstallLaravelVTPass extends Command
16
{
17
    protected $signature = 'vtpass:init';
18
19
    protected $description = 'Install Laravel VTPass package';
20
21
    public function handle()
22
    {
23
        $this->info('Installing Laravel VTPass by Henry Ejemuta...');
24
25
        $this->info('Publishing configuration...');
26
27
        $this->call('vendor:publish', [
28
            '--provider' => "HenryEjemuta\LaravelVTPass\LaravelVTPassServiceProvider",
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 VTPass Sandbox credential for you to modify...');
43
44
            $this->writeChanges($path, "VTPASS_BASE_URL", "base_url", 'https://sandbox.vtpass.com/api');
45
            $this->writeChanges($path, "VTPASS_USERNAME", "username", "REPLACE_WITH_YOUR_EMAIL");
46
            $this->writeChanges($path, "VTPASS_PASSWORD", "password", "REPLACE_WITH_YOUR_PASSWORD");
47
48
        }
49
50
        $this->info('Laravel VTPass Package Installation Complete!');
51
    }
52
53
    private function writeChanges($path, string $key, string $configKey, $value){
54
        if (Str::contains(file_get_contents($path), "$key") === false) {
55
            $this->info("Now writing .env with $key=$value ...");
56
            file_put_contents($path, PHP_EOL."$key=$value".PHP_EOL, FILE_APPEND);
57
        }else{
58
            $this->info("Now updating $key value in your .env to $value ...");
59
            // update existing entry
60
            file_put_contents($path, str_replace(
61
                "$key=".$this->laravel['config']["vtpass.$configKey"],
62
                "$key=$value", file_get_contents($path)
63
            ));
64
        }
65
    }
66
67
68
    /**
69
     * Get the .env file path.
70
     *
71
     * @return string
72
     */
73
    protected function envPath()
74
    {
75
        if (method_exists($this->laravel, 'environmentFilePath')) {
76
            return $this->laravel->environmentFilePath();
77
        }
78
79
        // check if laravel version Less than 5.4.17
80
        if (version_compare($this->laravel->version(), '5.4.17', '<')) {
81
            return $this->laravel->basePath() . DIRECTORY_SEPARATOR . '.env';
82
        }
83
84
        return $this->laravel->basePath('.env');
85
    }
86
87
    /**
88
     * Check if the modification is confirmed.
89
     *
90
     * @return bool
91
     */
92
    protected function isConfirmed()
93
    {
94
        return $this->confirm(
95
            'If your VTPass details are set within your .env file they would be overridden. Are you sure you want to override them if exists?'
96
        );
97
    }
98
}
99