Passed
Push — master ( 3a3605...2e332d )
by Quentin
06:36 queued 10s
created

Install::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 16
ccs 8
cts 11
cp 0.7272
crap 2.0811
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\DatabaseManager;
7
use Illuminate\Filesystem\Filesystem;
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';
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
     */
39 55
    public function __construct(Filesystem $files, DatabaseManager $db)
40
    {
41 55
        parent::__construct();
42
43 55
        $this->files = $files;
44 55
        $this->db = $db;
45 55
    }
46
47
    /**
48
     * Executes the console command.
49
     *
50
     * @return void
51
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
52
     */
53 55
    public function handle()
54
    {
55
        //check the database connection before installing
56
        try {
57 55
            $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
63 55
        $this->addRoutesFile();
64 55
        $this->call('migrate');
65 55
        $this->publishConfig();
66 55
        $this->publishAssets();
67 55
        $this->createSuperAdmin();
68 55
        $this->info('All good!');
69 55
    }
70
71
    /**
72
     * Creates the default `admin.php` route configuration file.
73
     *
74
     * @return void
75
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
76
     */
77 55
    private function addRoutesFile()
78
    {
79 55
        $routesPath = base_path('routes');
80
81 55
        if (!$this->files->exists($routesPath)) {
82 1
            $this->files->makeDirectory($routesPath, 0755, true);
83
        }
84
85 55
        if (!$this->files->exists($routesPath . '/admin.php')) {
86 1
            $stub = $this->files->get(__DIR__ . '/stubs/admin.stub');
87 1
            $this->files->put($routesPath . '/admin.php', $stub);
88
        }
89 55
    }
90
91
    /**
92
     * Calls the command responsible for creation of the default superadmin user.
93
     *
94
     * @return void
95
     */
96 55
    private function createSuperAdmin()
97
    {
98 55
        $this->call('twill:superadmin');
99 55
    }
100
101
    /**
102
     * Publishes the package configuration files.
103
     *
104
     * @return void
105
     */
106 55
    private function publishConfig()
107
    {
108 55
        $this->call('vendor:publish', [
109 55
            '--provider' => 'A17\Twill\TwillServiceProvider',
110
            '--tag' => 'config',
111
        ]);
112 55
    }
113
114
    /**
115
     * Publishes the package frontend assets.
116
     *
117
     * @return void
118
     */
119 55
    private function publishAssets()
120
    {
121 55
        $this->call('vendor:publish', [
122 55
            '--provider' => 'A17\Twill\TwillServiceProvider',
123
            '--tag' => 'assets',
124
        ]);
125 55
    }
126
127
}
128