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

Install::addRoutesFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 11
rs 10
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Database\DatabaseManager;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Facades\Storage;
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 = 'twill:install {preset?}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Install Twill into your Laravel application';
24
25
    /**
26
     * @var Filesystem
27
     */
28
    protected $files;
29
30
    /**
31
     * @var DatabaseManager
32
     */
33
    protected $db;
34
35
    /**
36
     * @param Filesystem $files
37
     * @param DatabaseManager $db
38 69
     */
39
    public function __construct(Filesystem $files, DatabaseManager $db)
40 69
    {
41
        parent::__construct();
42 69
43 69
        $this->files = $files;
44 69
        $this->db = $db;
45
    }
46
47
    /**
48
     * Executes the console command.
49
     *
50
     * @return void
51
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
52 69
     */
53
    public function handle()
54
    {
55
        //check the database connection before installing
56 69
        try {
57
            $this->db->connection()->getPdo();
58
        } catch (\Exception $e) {
59
            $this->error('Could not connect to the database, please check your configuration:' . "\n" . $e);
60
            return;
61
        }
62 69
63 69
        if (filled($preset = $this->argument('preset'))) {
64 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

64
            if ($this->presetExists(/** @scrutinizer ignore-type */ $preset)) {
Loading history...
65 69
                if ($this->confirm('Are you sure to install this preset? This can overwrite your models, config and routes.')) {
66 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

66
                    $this->installPreset(/** @scrutinizer ignore-type */ $preset);
Loading history...
67 69
                } else {
68 69
                    $this->warn('Cancelled.');
69
                }
70
            } else {
71
                $this->error("Could not find preset: $preset, available presets are: 'blog'");
72
            }
73
        } else {
74
            $this->addRoutesFile();
75
            $this->call('migrate');
76 69
            $this->publishConfig();
77
            $this->publishAssets();
78 69
            $this->createSuperAdmin();
79
            $this->info('All good!');
80 69
        }
81
    }
82
83
    private function presetExists(string $preset): bool
84 69
    {
85 1
        return in_array($preset, ['blog'], true);
86 1
    }
87
88 69
    private function installPreset(string $preset): void
89
    {
90
        $this->info("Checking preset requirements");
91
        if (!$this->meetsPresetRequirements($preset)) {
92
            $this->error('Cancelling installation as requirements are missing');
93
            exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
94
        }
95 69
96
        // First publish the config as we overwrite it later.
97 69
        $this->publishConfig();
98 69
99
        $this->info("Installing $preset preset");
100
101
        $storage = Storage::build([
102
            'driver' => 'local',
103
            'root' => __DIR__ . '/../../examples/' . $preset,
104
        ]);
105 69
        $appPathStorage = Storage::build([
106
            'driver' => 'local',
107 69
            'root' => base_path(),
108 69
        ]);
109
110
        foreach ($storage->allDirectories() as $directory) {
111 69
            if ($appPathStorage->makeDirectory($directory)) {
112
                foreach ($storage->files($directory) as $file) {
113
                    $appPathStorage->put($file, $storage->get($file));
114
                }
115
            }
116
        }
117
118 69
        $this->call('migrate');
119
        $this->publishAssets();
120 69
        $this->createSuperAdmin();
121 69
        $this->info('Finished installing preset!');
122
    }
123
124 69
    /**
125
     * Creates the default `admin.php` route configuration file.
126
     *
127
     * @return void
128
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
129
     */
130
    private function addRoutesFile()
131
    {
132
        $routesPath = base_path('routes');
133
134
        if (!$this->files->exists($routesPath)) {
135
            $this->files->makeDirectory($routesPath, 0755, true);
136
        }
137
138
        if (!$this->files->exists($routesPath . '/admin.php')) {
139
            $stub = $this->files->get(__DIR__ . '/stubs/admin.stub');
140
            $this->files->put($routesPath . '/admin.php', $stub);
141
        }
142
    }
143
144
    /**
145
     * Calls the command responsible for creation of the default superadmin user.
146
     *
147
     * @return void
148
     */
149
    private function createSuperAdmin()
150
    {
151
        if (!$this->option('no-interaction')) {
152
            $this->call('twill:superadmin');
153
        }
154
    }
155
156
    /**
157
     * Publishes the package configuration files.
158
     *
159
     * @return void
160
     */
161
    private function publishConfig()
162
    {
163
        $this->call('vendor:publish', [
164
            '--provider' => 'A17\Twill\TwillServiceProvider',
165
            '--tag' => 'config',
166
        ]);
167
    }
168
169
    /**
170
     * Publishes the package frontend assets.
171
     *
172
     * @return void
173
     */
174
    private function publishAssets()
175
    {
176
        $this->call('vendor:publish', [
177
            '--provider' => 'A17\Twill\TwillServiceProvider',
178
            '--tag' => 'assets',
179
        ]);
180
    }
181
182
    private function meetsPresetRequirements(string $preset)
183
    {
184
        if ($preset === 'blog') {
185
            if (!\Composer\InstalledVersions::isInstalled('kalnoy/nestedset')) {
186
                $this->warn('Missing nestedset, please install it using "composer require kalnoy/nestedset"');
187
                return false;
188
            }
189
        }
190
191
        return true;
192
    }
193
}
194