DbExportHandlerServiceProvider::loadClasses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Nwidart\DbExporter;
2
3
use Illuminate\Support\ServiceProvider;
4
use Illuminate\Support\Facades\Config;
5
6
7
class DbExportHandlerServiceProvider extends ServiceProvider
8
{
9
    protected $defer = false;
10
11
    /**
12
     * @var DbMigrations $migrator
13
     */
14
    protected $migrator;
15
16
    /**
17
     * @var DbSeeding $seeder
18
     */
19
    protected $seeder;
20
    /**
21
     * @var DbExportHandler $handler
22
     */
23
    protected $handler;
24
25
    public function boot()
26
    {
27
        $this->publishes([__DIR__.'/../../config/config.php' => config_path('db-exporter.php')], 'config');
28
    }
29
30
    public function register()
31
    {
32
        // Load the classes
33
        $this->loadClasses();
34
35
        // Register the base export handler class
36
        $this->registerDbExportHandler();
37
38
        // Handle the artisan commands
39
        $this->registerCommands();
40
41
        // Load the alias
42
        $this->loadAlias();
43
44
        // Default config
45
        $this->mergeConfigFrom(
46
            __DIR__.'/../../config/config.php', 'db-exporter'
47
        );
48
    }
49
50
    /**
51
     * Load to classes
52
     */
53
    protected function loadClasses()
54
    {
55
        // Instatiate a new DbMigrations class to send to the handler
56
        $this->migrator = new DbMigrations($this->getDatabaseName());
57
58
        // Instatiate a new DbSeeding class to send to the handler
59
        $this->seeder = new DbSeeding($this->getDatabaseName());
60
61
        // Instantiate the handler
62
        $this->handler = new DbExportHandler($this->migrator, $this->seeder);
63
    }
64
65
    /**
66
     * Get the database name from the app/config/database.php file
67
     * @return String
68
     */
69
    private function getDatabaseName()
70
    {
71
        $connType = config('database.default');
72
        $database = config('database.connections.' .$connType );
73
74
        return $database['database'];
75
    }
76
77
    public function provides()
78
    {
79
        return array('DbExportHandler');
80
    }
81
82
    /**
83
     * Register the needed commands
84
     */
85
    public function registerCommands()
86
    {
87
        $this->registerMigrationsCommand();
88
        $this->registerSeedsCommand();
89
        $this->registerRemoteCommand();
90
        $this->commands(
91
            'dbe::migrations',
92
            'dbe::seeds',
93
            'dbe::remote'
94
        );
95
    }
96
97
    /**
98
     * Register the migrations command
99
     */
100
    protected function registerMigrationsCommand()
101
    {
102
        $this->app['dbe::migrations'] = $this->app->share(function()
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

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...
103
        {
104
            return new Commands\MigrationsGeneratorCommand($this->handler);
105
        });
106
    }
107
108
    /**
109
     * Register the seeds command
110
     */
111
    protected function registerSeedsCommand()
112
    {
113
        $this->app['dbe::seeds'] = $this->app->share(function()
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

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...
114
        {
115
            return new Commands\SeedGeneratorCommand($this->handler);
116
        });
117
    }
118
119
    protected function registerRemoteCommand()
120
    {
121
        $this->app['dbe::remote'] = $this->app->share(function()
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

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...
122
        {
123
            return new Commands\CopyToRemoteCommand(new Server);
124
        });
125
    }
126
127
    /**
128
     * Register the Export handler class
129
     */
130
    protected function registerDbExportHandler()
131
    {
132
        $this->app['DbExportHandler'] = $this->app->share(function()
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

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...
133
        {
134
            return $this->handler;
135
        });
136
    }
137
138
    /**
139
     * Load the alias = One less install step for the user
140
     */
141
    protected function loadAlias()
142
    {
143
        $this->app->booting(function()
144
        {
145
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
146
            $loader->alias('DbExportHandler', 'Nwidart\DbExporter\Facades\DbExportHandler');
147
148
            // some users migrating from 5.0 don't have Str alias registered
149
            if (! class_exists('\Str')) {
150
                $loader->alias('Str',\Illuminate\Support\Str::class);
151
            }
152
        });
153
    }
154
155
}