Installer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 85.45%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 70
c 2
b 0
f 0
dl 0
loc 118
rs 10
ccs 47
cts 55
cp 0.8545

1 Method

Rating   Name   Duplication   Size   Complexity  
B install() 0 93 8
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Laravel Zero.
7
 *
8
 * (c) Nuno Maduro <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace LaravelZero\Framework\Components\Database;
15
16
use Illuminate\Support\Facades\File;
17
use Illuminate\Support\Str;
18
use LaravelZero\Framework\Components\AbstractInstaller;
19
20
/**
21
 * @internal
22
 */
23
final class Installer extends AbstractInstaller
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $name = 'install:database';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected $description = 'Eloquent ORM: Database layer';
34
35
    /**
36
     * The config file path.
37
     */
38
    private const CONFIG_FILE = __DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'database.php';
39
40
    /**
41
     * The seeder file path.
42
     */
43
    private const SEEDER_FILE = __DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php';
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4
    public function install(): void
49
    {
50 4
        $this->require('illuminate/database "^6.0"');
51 4
        $this->require('fzaninotto/faker "^1.4"', true);
52
53 4
        $this->task(
54 4
            'Creating a default SQLite database',
55
            function () {
56 4
                if (File::exists(database_path('database.sqlite'))) {
57
                    return false;
58
                }
59
60 4
                File::makeDirectory($this->app->databasePath(), 0755, true, true);
61
62 4
                File::put(
63 4
                    $this->app->databasePath('database.sqlite'),
64 4
                    ''
65
                );
66 4
            }
67
        );
68
69 4
        $this->task(
70 4
            'Creating migrations folder',
71
            function () {
72 4
                if (File::exists($this->app->databasePath('migrations'))) {
73
                    return false;
74
                }
75
76 4
                File::makeDirectory($this->app->databasePath('migrations'), 0755, true, true);
77 4
            }
78
        );
79
80 4
        $this->task(
81 4
            'Creating seeds folders and files',
82
            function () {
83 4
                if (File::exists($this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php'))) {
84
                    return false;
85
                }
86
87 4
                File::makeDirectory($this->app->databasePath('seeds'), 0755, false, true);
88
89 4
                File::copy(
90 4
                    self::SEEDER_FILE,
91 4
                    $this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php')
92
                );
93 4
            }
94
        );
95
96 4
        $this->task(
97 4
            'Creating factories folder',
98
            function () {
99 4
                if (File::exists($this->app->databasePath('factories'))) {
100
                    return false;
101
                }
102
103 4
                File::makeDirectory($this->app->databasePath('factories'), 0755, true, true);
104 4
            }
105
        );
106
107 4
        $this->task(
108 4
            'Creating default database configuration',
109
            function () {
110 4
                if (File::exists(config_path('database.php'))) {
111 4
                    return false;
112
                }
113
                File::copy(
114
                    self::CONFIG_FILE,
115
                    config_path('database.php')
116
                );
117 4
            }
118
        );
119
120 4
        $this->task(
121 4
            'Updating .gitignore',
122
            function () {
123 4
                $gitignorePath = base_path('.gitignore');
124 4
                if (File::exists($gitignorePath)) {
125 4
                    $contents = File::get($gitignorePath);
126 4
                    $neededLine = '/database/database.sqlite';
127 4
                    if (! Str::contains($contents, $neededLine)) {
128 4
                        File::append($gitignorePath, $neededLine.PHP_EOL);
129
130 4
                        return true;
131
                    }
132
                }
133
134
                return false;
135 4
            }
136
        );
137
138 4
        $this->info('Usage:');
139 4
        $this->comment(
140 4
            '
141
$ php <your-application-name> make:migration create_users_table
142
$ php <your-application-name> migrate
143
144
use DB;
145
146
DB::table(\'users\')->insert(
147
    [\'email\' => \'[email protected]\']
148
);
149
150
$users = DB::table(\'users\')->get();
151
        '
152
        );
153 4
    }
154
}
155