Completed
Push — master ( edb9ee...b465c5 )
by
unknown
10:14
created

NovaTranslationServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 14 2
A bootPackage() 0 10 1
A isNovaInstalled() 0 4 1
A bootRoutes() 0 10 2
A loadNovaTranslations() 0 14 2
1
<?php
2
3
namespace BBSLab\NovaTranslation;
4
5
use BBSLab\NovaTranslation\Http\Middleware\Authorize;
6
use BBSLab\NovaTranslation\Models\Locale;
7
use BBSLab\NovaTranslation\Models\Observers\LocaleObserver;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
10
11
class NovaTranslationServiceProvider extends BaseServiceProvider
12
{
13
    /**
14
     * Package ID.
15
     *
16
     * @var string
17
     */
18
    const PACKAGE_ID = 'nova-translation';
19
20
    /**
21
     * Bootstrap Kernel.
22
     *
23
     * @return void
24
     */
25
    public function boot()
26
    {
27
        $this->bootPackage();
28
29
        if ($this->isNovaInstalled()) {
30
            $this->app->booted(function () {
31
                $this->bootRoutes();
32
            });
33
34
            $this->loadNovaTranslations();
35
        }
36
37
        Locale::observe(LocaleObserver::class);
38
    }
39
40
    /**
41
     * Boot Laravel package.
42
     *
43
     * @return void
44
     */
45
    protected function bootPackage()
46
    {
47
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', static::PACKAGE_ID);
48
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
49
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', static::PACKAGE_ID);
50
51
        $this->publishes([
52
            __DIR__.'/../../../../config/config.php' => base_path('config/'.static::PACKAGE_ID.'.php'),
53
        ]);
54
    }
55
56
    /**
57
     * Check if Laravel Nova is installed.
58
     *
59
     * @return bool
60
     */
61
    protected function isNovaInstalled()
62
    {
63
        return class_exists('Laravel\Nova\Nova');
64
    }
65
66
    /**
67
     * Register the tool's routes.
68
     *
69
     * @return void
70
     */
71
    protected function bootRoutes()
72
    {
73
        if ($this->app->routesAreCached()) {
74
            return;
75
        }
76
77
        Route::middleware(['nova', Authorize::class])
78
            ->prefix('nova-vendor/'.static::PACKAGE_ID)
79
            ->group(__DIR__.'/../routes/api.php');
80
    }
81
82
    /**
83
     * Load prefixed Nova translations.
84
     *
85
     * @return void
86
     */
87
    protected function loadNovaTranslations()
88
    {
89
        $file = __DIR__.'/../resources/lang/'.app()->getLocale().'.json';
90
        if (! file_exists($file)) {
91
            $file = __DIR__.'/../resources/lang/en.json';
92
        }
93
94
        $translations = json_decode(file_get_contents($file), true);
95
        $translations = collect($translations)->mapWithKeys(function ($value, $key) {
96
            return [static::PACKAGE_ID.'::'.$key => $value];
97
        })->toArray();
98
99
        \Laravel\Nova\Nova::translations($translations);
100
    }
101
}
102