Passed
Push — master ( 26152a...8fe73e )
by F
04:04
created

LocalisationServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMigrationFileName() 0 11 1
A boot() 0 55 2
A register() 0 12 1
A registerModelBindings() 0 7 1
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
            // function not available and 'publish' not relevant in Lumen
58
            $this->publishes(
59
                [
60
                __DIR__ . '/../config/localisation.php' => config_path('pwweb/localisation.php'),
61
                ],
62
                'pwweb.localisation.config'
63
            );
64
65
            $this->publishes(
66
                [
67
                __DIR__ . '/../database/migrations/create_localisation_tables.php.stub' => $this->getMigrationFileName($filesystem),
68
                ],
69
                'pwweb.localisation.migrations'
70
            );
71
72
            $this->publishes(
73
                [
74
                __DIR__ . '/resources/lang' => resource_path('lang/vendor/pwweb'),
75
                ]
76
            );
77
78
            $this->publishes(
79
                [
80
                    __DIR__ . '/resources/views' => base_path('resources/views/vendor/localisation'),
81
                ],
82
                'views'
83
            );
84
        }//end if
85
86
        $this->commands(
87
            [
88
            Commands\CacheReset::class,
89
            ]
90
        );
91
92
        $this->loadTranslationsFrom(realpath(__DIR__ . '/resources/lang'), 'pwweb');
93
94
        $this->registerModelBindings();
95
96
        $localisationLoader->clearClassLanguages();
97
        $localisationLoader->registerLanguages();
98
99
        $this->app->bind('localisation', \PWWeb\Localisation\Localisation::class);
100
        $loader = AliasLoader::getInstance();
101
        $loader->alias('Localisation', \PWWeb\Localisation\Facades\Localisation::class);
102
103
        $this->app->singleton(
104
            LocalisationRegistrar::class,
105
            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

105
            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...
106
                return $localisationLoader;
107
            }
108
        );
109
    }
110
111
    /**
112
     * Registers model bindings.
113
     *
114
     * @return void
115
     */
116
    protected function registerModelBindings()
117
    {
118
        $config = $this->app->config['pwweb.localisation.models'];
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
119
120
        $this->app->bind(CountryContract::class, $config['country']);
121
        $this->app->bind(LanguageContract::class, $config['language']);
122
        $this->app->bind(CurrencyContract::class, $config['currency']);
123
    }
124
125
    /**
126
     * Returns existing migration file if found, else uses the current timestamp.
127
     *
128
     * @param Filesystem $filesystem filesystem object for file handling
129
     *
130
     * @return string migration filename
131
     */
132
    protected function getMigrationFileName(Filesystem $filesystem): string
133
    {
134
        $timestamp = date('Y_m_d_His', mktime(0, 0, 0, 1, 1, 2020));
135
136
        return Collection::make($this->app->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR)
137
            ->flatMap(
138
                function ($path) use ($filesystem) {
139
                    return $filesystem->glob($path . '*_create_localisation_tables.php');
140
                }
141
            )->push($this->app->databasePath() . "/migrations/{$timestamp}_create_localisation_tables.php")
142
            ->first();
143
    }
144
}
145