Passed
Push — master ( 051f8c...0effca )
by Daniel
02:41
created

InstallCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
dl 0
loc 67
rs 10
c 1
b 0
f 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 1
A registerTelescopeServiceProvider() 0 28 2
1
<?php
2
3
namespace DanielRobert\Otp\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
8
class InstallCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'otp-generator:install';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Install all of the Otp Generator resources';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return void
28
     */
29
    public function handle()
30
    {
31
        $this->comment('Publishing Otp Generator Service Provider...');
32
        $this->callSilent('vendor:publish', ['--tag' => 'otp-provider']);
33
34
        $this->comment('Publishing Otp Generator Configuration...');
35
        $this->callSilent('vendor:publish', ['--tag' => 'otp-config']);
36
37
        $this->registerTelescopeServiceProvider();
38
39
        $this->info('Otp Generator scaffolding installed successfully.');
40
    }
41
42
    /**
43
     * Register the Otp service provider in the application configuration file.
44
     *
45
     * @return void
46
     */
47
    protected function registerTelescopeServiceProvider()
48
    {
49
        $namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace());
50
51
        $appConfig = file_get_contents(config_path('app.php'));
52
53
        if (Str::contains($appConfig, $namespace.'\\Providers\\OtpGeneratorServiceProvider::class')) {
54
            return;
55
        }
56
57
        $lineEndingCount = [
58
            "\r\n" => substr_count($appConfig, "\r\n"),
59
            "\r" => substr_count($appConfig, "\r"),
60
            "\n" => substr_count($appConfig, "\n"),
61
        ];
62
63
        $eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
64
65
        file_put_contents(config_path('app.php'), str_replace(
66
            "{$namespace}\\Providers\RouteServiceProvider::class,".$eol,
67
            "{$namespace}\\Providers\RouteServiceProvider::class,".$eol."        {$namespace}\Providers\OtpGeneratorServiceProvider::class,".$eol,
68
            $appConfig
69
        ));
70
71
        file_put_contents(app_path('Providers/OtpGeneratorServiceProvider.php'), str_replace(
72
            "namespace App\Providers;",
73
            "namespace {$namespace}\Providers;",
74
            file_get_contents(app_path('Providers/OtpGeneratorServiceProvider.php'))
75
        ));
76
    }
77
}