LocalisationServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 52
c 3
b 0
f 0
dl 0
loc 135
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMigrationFileName() 0 11 1
A boot() 0 66 2
A register() 0 12 1
A registerModelBindings() 0 13 2
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\Routing\Router;
18
use Illuminate\Support\Collection;
19
use Illuminate\Support\ServiceProvider;
20
use PWWEB\Localisation\Contracts\Country as CountryContract;
21
use PWWEB\Localisation\Contracts\Currency as CurrencyContract;
22
use PWWEB\Localisation\Contracts\Language as LanguageContract;
23
24
class LocalisationServiceProvider extends ServiceProvider
25
{
26
    /**
27
     * Register services.
28
     *
29
     * @return void
30
     */
31
    public function register()
32
    {
33
        $this->mergeConfigFrom(
34
            __DIR__.'/../config/localisation.php',
35
            'pwweb.localisation'
36
        );
37
38
        // Register controllers.
39
        $this->app->make('PWWEB\Localisation\Controllers\IndexController');
40
41
        // Register views.
42
        $this->loadViewsFrom(__DIR__.'/resources/views', 'localisation');
43
    }
44
45
    /**
46
     * Boostrap the services of the package.
47
     *
48
     * @param LocalisationRegistrar $localisationLoader the localisation registrar
49
     * @param Filesystem            $filesystem         laravel filesystem object for file handling
50
     *
51
     * @return void
52
     */
53
    public function boot(LocalisationRegistrar $localisationLoader, Filesystem $filesystem, Router $router)
54
    {
55
        $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
56
57
        if (true === function_exists('config_path')) {
58
            $timestamp = date('Y_m_d_His', mktime(0, 0, 0, 1, 1, 2000));
59
            // function not available and 'publish' not relevant in Lumen
60
            $this->publishes(
61
                [
62
                    __DIR__.'/../config/localisation.php' => config_path('pwweb/localisation.php'),
63
                ],
64
                'pwweb.localisation.config'
65
            );
66
67
            $this->publishes(
68
                [
69
                    __DIR__.'/Database/Migrations/create_localisation_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_localisation_tables.php",
70
                ],
71
                'pwweb.localisation.migrations'
72
            );
73
74
            $this->publishes(
75
                [
76
                    __DIR__.'/resources/lang' => resource_path('lang/vendor/pwweb'),
77
                ],
78
                'pwweb.localisation.language'
79
            );
80
81
            $this->publishes(
82
                [
83
                    __DIR__.'/resources/views' => base_path('resources/views/vendor/localisation'),
84
                ],
85
                'pwweb.localisation.views'
86
            );
87
        }//end if
88
89
        $this->commands(
90
            [
91
                Commands\CacheReset::class,
92
            ]
93
        );
94
95
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'pwweb');
96
97
        $this->registerModelBindings();
98
99
        $localisationLoader->clearClassLanguages();
100
        $localisationLoader->registerLanguages();
101
102
        $this->app->bind('localisation', \PWWEB\Localisation\Localisation::class);
103
        $loader = AliasLoader::getInstance();
104
        $loader->alias('Localisation', \PWWEB\Localisation\Facades\Localisation::class);
105
106
        $this->app->singleton(
107
            LocalisationRegistrar::class,
108
            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

108
            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...
109
                return $localisationLoader;
110
            }
111
        );
112
113
        // Bind an instance of the language repository to the container.
114
        $languageRepo = new \PWWEB\Localisation\Repositories\LanguageRepository($this->app);
115
        $this->app->instance(\PWWEB\Localisation\Repositories\LanguageRepository::class, $languageRepo);
116
117
        // Register the local middleware with the application.
118
        $router->middlewareGroup('localisation', [\PWWEB\Localisation\Middleware\Locale::class]);
119
    }
120
121
    /**
122
     * Registers model bindings.
123
     *
124
     * @return void
125
     */
126
    protected function registerModelBindings()
127
    {
128
        $config = config('pwweb.localisation.models');
129
130
        if (false === $config) {
131
            return;
132
        }
133
134
        $this->app->bind(AddressContract::class, $config['address']);
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\AddressContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
135
        $this->app->bind(AddressTypeContract::class, $config['address_type']);
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\AddressTypeContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
136
        $this->app->bind(CountryContract::class, $config['country']);
137
        $this->app->bind(CurrencyContract::class, $config['currency']);
138
        $this->app->bind(LanguageContract::class, $config['language']);
139
    }
140
141
    /**
142
     * Returns existing migration file if found, else uses the current timestamp.
143
     *
144
     * @param Filesystem $filesystem filesystem object for file handling
145
     *
146
     * @return string migration filename
147
     */
148
    protected function getMigrationFileName(Filesystem $filesystem): string
149
    {
150
        $timestamp = date('Y_m_d_His', mktime(0, 0, 0, 1, 1, 2000));
151
152
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
153
            ->flatMap(
154
                function ($path) use ($filesystem) {
155
                    return $filesystem->glob($path.'*_create_localisation_tables.php');
156
                }
157
            )->push($this->app->databasePath()."/migrations/{$timestamp}_create_localisation_tables.php")
158
            ->first();
159
    }
160
}
161