Completed
Push — master ( 806d76...b55f3e )
by CodexShaper
04:53
created

InstallDatabaseManager::handle()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 37
c 3
b 0
f 0
nc 16
nop 1
dl 0
loc 52
ccs 0
cts 45
cp 0
crap 42
rs 8.7057

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'dbm:install {mongodb?} {--force=}';
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
        $composer = $this->findComposer();
66
        if ($this->argument('mongodb') == 'mongodb') {
67
            $this->info('Installing MongoDB package');
68
            $process = new Process($composer . ' require jenssegers/mongodb');
0 ignored issues
show
Bug introduced by
$composer . ' require jenssegers/mongodb' 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

68
            $process = new Process(/** @scrutinizer ignore-type */ $composer . ' require jenssegers/mongodb');
Loading history...
69
            $process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time
70
            $process->setWorkingDirectory(base_path())->run();
71
        }
72
        $this->info('Publishing the Database Manager assets, database, and config files');
73
        // Publish only relevant resources on install
74
        $tags = ['dbm.config'];
75
        $this->call('vendor:publish', ['--provider' => ManagerServiceProvider::class, '--tag' => $tags]);
76
        // Generate Storage Link
77
        $this->info('Generate storage symblink');
78
        $this->call('storage:link');
79
        // Dump autoload
80
        $this->info('Dumping the autoloaded files and reloading all new files');
81
        $process = new Process($composer . ' dump-autoload');
82
        $process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time
83
        $process->setWorkingDirectory(base_path())->run();
84
        // Migrate database
85
        $this->info('Migrating the database tables into your application');
86
        $this->call('migrate', ['--force' => $this->option('force')]);
87
        // Install laravel passport
88
        $this->info('Install Passport');
89
        $this->call('passport:install', ['--force' => $this->option('force')]);
90
        // Load Custom Database Manager routes
91
        $this->info('Adding Database Manager routes');
92
        $web_routes_contents = $filesystem->get(base_path('routes/web.php'));
93
        $api_routes_contents = $filesystem->get(base_path('routes/api.php'));
94
        if (false === strpos($web_routes_contents, 'DBM::webRoutes();')) {
95
            $filesystem->append(
96
                base_path('routes/web.php'),
97
                "\n\nDBM::webRoutes();\n"
98
            );
99
        }
100
        if (false === strpos($api_routes_contents, 'DBM::apiRoutes();')) {
101
            $filesystem->append(
102
                base_path('routes/api.php'),
103
                "\n\nDBM::apiRoutes();\n"
104
            );
105
        }
106
        // Database seeder
107
        $this->info('Seeding...');
108
        $class = 'DatabaseManagerSeeder';
109
        $file  = $this->seedersPath . $class . '.php';
110
        if (file_exists($file) && !class_exists($class)) {
111
            require_once $file;
112
        }
113
        with(new $class())->run();
114
        $this->info('Seeding Completed');
115
    }
116
}
117