Install::copyNavigationView()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace Yaro\Jarboe\Console\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use SplFileObject;
8
9
class Install extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'jarboe:install';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Configure package';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return mixed
29
     */
30
    public function handle()
31
    {
32
        $this->publishAssets();
33
        $this->publishConfigs();
34
        $this->copyNavigationView();
35
        $this->copyMigrationFiles();
36
        $this->copyThirdPartyMigrationFiles();
37
38
        if ($this->confirm("Add admin's guard and provider to auth config file?")) {
39
            $this->addAdminsGuardToConfigFile();
40
        }
41
42
        if ($this->confirm('Run migrations?')) {
43
            $this->call('migrate');
44
        }
45
46
        $this->info('Completed');
47
    }
48
49
    private function copyNavigationView()
50
    {
51
        $this->comment('Creating navigation view:');
52
        if ($this->isNavigationViewExists()) {
53
            $this->comment('  - already exists');
54
            return;
55
        }
56
57
        shell_exec(sprintf(
58
            'mkdir -p "%s" && cp "%s" "%s"',
59
            base_path('resources/views/vendor/jarboe/inc/'),
60
            base_path('vendor/yaro/jarboe/src/resources/views/inc/navigation.blade.php'),
61
            base_path('resources/views/vendor/jarboe/inc/navigation.blade.php')
62
        ));
63
    }
64
65
    private function isNavigationViewExists()
66
    {
67
        return file_exists(base_path('resources/views/vendor/jarboe/inc/navigation.blade.php'));
68
    }
69
70
    private function copyMigrationFiles()
71
    {
72
        $this->comment('Creating migration files:');
73
74
        $date = Carbon::now();
75
76
        if (!$this->isMigrationFileExist('create_admins_table.php')) {
77
            shell_exec(sprintf(
78
                'cp "%s" "%s"',
79
                base_path('vendor/yaro/jarboe/src/database/migrations/2018_06_28_152903_create_admins_table.php'),
80
                database_path(sprintf('migrations/%s_create_admins_table.php', $date->format('Y_m_d_His')))
81
            ));
82
            $date->addSecond();
83
        }
84
85
        if (!$this->isMigrationFileExist('add_otp_secret_column_to_admins_table.php')) {
86
            shell_exec(sprintf(
87
                'cp "%s" "%s"',
88
                base_path('vendor/yaro/jarboe/src/database/migrations/2019_05_18_210659_add_otp_secret_column_to_admins_table.php'),
89
                database_path(sprintf('migrations/%s_add_otp_secret_column_to_admins_table.php', $date->format('Y_m_d_His')))
90
            ));
91
            $date->addSecond();
92
        }
93
94
        if (!$this->isMigrationFileExist('create_versions_table.php')) {
95
            shell_exec(sprintf(
96
                'cp "%s" "%s"',
97
                base_path('vendor/yaro/jarboe/src/database/migrations/2020_07_15_213242_create_versions_table.php'),
98
                database_path(sprintf('migrations/%s_create_versions_table.php', $date->format('Y_m_d_His')))
99
            ));
100
            $date->addSecond();
101
        }
102
    }
103
104
    private function isMigrationFileExist($filename)
105
    {
106
        return (bool)glob(database_path('migrations/*_'. $filename));
107
    }
108
109
    private function copyThirdPartyMigrationFiles()
110
    {
111
        $this->comment('Publishing third-party migration files:');
112
        if ($this->isThirdPartyMigrationFilesExist()) {
113
            $this->comment('  - already exists');
114
            return;
115
        }
116
117
        $this->call('vendor:publish', [
118
            '--provider' => 'Spatie\Permission\PermissionServiceProvider',
119
            '--tag' => 'migrations',
120
        ]);
121
        $this->call('vendor:publish', [
122
            '--provider' => 'Spatie\Permission\PermissionServiceProvider',
123
            '--tag' => 'config',
124
        ]);
125
    }
126
127
    private function isThirdPartyMigrationFilesExist()
128
    {
129
        return (bool)glob(database_path('migrations/*_create_permission_tables.php'));
130
    }
131
132
    private function publishAssets()
133
    {
134
        $this->comment('Publishing public assets:');
135
        if ($this->isAssetsPublished()) {
136
            $this->comment('  - already exists');
137
            return;
138
        }
139
140
        $this->call('vendor:publish', [
141
            '--provider' => "Yaro\Jarboe\ServiceProvider",
142
            '--tag' => 'public',
143
            '--force' => true,
144
        ]);
145
    }
146
147
    private function isAssetsPublished()
148
    {
149
        return file_exists(public_path('vendor/jarboe'));
150
    }
151
152
    private function publishConfigs()
153
    {
154
        $this->comment('Publishing config files:');
155
        if ($this->isConfigsPublished()) {
156
            $this->comment('  - already exists');
157
            return;
158
        }
159
160
        $this->call('vendor:publish', [
161
            '--provider' => "Yaro\Jarboe\ServiceProvider",
162
            '--tag' => 'config',
163
        ]);
164
    }
165
166
    private function isConfigsPublished()
167
    {
168
        return file_exists(config_path('jarboe'));
169
    }
170
171
    private function addAdminsGuardToConfigFile()
172
    {
173
        $shouldAddGuardsSection = true;
174
        if (config('auth.guards.admin')) {
175
            $shouldAddGuardsSection = false;
176
            $this->comment('  - guard [admin] already exist');
177
        }
178
179
        $shouldAddProvidersSection = true;
180
        if (config('auth.guards.providers.admins')) {
181
            $shouldAddProvidersSection = false;
182
            $this->comment('  - provider [admins] already exist');
183
        }
184
185
        if (!$shouldAddGuardsSection && !$shouldAddProvidersSection) {
186
            return;
187
        }
188
189
        $configFile = config_path('auth.php');
190
191
        $output = '';
192
        $file = new SplFileObject($configFile, 'r');
193
        foreach ($file as $lineNumber => $line) {
194
            $output .= $line;
195
            if ($line == "    'guards' => [\n" && $shouldAddGuardsSection) {
196
                $output .= "        'admin' => [\n";
197
                $output .= "            'driver' => 'session',\n";
198
                $output .= "            'provider' => 'admins',\n";
199
                $output .= "        ],\n\n";
200
            }
201
202
            if ($line == "    'providers' => [\n" && $shouldAddProvidersSection) {
203
                $output .= "        'admins' => [\n";
204
                $output .= "            'driver' => 'eloquent',\n";
205
                $output .= "            'model' => \Yaro\Jarboe\Models\Admin::class,\n";
206
                $output .= "        ],\n\n";
207
            }
208
        }
209
210
        $file = new SplFileObject($configFile, 'w+');
211
        $file->fwrite($output);
212
    }
213
}
214