Completed
Push — master ( 1f19ca...d173f6 )
by CodexShaper
04:58
created

InstallDatabaseManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 39
c 4
b 0
f 0
dl 0
loc 98
ccs 0
cts 50
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 4 1
A findComposer() 0 6 2
A handle() 0 45 5
1
<?php
2
namespace CodexShaper\DBM\Commands;
3
4
use CodexShaper\DBM\ManagerServiceProvider;
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Process\Process;
9
10
class InstallDatabaseManager extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'dbm:install';
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Install the Laravel Database Manager';
24
    /**
25
     * The database Seeder Path.
26
     *
27
     * @var string
28
     */
29
    protected $seedersPath = __DIR__ . '/../../database/seeds/';
30
31
    /**
32
     * Get Option
33
     *
34
     * @return array
35
     */
36
    protected function getOptions()
37
    {
38
        return [
39
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production', null],
40
        ];
41
    }
42
43
    /**
44
     * Get the composer command for the environment.
45
     *
46
     * @return string
47
     */
48
    protected function findComposer()
49
    {
50
        if (file_exists(getcwd() . '/composer.phar')) {
51
            return '"' . PHP_BINARY . '" ' . getcwd() . '/composer.phar';
52
        }
53
        return 'composer';
54
    }
55
56
    /**
57
     * Execute the console command.
58
     *
59
     * @param \Illuminate\Filesystem\Filesystem $filesystem
60
     *
61
     * @return void
62
     */
63
    public function handle(Filesystem $filesystem)
64
    {
65
        $this->info('Publishing the Database Manager assets, database, and config files');
66
        // Publish only relevant resources on install
67
        $tags = ['dbm.config'];
68
        $this->call('vendor:publish', ['--provider' => ManagerServiceProvider::class, '--tag' => $tags]);
69
70
        $this->info('Migrating the database tables into your application');
71
        $this->call('migrate', ['--force' => $this->option('force')]);
72
        $this->info('Install Passport');
73
        $this->call('passport:install', ['--force' => $this->option('force')]);
74
75
        $this->info('Dumping the autoloaded files and reloading all new files');
76
        $composer = $this->findComposer();
77
        $process  = new Process($composer . ' dump-autoload');
0 ignored issues
show
Bug introduced by
$composer . ' dump-autoload' of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        $process  = new Process(/** @scrutinizer ignore-type */ $composer . ' dump-autoload');
Loading history...
78
        $process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time
79
        $process->setWorkingDirectory(base_path())->run();
80
81
        // Load Custom Database Manager routes into application's 'routes/web.php'
82
        $this->info('Adding Database Manager routes');
83
        $web_routes_contents = $filesystem->get(base_path('routes/web.php'));
84
        $api_routes_contents = $filesystem->get(base_path('routes/api.php'));
85
        if (false === strpos($web_routes_contents, 'DBM::webRoutes();')) {
86
            $filesystem->append(
87
                base_path('routes/web.php'),
88
                "\n\nDBM::webRoutes();\n"
89
            );
90
        }
91
        if (false === strpos($api_routes_contents, 'DBM::apiRoutes();')) {
92
            $filesystem->append(
93
                base_path('routes/api.php'),
94
                "\n\nDBM::apiRoutes();\n"
95
            );
96
        }
97
98
        $this->info('Seeding...');
99
        // Seeding Dummy Data
100
        $class = 'DatabaseManagerSeeder';
101
        $file  = $this->seedersPath . $class . '.php';
102
        if (file_exists($file) && !class_exists($class)) {
103
            require_once $file;
104
        }
105
        with(new $class())->run();
106
107
        $this->info('Seeding Completed');
108
    }
109
}
110