Completed
Pull Request — stable (#353)
by Matt
04:33
created

Installer::install()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 81
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 81
ccs 0
cts 49
cp 0
rs 7.9612
c 0
b 0
f 0
cc 7
nc 1
nop 0
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Str;
17
use Illuminate\Support\Facades\File;
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
    public function install(): void
49
    {
50
        $this->require('illuminate/database "5.8.*"');
51
        $this->require('fzaninotto/faker "^1.4"', true);
52
53
        $this->task(
54
            'Creating a default SQLite database',
55
            function () {
56
                if (File::exists(database_path('database.sqlite'))) {
57
                    return false;
58
                }
59
                File::makeDirectory($this->app->databasePath('migrations'), 0755, true, true);
60
61
                File::put(
62
                    $this->app->databasePath('database.sqlite'),
63
                    ''
64
                );
65
            }
66
        );
67
68
        $this->task(
69
            'Creating seeds folders and files',
70
            function () {
71
                if (File::exists($this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php'))) {
72
                    return false;
73
                }
74
75
                File::makeDirectory($this->app->databasePath('seeds'), 0755, false, true);
76
77
                File::copy(
78
                    self::SEEDER_FILE,
79
                    $this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php')
80
                );
81
            }
82
        );
83
84
        $this->task(
85
            'Creating factories folder',
86
            function () {
87
                if (File::exists($this->app->databasePath('factories'))) {
88
                    return false;
89
                }
90
91
                File::makeDirectory($this->app->databasePath('factories'), 0755, true, true);
92
            }
93
        );
94
95
        $this->task(
96
            'Creating default database configuration',
97
            function () {
98
                if (File::exists(config_path('database.php'))) {
99
                    return false;
100
                }
101
                File::copy(
102
                    self::CONFIG_FILE,
103
                    config_path('database.php')
104
                );
105
            }
106
        );
107
108
        $this->task(
109
            'Updating .gitignore',
110
            function () {
111
                $gitignorePath = base_path('.gitignore');
112
                if (File::exists($gitignorePath)) {
113
                    $contents = File::get($gitignorePath);
114
                    $neededLine = '/database/database.sqlite';
115
                    if (! Str::contains($contents, $neededLine)) {
116
                        File::append($gitignorePath, $neededLine.PHP_EOL);
117
118
                        return true;
119
                    }
120
                }
121
122
                return false;
123
            }
124
        );
125
126
        $this->info('Usage:');
127
        $this->comment(
128
            '
129
$ php <your-application-name> make:migration create_users_table
130
$ php <your-application-name> migrate
131
132
use DB;
133
134
DB::table(\'users\')->insert(
135
    [\'email\' => \'[email protected]\']
136
);
137
138
$users = DB::table(\'users\')->get();
139
        '
140
        );
141
    }
142
}
143