VariableServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
eloc 33
c 4
b 0
f 0
dl 0
loc 73
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 1
A boot() 0 22 3
A replaceConfigsWithVariables() 0 23 5
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
        ], '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",
26
             ], 'migrations');
27
        }
28
29
        $this->replaceConfigsWithVariables();
30
31
        if ($this->app->runningInConsole()) {
32
             $this->commands([
33
                 Commands\AllVariable::class,
34
                 Commands\GetVariable::class,
35
                 Commands\SaveVariable::class,
36
                 Commands\CacheClearVariable::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
            $variableModel = $this->app['config']->get('variables.model_name');
53
            
54
            return new VariableManager(new $variableModel, $cacheRepo, $this->app);
55
        });
56
    }
57
58
    protected function replaceConfigsWithVariables()
59
    {
60
        $this->app->booted(function () {
61
62
            // Package config
63
            $config = $this->app['config']->get('variables');
64
65
            // Dynamic replace config keys with variables
66
            $variableConfig = $config['variable_config'];
67
68
            try {
69
                $variables = $this->app->make(VariableManagerContract::class)->all();
70
71
                foreach ($variables as $var) {
72
                    if (! empty($variableConfig[$var->key])) {
73
                        $this->app['config']->set($variableConfig[$var->key], $var->value);
74
                    }
75
                    if (! empty($config['config_key_for_vars'])) {
76
                        $this->app['config']->set($config['config_key_for_vars'] . '.' . $var->key, $var->value);
77
                    }
78
                }
79
            } catch (\Exception $exception) {
80
                $this->app['log']->warning($exception->getMessage());
81
            }
82
        });
83
    }
84
}
85