TranslatableBootFormsServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 5
c 7
b 0
f 0
lcom 1
cbo 5
dl 0
loc 82
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A provides() 0 7 1
B register() 0 31 2
A getLocales() 0 4 1
1
<?php namespace Propaganistas\LaravelTranslatableBootForms;
2
3
use Illuminate\Foundation\Application;
4
use Illuminate\Support\ServiceProvider;
5
6
class TranslatableBootFormsServiceProvider extends ServiceProvider
7
{
8
9
    /**
10
     * Indicates if loading of the provider is deferred.
11
     *
12
     * @var bool
13
     */
14
    protected $defer = true;
15
16
    /**
17
     * Boot the application events.
18
     *
19
     * @return void
20
     */
21
    public function boot()
22
    {
23
        $this->publishes([
24
            __DIR__ . '/../config/config.php' => config_path('translatable-bootforms.php'),
25
        ], 'config');
26
    }
27
28
    /**
29
     * Register the service provider.
30
     *
31
     * @return void
32
     */
33
    public function register()
34
    {
35
        $this->mergeConfigFrom(
36
            __DIR__ . '/../config/config.php', 'translatable-bootforms'
37
        );
38
39
        // Override BootForm's form builder in order to get model binding
40
        // between BootForm & TranslatableBootForm working.
41
        $this->app->singleton('adamwathan.form', function ($app) {
42
            $formBuilder = new Form\FormBuilder();
43
            $formBuilder->setLocales($this->getLocales());
44
            $formBuilder->setErrorStore($app['adamwathan.form.errorstore']);
45
            $formBuilder->setOldInputProvider($app['adamwathan.form.oldinput']);
46
47
            $token = version_compare(Application::VERSION, '5.4', '<')
48
                ? $app['session.store']->getToken()
49
                : $app['session.store']->token();
50
51
            $formBuilder->setToken($token);
52
53
            return $formBuilder;
54
        });
55
56
        // Define TranslatableBootForm.
57
        $this->app->singleton('translatable-bootform', function ($app) {
58
            $form = new TranslatableBootForm($app['bootform']);
59
            $form->locales($this->getLocales());
60
61
            return $form;
62
        });
63
    }
64
65
    /**
66
     * Get the services provided by the provider.
67
     *
68
     * @return array
69
     */
70
    public function provides()
71
    {
72
        return [
73
            'adamwathan.form',
74
            'translatable-bootform',
75
        ];
76
    }
77
78
    /**
79
     * Get Translatable's locales.
80
     *
81
     * @return array
82
     */
83
    protected function getLocales()
84
    {
85
        return with(new Translatable\TranslatableWrapper)->getLocales();
86
    }
87
}
88