Completed
Push — master ( 044c50...7232aa )
by Ben
02:18
created

ServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 1 Features 4
Metric Value
wmc 10
c 9
b 1
f 4
lcom 1
cbo 3
dl 0
loc 76
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 1
A register() 0 10 2
A provides() 0 7 1
B selectStorage() 0 29 6
1
<?php
2
3
namespace Benrowe\Laravel\Config;
4
5
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6
7
/**
8
 * Service Provider for Config
9
 *
10
 * @package Benrowe\Laravel\Config;
11
 */
12
class ServiceProvider extends BaseServiceProvider
13
{
14
    protected $defer = false;
15
16
    /**
17
     * @inheritdoc
18
     */
19
    public function boot()
20
    {
21
        # publish necessary files
22
        $this->publishes([
23
            __DIR__ . '/config/config.php' => config_path('config.php'),
24
        ], 'config');
25
26
        $this->publishes([
27
            __DIR__.'/migrations/' => database_path('migrations'),
28
        ], 'migrations');
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function register()
35
    {
36
        $this->app->singleton('config-runtime', function () {
37
            $storage = $this->selectStorage($this->app['config']);
38
            if ($storage === null) {
39
                $storage = [];
40
            }
41
            return new Config($storage);
42
        });
43
    }
44
45
    /**
46
     * Define the services this provider will build & provide
47
     *
48
     * @return array
49
     */
50
    public function provides()
51
    {
52
        return [
53
            'Benrowe\Laravel\Config\Config',
54
            'config-runtime'
55
        ];
56
    }
57
58
    protected function selectStorage($config)
59
    {
60
        $storage = null;
61
        if ($config->get('config.storage.enabled')) {
62
            $driver = $config->get('config.storage.driver', 'file');
63
            switch ($driver) {
64
                case 'pdo':
65
                    $connection = $config->get('config.storage.connection');
66
                    $table = $this->app['db']->getTablePrefix() . 'config';
67
                    $pdo = $this->app['db']->connection($connection)->getPdo();
68
                    $storage = new Benrowe\Laravel\Config\Storage\Pdo($pdo, $table);
69
                    break;
70
                case 'redis':
71
                    $connection = $config->get('config.storage.connection');
72
                    $storage = new RedisStorage($this->app['redis']->connection($connection));
73
                    break;
74
                case 'custom':
75
                    $class = $config->get('config.storage.provider');
76
                    $storage = $this->app->make($class);
77
                    break;
78
                case 'file':
79
                default:
80
                    $path = $config->get('config.storage.path');
81
                    $storage = new Benrowe\Laravel\Config\Storage\File($path);
82
                    break;
83
            }
84
        }
85
        return $storage;
86
    }
87
}
88