PConnectorServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 20
c 4
b 0
f 0
dl 0
loc 76
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 11 1
A boot() 0 18 3
A getMigrationFileName() 0 9 1
1
<?php
2
3
namespace MedianetDev\PConnector;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\ServiceProvider;
8
9
class PConnectorServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     */
14
    public function boot(Filesystem $filesystem)
15
    {
16
        /*
17
         * Optional methods to load your package assets
18
         */
19
        // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'p-connector');
20
        // $this->loadViewsFrom(__DIR__.'/../resources/views', 'p-connector');
21
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
22
        // $this->loadRoutesFrom(__DIR__.'/routes.php');
23
24
        if ($this->app->runningInConsole() && function_exists('config_path')) {  // function not available and 'publish' not relevant in Lumen
25
            $this->publishes([
26
                __DIR__.'/../config/config.php' => config_path('p-connector.php'),
27
            ], 'config');
28
29
            $this->publishes([
30
                __DIR__.'/../database/migrations/create_p_connector_table.php.stub' => $this->getMigrationFileName($filesystem),
31
            ], 'migrations');
32
33
            // Publishing the views.
34
            /*$this->publishes([
35
                __DIR__.'/../resources/views' => resource_path('views/vendor/p-connector'),
36
            ], 'views');*/
37
38
            // Publishing assets.
39
            /*$this->publishes([
40
                __DIR__.'/../resources/assets' => public_path('vendor/p-connector'),
41
            ], 'assets');*/
42
43
            // Publishing the translation files.
44
            /*$this->publishes([
45
                __DIR__.'/../resources/lang' => resource_path('lang/vendor/p-connector'),
46
            ], 'lang');*/
47
48
            // Registering package commands.
49
            // $this->commands([]);
50
        }
51
    }
52
53
    /**
54
     * Register the application services.
55
     */
56
    public function register()
57
    {
58
        // Automatically apply the package configuration
59
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'p-connector');
60
61
        // Register the main class to use with the facade
62
        $this->app->bind('p-connector', function () {
63
            $httpClient = config('p-connector.http_client');
64
            $httpClient = new $httpClient;
65
66
            return new PConnector($httpClient);
67
        });
68
    }
69
70
    /**
71
     * Returns existing migration file if found, else uses the current timestamp.
72
     *
73
     * @param  Filesystem  $filesystem
74
     * @return string
75
     */
76
    protected function getMigrationFileName(Filesystem $filesystem): string
77
    {
78
        $timestamp = date('Y_m_d_His');
79
80
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
81
            ->flatMap(function ($path) use ($filesystem) {
82
                return $filesystem->glob($path.'*_create_p_connector_table.php');
83
            })->push($this->app->databasePath()."/migrations/{$timestamp}_create_p_connector_table.php")
84
            ->first();
85
    }
86
}
87