FeatureServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 3
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 1
A register() 0 12 1
A registerBladeDirectives() 0 5 1
A registerBladeFeatureDirective() 0 10 1
A registerBladeFeatureForDirective() 0 10 1
A registerConsoleCommand() 0 8 2
1
<?php
2
3
namespace LaravelFeature\Provider;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
use LaravelFeature\Domain\Repository\FeatureRepositoryInterface;
8
use LaravelFeature\Console\Command\ScanViewsForFeaturesCommand;
9
10
class FeatureServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap any application services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        $this->loadMigrationsFrom(__DIR__.'/../Migration');
20
21
        $this->publishes([
22
            __DIR__.'/../Config/features.php' => config_path('features.php'),
23
        ]);
24
25
        $this->registerBladeDirectives();
26
    }
27
28
    /**
29
     * Register any application services.
30
     *
31
     * @return void
32
     */
33
    public function register()
34
    {
35
        $this->mergeConfigFrom(__DIR__.'/../Config/features.php', 'features');
36
37
        $config = $this->app->make('config');
38
39
        $this->app->bind(FeatureRepositoryInterface::class, function () use ($config) {
40
            return app()->make($config->get('features.repository'));
41
        });
42
43
        $this->registerConsoleCommand();
44
    }
45
46
    private function registerBladeDirectives()
47
    {
48
        $this->registerBladeFeatureDirective();
49
        $this->registerBladeFeatureForDirective();
50
    }
51
52
    private function registerBladeFeatureDirective()
53
    {
54
        Blade::directive('feature', function ($featureName) {
55
            return "<?php if (app(\\LaravelFeature\\Domain\\FeatureManager::class)->isEnabled($featureName)): ?>";
56
        });
57
58
        Blade::directive('endfeature', function () {
59
            return '<?php endif; ?>';
60
        });
61
    }
62
63
    private function registerBladeFeatureForDirective()
64
    {
65
        Blade::directive('featurefor', function ($args) {
66
            return "<?php if (app(\\LaravelFeature\\Domain\\FeatureManager::class)->isEnabledFor($args)): ?>";
67
        });
68
69
        Blade::directive('endfeaturefor', function () {
70
            return '<?php endif; ?>';
71
        });
72
    }
73
74
    private function registerConsoleCommand()
75
    {
76
        if ($this->app->runningInConsole()) {
77
            $this->commands([
78
                ScanViewsForFeaturesCommand::class
79
            ]);
80
        }
81
    }
82
}
83