Passed
Push — master ( bf1cb1...07ceb6 )
by Vasyl
02:42
created

VariableServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A boot() 0 22 4
A replaceConfigsWithVariables() 0 17 2
1
<?php
2
3
namespace Fomvasss\Variable;
4
5
use Fomvasss\Variable\Models\Variable;
6
use Illuminate\Support\ServiceProvider;
7
8
class VariableServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap the application services.
12
     *
13
     * @return void
14
     */
15
    public function boot()
16
    {
17
        $this->publishes([
18
            __DIR__.'/../config/variables.php' => config_path('variables.php'),
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

18
            __DIR__.'/../config/variables.php' => /** @scrutinizer ignore-call */ config_path('variables.php'),
Loading history...
19
        ], 'variables-config');
20
21
        if (! class_exists('CreateVariablesTable')) {
22
             $timestamp = date('Y_m_d_His', time());
23
24
             $this->publishes([
25
                 __DIR__.'/../database/migrations/create_variables_table.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_variables_table.php",
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()? ( Ignorable by Annotation )

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

25
                 __DIR__.'/../database/migrations/create_variables_table.php.stub' => $this->app->/** @scrutinizer ignore-call */ databasePath()."/migrations/{$timestamp}_create_variables_table.php",

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...
26
             ], 'variables-migrations');
27
        }
28
29
        if ($this->app['config']->get('variables.config_key_for_vars')) {
30
            $this->replaceConfigsWithVariables();
31
        }
32
33
        if ($this->app->runningInConsole()) {
34
             $this->commands([
35
                 Commands\AllVariable::class,
36
                 Commands\GetVariable::class,
37
             ]);
38
        }
39
    }
40
41
    /**
42
     * Register the application services.
43
     *
44
     * @return void
45
     */
46
    public function register()
47
    {
48
        $this->mergeConfigFrom(__DIR__.'/../config/variables.php', 'variables');
49
50
        $this->app->singleton(VariableManagerContract::class, function () {
51
            $cacheRepo = $this->app->make(\Illuminate\Cache\Repository::class);
52
            return new VariableManager(new Variable, $cacheRepo, $this->app);
53
        });
54
    }
55
56
    protected function replaceConfigsWithVariables()
57
    {
58
        $this->app->booted(function () {
59
60
            // package config
61
            $config = $this->app['config']->get('variables');
62
63
            // replace configs with variables
64
            $variableConfig = $config['variable_config'];
65
66
            $variables = $this->app->make(VariableManagerContract::class)->all();
67
68
            foreach ($variables as $varKey => $varValue) {
69
70
                $configKey = $variableConfig[$varKey] ?? ($config['config_key_for_vars'] . '.' . $varKey);
71
72
                $this->app['config']->set($configKey, $varValue);
73
            }
74
        });
75
    }
76
}
77