InstallDatabaseManager::findComposer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace CodexShaper\DBM\Commands;
4
5
use CodexShaper\DBM\ManagerServiceProvider;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Process\Process;
10
11
class InstallDatabaseManager extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'dbm:install';
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Install the Laravel Database Manager';
25
    /**
26
     * The database Seeder Path.
27
     *
28
     * @var string
29
     */
30
    protected $seedersPath = __DIR__.'/../../database/seeds/';
31
32
    /**
33
     * Get Option.
34
     *
35
     * @return array
36
     */
37
    protected function getOptions()
38
    {
39
        return [
40
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production', null],
41
        ];
42
    }
43
44
    /**
45
     * Get the composer command for the environment.
46
     *
47
     * @return string
48
     */
49
    protected function findComposer()
50
    {
51
        if (file_exists(getcwd().'/composer.phar')) {
52
            return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar';
53
        }
54
55
        return 'composer';
56
    }
57
58
    /**
59
     * Execute the console command.
60
     *
61
     * @param \Illuminate\Filesystem\Filesystem $filesystem
62
     *
63
     * @return void
64
     */
65
    public function handle(Filesystem $filesystem)
66
    {
67
        $this->info('Publishing the Database Manager assets, database, and config files');
68
        // Publish only relevant resources on install
69
        $tags = ['dbm.config'];
70
        $this->call('vendor:publish', ['--provider' => ManagerServiceProvider::class, '--tag' => $tags]);
71
        // Create storage link
72
        $this->info('Generate storage link');
73
        $this->call('storage:link');
74
        // Migrate Database
75
        $this->info('Migrating the database tables into your application');
76
        $this->call('migrate', ['--force' => $this->option('force')]);
77
        //Install Passport
78
        $this->info('Install Passport');
79
        $this->call('passport:install', ['--force' => $this->option('force')]);
80
81
        $this->info('Dumping the autoloaded files and reloading all new files');
82
        $composer = $this->findComposer();
83
        $process = Process::fromShellCommandline($composer.' dump-autoload');
84
        $process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time
85
        $process->setWorkingDirectory(base_path())->run();
86
87
        // Modify auth api driver token to passport
88
        $auth_config_contents = $filesystem->get(base_path('config/auth.php'));
89
        $auth_config_contents = str_replace('\'token\'', '\'passport\'', $auth_config_contents);
90
        $filesystem->put(
91
            base_path('config/auth.php'),
92
            $auth_config_contents
93
        );
94
95
        // Load Custom Database Manager routes into application's 'routes/web.php'
96
        $this->info('Adding Database Manager routes');
97
        $web_routes_contents = $filesystem->get(base_path('routes/web.php'));
98
        $api_routes_contents = $filesystem->get(base_path('routes/api.php'));
99
        if (false === strpos($web_routes_contents, 'DBM::webRoutes();')) {
100
            $filesystem->append(
101
                base_path('routes/web.php'),
102
                "\n\nDBM::webRoutes();\n"
103
            );
104
        }
105
        if (false === strpos($api_routes_contents, 'DBM::apiRoutes();')) {
106
            $filesystem->append(
107
                base_path('routes/api.php'),
108
                "\n\nDBM::apiRoutes();\n"
109
            );
110
        }
111
112
        $this->info('Seeding...');
113
        // Seeding Dummy Data
114
        $class = 'DatabaseManagerSeeder';
115
        $file = $this->seedersPath.$class.'.php';
116
        if (file_exists($file) && ! class_exists($class)) {
117
            require_once $file;
118
        }
119
        with(new $class())->run();
120
121
        $this->info('Seeding Completed');
122
    }
123
}
124