Completed
Push — stable ( d6da9c...063f28 )
by Nuno
03:13
created

Provider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 103
ccs 42
cts 43
cp 0.9767
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 5 2
A register() 0 10 2
A boot() 0 20 2
A registerDatabaseService() 0 18 2
A registerMigrationService() 0 22 2
1
<?php
2
3
/**
4
 * This file is part of Laravel Zero.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace LaravelZero\Framework\Components\Database;
13
14
use Illuminate\Support\Facades\File;
15
use LaravelZero\Framework\Components\AbstractComponentProvider;
16
17
/**
18
 * This is the Laravel Zero Framework Database Component Provider Implementation.
19
 */
20
class Provider extends AbstractComponentProvider
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 19
    public function isAvailable(): bool
26
    {
27 19
        return class_exists(\Illuminate\Database\DatabaseServiceProvider::class)
28 19
            && is_array(config('database', false));
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 19
    public function register(): void
35
    {
36 19
        if (File::exists(config_path('database.php'))) {
37 19
            $this->mergeConfigFrom(config_path('database.php'), 'database');
38
        }
39
40 19
        $this->registerDatabaseService();
41
42 19
        $this->registerMigrationService();
43 19
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 19
    public function boot(): void
49
    {
50 19
        if ($this->app->environment() !== 'production') {
51 19
            $this->commands([\Illuminate\Database\Console\Seeds\SeederMakeCommand::class]);
52 19
            $this->commands([\Illuminate\Database\Console\Migrations\MigrateMakeCommand::class]);
53
        }
54
55 19
        $this->commands(
56
            [
57 19
                \Illuminate\Database\Console\Migrations\FreshCommand::class,
58
                \Illuminate\Database\Console\Migrations\InstallCommand::class,
59
                \Illuminate\Database\Console\Migrations\MigrateCommand::class,
60
                \Illuminate\Database\Console\Migrations\RefreshCommand::class,
61
                \Illuminate\Database\Console\Migrations\ResetCommand::class,
62
                \Illuminate\Database\Console\Migrations\RollbackCommand::class,
63
                \Illuminate\Database\Console\Migrations\StatusCommand::class,
64
                \Illuminate\Database\Console\Seeds\SeedCommand::class,
65
            ]
66
        );
67 19
    }
68
69
    /**
70
     * Registers the database service.
71
     *
72
     * Makes this Capsule Instance available globally via static methods.
73
     *
74
     * @return void
75
     */
76 19
    protected function registerDatabaseService(): void
77
    {
78 19
        $this->app['config']->set('database.migrations', 'migrations');
79 19
        $this->app->alias('db', \Illuminate\Database\ConnectionResolverInterface::class);
80
81 19
        $this->app->register(\Illuminate\Database\DatabaseServiceProvider::class);
82
83 19
        $this->app->make(\Illuminate\Database\Capsule\Manager::class)
84 19
            ->setAsGlobal();
85
86 19
        if (File::exists($this->app->databasePath('seeds'))) {
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87 11
            collect(File::files($this->app->databasePath('seeds')))->each(
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
88 11
                function ($file) {
89
                    File::requireOnce($file);
90 11
                }
91
            );
92
        }
93 19
    }
94
95
    /**
96
     * Registers the migration service.
97
     *
98
     * @return void
99
     */
100 19
    protected function registerMigrationService(): void
101
    {
102 19
        $config = $this->app['config'];
103 19
        $config->set('database.migrations', $config->get('database.migrations') ?: 'migrations');
104 19
        $this->app->register(\Illuminate\Database\MigrationServiceProvider::class);
105
106 19
        $this->app->alias(
107 19
            'migration.repository',
108 19
            \Illuminate\Database\Migrations\MigrationRepositoryInterface::class
109
        );
110
111 19
        $this->app->singleton('migrator', function ($app) {
112 19
            $repository = $app['migration.repository'];
113
114 19
            return new Migrator($repository, $app['db'], $app['files']);
115 19
        });
116
117 19
        $this->app->alias(
118 19
            'migrator',
119 19
            \Illuminate\Database\Migrations\Migrator::class
120
        );
121 19
    }
122
}
123