Test Failed
Push — stable ( 994ca0...7d5c83 )
by Nuno
04:30
created

Installer::install()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 69
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 6.105

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 69
ccs 36
cts 42
cp 0.8571
rs 8.425
c 0
b 0
f 0
cc 6
nc 1
nop 0
crap 6.105

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