Passed
Pull Request — 2.x (#1376)
by Harings
09:59
created

Install::installPreset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use A17\Twill\Commands\Traits\HandlesPresets;
6
use Illuminate\Database\DatabaseManager;
7
use Illuminate\Filesystem\Filesystem;
8
9
class Install extends Command
10
{
11
    use HandlesPresets;
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'twill:install {preset?}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Install Twill into your Laravel application';
25
26
    /**
27
     * @var Filesystem
28
     */
29
    protected $files;
30
31
    /**
32
     * @var DatabaseManager
33
     */
34
    protected $db;
35
36
    /**
37
     * @param Filesystem $files
38 69
     * @param DatabaseManager $db
39
     */
40 69
    public function __construct(Filesystem $files, DatabaseManager $db)
41
    {
42 69
        parent::__construct();
43 69
44 69
        $this->files = $files;
45
        $this->db = $db;
46
    }
47
48
    /**
49
     * Executes the console command.
50
     *
51
     * @return void
52 69
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
53
     */
54
    public function handle()
55
    {
56 69
        //check the database connection before installing
57
        try {
58
            $this->db->connection()->getPdo();
59
        } catch (\Exception $e) {
60
            $this->error('Could not connect to the database, please check your configuration:' . "\n" . $e);
61
            return;
62 69
        }
63 69
64 69
        if (filled($preset = $this->argument('preset'))) {
65 69
            if ($this->presetExists($preset)) {
0 ignored issues
show
Bug introduced by
It seems like $preset can also be of type array and null; however, parameter $preset of A17\Twill\Commands\Install::presetExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            if ($this->presetExists(/** @scrutinizer ignore-type */ $preset)) {
Loading history...
66 69
                if ($this->confirm('Are you sure to install this preset? This can overwrite your models, config and routes.')) {
67 69
                    $this->installPreset($preset);
0 ignored issues
show
Bug introduced by
It seems like $preset can also be of type array and null; however, parameter $preset of A17\Twill\Commands\Install::installPreset() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

67
                    $this->installPreset(/** @scrutinizer ignore-type */ $preset);
Loading history...
68 69
                } else {
69
                    $this->warn('Cancelled.');
70
                }
71
            } else {
72
                $this->error("Could not find preset: $preset");
73
            }
74
        } else {
75
            $this->addRoutesFile();
76 69
            $this->call('migrate');
77
            $this->publishConfig();
78 69
            $this->publishAssets();
79
            $this->createSuperAdmin();
80 69
            $this->info('All good!');
81
        }
82
    }
83
84 69
    private function installPreset(string $preset): void
85 1
    {
86 1
        $this->installPresetFiles($preset);
87
88 69
        $this->call('migrate');
89
        $this->publishAssets();
90
        $this->createSuperAdmin();
91
        $this->info('Finished installing preset!');
92
    }
93
94
    /**
95 69
     * Creates the default `admin.php` route configuration file.
96
     *
97 69
     * @return void
98 69
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
99
     */
100
    private function addRoutesFile()
101
    {
102
        $routesPath = base_path('routes');
103
104
        if (!$this->files->exists($routesPath)) {
105 69
            $this->files->makeDirectory($routesPath, 0755, true);
106
        }
107 69
108 69
        if (!$this->files->exists($routesPath . '/admin.php')) {
109
            $stub = $this->files->get(__DIR__ . '/stubs/admin.stub');
110
            $this->files->put($routesPath . '/admin.php', $stub);
111 69
        }
112
    }
113
114
    /**
115
     * Calls the command responsible for creation of the default superadmin user.
116
     *
117
     * @return void
118 69
     */
119
    private function createSuperAdmin()
120 69
    {
121 69
        if (!$this->option('no-interaction')) {
122
            $this->call('twill:superadmin');
123
        }
124 69
    }
125
126
    /**
127
     * Publishes the package configuration files.
128
     *
129
     * @return void
130
     */
131
    private function publishConfig()
132
    {
133
        $this->call('vendor:publish', [
134
            '--provider' => 'A17\Twill\TwillServiceProvider',
135
            '--tag' => 'config',
136
        ]);
137
    }
138
139
    /**
140
     * Publishes the package frontend assets.
141
     *
142
     * @return void
143
     */
144
    private function publishAssets()
145
    {
146
        $this->call('vendor:publish', [
147
            '--provider' => 'A17\Twill\TwillServiceProvider',
148
            '--tag' => 'assets',
149
        ]);
150
    }
151
}
152