Passed
Push — master ( 8094df...219b00 )
by Ben
08:14
created

LocaleServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 23
cts 29
cp 0.7931
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigValues() 0 8 2
A register() 0 48 1
1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Illuminate\Support\ServiceProvider;
6
use Thinktomorrow\Locale\Parsers\RouteParser;
7
use Thinktomorrow\Locale\Parsers\UrlParser;
8
use Thinktomorrow\Locale\Values\Config;
9
10
class LocaleServiceProvider extends ServiceProvider
11
{
12 81
    public function register()
13
    {
14 81
        require_once __DIR__.'/helpers.php';
15
16 81
        $this->publishes([
17 81
            __DIR__.'/config/locale.php' => config_path('thinktomorrow/locale.php'),
18
        ]);
19
20 81
        $this->mergeConfigFrom(
21 81
            __DIR__.'/config/locale.php',
22 81
            'thinktomorrow.locale'
23
        );
24
25 81
        $config = Config::from($this->getConfigValues());
26
27
        $this->app->singleton(Detect::class, function ($app) use ($config) {
28
            return new Detect($app['request'], $config);
29 81
        });
30
31
        $this->app->singleton(UrlParser::class, function ($app) {
32 74
            return new UrlParser(
33 74
                $app['Illuminate\Contracts\Routing\UrlGenerator']
34
            );
35 81
        });
36
37
        $this->app->singleton(RouteParser::class, function ($app) {
38 74
            return new RouteParser(
39 74
                $app['Thinktomorrow\Locale\Parsers\UrlParser'],
40 74
                $app['translator']
41
            );
42 81
        });
43
44
        $this->app->singleton(LocaleUrl::class, function ($app) use ($config) {
45
            return new LocaleUrl(
46
                $app['Thinktomorrow\Locale\Detect'],
47
                $app['Thinktomorrow\Locale\Parsers\UrlParser'],
48
                $app['Thinktomorrow\Locale\Parsers\RouteParser'],
49
                $config
50
            );
51 81
        });
52
53
        /*
54
         * Facade for getting current active scope
55
         */
56
        $this->app->singleton('tt-locale-scope', function ($app) {
57 10
            return $app->make(Detect::class)->detectLocale()->getScope();
58 81
        });
59 81
    }
60
61 81
    private function getConfigValues()
62
    {
63 81
        if (file_exists(config_path('thinktomorrow/locale.php'))) {
64
            return require config_path('thinktomorrow/locale.php');
65
        }
66
67 81
        return require __DIR__.'/config/locale.php';
68
    }
69
}
70