CombinedConfigServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 24
ccs 0
cts 15
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Koomai\LaravelConfig\Providers;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Support\ServiceProvider;
7
use Koomai\LaravelConfig\DatabaseConfig;
8
9
class CombinedConfigServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     */
14
    public function boot()
15
    {
16
        // If app config is already cached, simply use the cached config key/values.
17
        // You will have to clear config cache to merge new database config
18
        // values with application config and cache them again.
19
        if (!file_exists($this->app->getCachedConfigPath())) {
20
            // Load database config
21
            $databaseConfig = $this->app['cache']->rememberForever(
22
                $this->app['config']->get('database-config.cache_key'),
23
                function () {
24
                    return DatabaseConfig::all()
25
                                         ->mapWithKeys(function ($config) {
26
                                             return [$config->name => $config->value];
27
                                         })
28
                                         ->all();
29
                }
30
            );
31
32
            $config = $this->app['config']->all();
33
            $items = array_merge_recursive_distinct($config, $databaseConfig);
34
            $repository = new Repository($items);
35
36
            // Register custom instance in container
37
            $this->app->instance('config', $repository);
38
        }
39
    }
40
}
41