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

LocaleServiceProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1.008

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 48
ccs 20
cts 25
cp 0.8
crap 1.008
rs 9.1344
c 0
b 0
f 0
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