JudgeServiceProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 89
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3
ccs 43
cts 43
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 22 2
A boot() 0 10 2
A loadConfigs() 0 6 2
A getConfigs() 0 8 1
A getAdapterClass() 0 15 3
1
<?php
2
3
namespace Jlis\Judge;
4
5
use Jlis\Judge\Judges\ValueJudge;
6
use Jlis\Judge\Judges\FeatureJudge;
7
use Illuminate\Support\ServiceProvider;
8
9
/**
10
 * @author Julius Ehrlich <[email protected]>
11
 */
12
class JudgeServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Indicates if loading of the provider is deferred.
16
     *
17
     * @var bool
18
     */
19
    protected $defer = false;
20
21
    /**
22
     * Register the service provider.
23
     */
24 2
    public function register()
25
    {
26 2
        $app = $this->app;
27 2
        $this->loadConfigs();
28 2
        $config = $app->make('config')->get('judge');
29 2
        $adapter = $this->app->make($this->getAdapterClass($config));
30 2
        $voters = isset($config['voters']) ? $config['voters'] : [];
31
32 2
        $app->bind(
33 2
            'feature',
34
            function () use ($adapter, $voters) {
35 2
                return new FeatureJudge($adapter, $voters);
36
            }
37 2
        );
38
39 2
        $app->bind(
40 2
            'value',
41 2
            function () use ($adapter, $voters) {
42 2
                return new ValueJudge($adapter, $voters);
43
            }
44 2
        );
45 2
    }
46
47
    /**
48
     * Bootstrap the application events.
49
     */
50 3
    public function boot()
51
    {
52 1
        $publishes = [];
53 1
        $app = $this->app;
54 1
        foreach ($this->getConfigs() as $key => $path) {
55
            /* @var $app array */
56 1
            $publishes[$path] = $app['path.config'].DIRECTORY_SEPARATOR.$key.'.php';
57 3
        }
58 1
        $this->publishes($publishes, 'config');
59 3
    }
60
61 2
    private function loadConfigs()
62
    {
63 2
        foreach ($this->getConfigs() as $key => $path) {
64 2
            $this->mergeConfigFrom($path, $key);
65 2
        }
66 2
    }
67
68
    /**
69
     * @return array
70
     */
71 3
    private function getConfigs()
72
    {
73
        return [
74 3
            'features' => __DIR__.'/../config/features.php',
75 3
            'values' => __DIR__.'/../config/values.php',
76 3
            'judge' => __DIR__.'/../config/judge.php',
77 3
        ];
78
    }
79
80
    /**
81
     * @param array $config
82
     *
83
     * @return string
84
     */
85 2
    private function getAdapterClass(array $config)
86
    {
87
        $map = [
88 2
            'config' => Adapters\ConfigAdapter::class,
89 2
            'redis' => Adapters\RedisAdapter::class,
90 2
            'cache' => Adapters\CacheAdapter::class,
91 2
        ];
92
93 2
        $className = isset($config['adapter']) ? $config['adapter'] : 'config';
94 2
        if (isset($map[$className])) {
95 1
            return $map[$className];
96
        }
97
98 1
        return $className;
99
    }
100
}
101