Passed
Push — laravel-8-support ( f89d92...92a2fc )
by Quentin
11:50
created

Install::addRoutesFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Database\DatabaseManager;
6
use Illuminate\Filesystem\Filesystem;
7
8
class Install extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'twill:install';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Install Twill into your Laravel application';
23
24
    /**
25
     * @var Filesystem
26
     */
27
    protected $files;
28
29
    /**
30
     * @var DatabaseManager
31
     */
32
    protected $db;
33
34
    /**
35
     * @param Filesystem $files
36
     * @param DatabaseManager $db
37
     */
38 69
    public function __construct(Filesystem $files, DatabaseManager $db)
39
    {
40 69
        parent::__construct();
41
42 69
        $this->files = $files;
43 69
        $this->db = $db;
44 69
    }
45
46
    /**
47
     * Executes the console command.
48
     *
49
     * @return void
50
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
51
     */
52 69
    public function handle()
53
    {
54
        //check the database connection before installing
55
        try {
56 69
            $this->db->connection()->getPdo();
57
        } catch (\Exception $e) {
58
            $this->error('Could not connect to the database, please check your configuration:' . "\n" . $e);
59
            return;
60
        }
61
62 69
        $this->addRoutesFile();
63 69
        $this->call('migrate');
64 69
        $this->publishConfig();
65 69
        $this->publishAssets();
66 69
        $this->createSuperAdmin();
67 69
        $this->info('All good!');
68 69
    }
69
70
    /**
71
     * Creates the default `admin.php` route configuration file.
72
     *
73
     * @return void
74
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
75
     */
76 69
    private function addRoutesFile()
77
    {
78 69
        $routesPath = base_path('routes');
79
80 69
        if (!$this->files->exists($routesPath)) {
81
            $this->files->makeDirectory($routesPath, 0755, true);
82
        }
83
84 69
        if (!$this->files->exists($routesPath . '/admin.php')) {
85 1
            $stub = $this->files->get(__DIR__ . '/stubs/admin.stub');
86 1
            $this->files->put($routesPath . '/admin.php', $stub);
87
        }
88 69
    }
89
90
    /**
91
     * Calls the command responsible for creation of the default superadmin user.
92
     *
93
     * @return void
94
     */
95 69
    private function createSuperAdmin()
96
    {
97 69
        $this->call('twill:superadmin');
98 69
    }
99
100
    /**
101
     * Publishes the package configuration files.
102
     *
103
     * @return void
104
     */
105 69
    private function publishConfig()
106
    {
107 69
        $this->call('vendor:publish', [
108 69
            '--provider' => 'A17\Twill\TwillServiceProvider',
109
            '--tag' => 'config',
110
        ]);
111 69
    }
112
113
    /**
114
     * Publishes the package frontend assets.
115
     *
116
     * @return void
117
     */
118 69
    private function publishAssets()
119
    {
120 69
        $this->call('vendor:publish', [
121 69
            '--provider' => 'A17\Twill\TwillServiceProvider',
122
            '--tag' => 'assets',
123
        ]);
124 69
    }
125
126
}
127