Passed
Push — master ( c1150a...693728 )
by Richard
02:48
created

LocalisationServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 62
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 29
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 62
rs 9.456

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PWWEB\Localisation.
5
 *
6
 * Localisation Service Provider.
7
 *
8
 * @author    Frank Pillukeit <[email protected]>
9
 * @copyright 2020 pw-websolutions.com
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
11
 */
12
13
namespace PWWEB\Localisation;
14
15
use Illuminate\Filesystem\Filesystem;
16
use Illuminate\Foundation\AliasLoader;
17
use Illuminate\Support\Collection;
18
use Illuminate\Support\ServiceProvider;
19
use PWWEB\Localisation\Contracts\Country as CountryContract;
20
use PWWEB\Localisation\Contracts\Currency as CurrencyContract;
21
use PWWEB\Localisation\Contracts\Language as LanguageContract;
22
23
class LocalisationServiceProvider extends ServiceProvider
24
{
25
    /**
26
     * Register services.
27
     *
28
     * @return void
29
     */
30
    public function register()
31
    {
32
        $this->mergeConfigFrom(
33
            __DIR__.'/../config/localisation.php',
34
            'pwweb.localisation'
35
        );
36
37
        // Register controllers.
38
        $this->app->make('PWWEB\Localisation\Controllers\IndexController');
39
40
        // Register views.
41
        $this->loadViewsFrom(__DIR__.'/resources/views', 'localisation');
42
    }
43
44
    /**
45
     * Boostrap the services of the package.
46
     *
47
     * @param LocalisationRegistrar $localisationLoader the localisation registrar
48
     * @param Filesystem            $filesystem         laravel filesystem object for file handling
49
     *
50
     * @return void
51
     */
52
    public function boot(LocalisationRegistrar $localisationLoader, Filesystem $filesystem)
53
    {
54
        include __DIR__.'/../routes/web.php';
55
56
        if (true === function_exists('config_path')) {
57
            $timestamp = date('Y_m_d_His', mktime(0, 0, 0, 1, 1, 2000));
58
            // function not available and 'publish' not relevant in Lumen
59
            $this->publishes(
60
                [
61
                    __DIR__.'/../config/localisation.php' => config_path('pwweb/localisation.php'),
62
                ],
63
                'pwweb.localisation.config'
64
            );
65
66
            $this->publishes(
67
                [
68
                    __DIR__.'/Database/Migrations/create_localisation_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_localisation_tables.php",
69
                ],
70
                'pwweb.localisation.migrations'
71
            );
72
73
            $this->publishes(
74
                [
75
                    __DIR__.'/resources/lang' => resource_path('lang/vendor/pwweb'),
76
                ],
77
                'pwweb.localisation.language'
78
            );
79
80
            $this->publishes(
81
                [
82
                    __DIR__.'/resources/views' => base_path('resources/views/vendor/localisation'),
83
                ],
84
                'pwweb.localisation.views'
85
            );
86
        }//end if
87
88
        $this->commands(
89
            [
90
                Commands\CacheReset::class,
91
            ]
92
        );
93
94
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'pwweb');
95
96
        $this->registerModelBindings();
97
98
        $localisationLoader->clearClassLanguages();
99
        $localisationLoader->registerLanguages();
100
101
        $this->app->bind('localisation', \PWWEB\Localisation\Localisation::class);
102
        $loader = AliasLoader::getInstance();
103
        $loader->alias('Localisation', \PWWEB\Localisation\Facades\Localisation::class);
104
105
        $this->app->singleton(
106
            LocalisationRegistrar::class,
107
            function ($app) use ($localisationLoader) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

107
            function (/** @scrutinizer ignore-unused */ $app) use ($localisationLoader) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
                return $localisationLoader;
109
            }
110
        );
111
112
        // Register the local middleware with the application.
113
        $this->app->make(\Illuminate\Contracts\Http\Kernel::class)->pushMiddleware(\PWWEB\Localisation\Middleware\Locale::class);
114
    }
115
116
    /**
117
     * Registers model bindings.
118
     *
119
     * @return void
120
     */
121
    protected function registerModelBindings()
122
    {
123
        $config = config('pwweb.localisation.models');
124
125
        $this->app->bind(CountryContract::class, $config['country']);
126
        $this->app->bind(LanguageContract::class, $config['language']);
127
        $this->app->bind(CurrencyContract::class, $config['currency']);
128
    }
129
130
    /**
131
     * Returns existing migration file if found, else uses the current timestamp.
132
     *
133
     * @param Filesystem $filesystem filesystem object for file handling
134
     *
135
     * @return string migration filename
136
     */
137
    protected function getMigrationFileName(Filesystem $filesystem): string
138
    {
139
        $timestamp = date('Y_m_d_His', mktime(0, 0, 0, 1, 1, 2000));
140
141
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
142
            ->flatMap(
143
                function ($path) use ($filesystem) {
144
                    return $filesystem->glob($path.'*_create_localisation_tables.php');
145
                }
146
            )->push($this->app->databasePath()."/migrations/{$timestamp}_create_localisation_tables.php")
147
            ->first();
148
    }
149
}
150