Passed
Branch develop (893064)
by Nuno
02:09
created

Installer::install()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 6.105

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 36
cts 42
cp 0.8571
rs 8.0541
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
final class Installer extends AbstractInstaller
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $name = 'install:database';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $description = 'Installs illuminate/database - Eloquent';
31
32
    /**
33
     * The config file path.
34
     */
35
    private const CONFIG_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'database.php';
36
37
    /**
38
     * The seeder file path.
39
     */
40
    private const SEEDER_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'DatabaseSeeder.php';
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function install(): void
46
    {
47 3
        $this->require('illuminate/database "5.6.*"');
48
49 3
        $this->task(
0 ignored issues
show
Bug introduced by
The method task() does not exist on LaravelZero\Framework\Co...ents\Database\Installer. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->/** @scrutinizer ignore-call */ 
50
               task(
Loading history...
50 3
            'Creating a default SQLite database',
51
            function () {
52 3
                if (File::exists(database_path('database.sqlite'))) {
53
                    return false;
54
                }
55 3
                File::makeDirectory($this->app->databasePath('migrations'), 0755, true, true);
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                File::makeDirectory($this->app->/** @scrutinizer ignore-call */ databasePath('migrations'), 0755, true, true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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