IntlServiceProvider::setLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Propaganistas\LaravelIntl;
2
3
use Illuminate\Foundation\Events\LocaleUpdated;
4
use Illuminate\Support\ServiceProvider;
5
use Punic\Data as Punic;
6
7
class IntlServiceProvider extends ServiceProvider
8
{
9
    /**
10
     * Register the service provider.
11
     *
12
     * @return void
13
     */
14 144
    public function register()
15
    {
16 144
        $this->registerCountry();
17 144
        $this->registerCurrency();
18 144
        $this->registerLanguage();
19 144
        $this->registerNumber();
20 144
        $this->registerDate();
21 144
    }
22
23
    /**
24
     * Bootstrap the application services.
25
     *
26
     * @return void
27
     */
28 144
    public function boot()
29
    {
30
        $this->app['events']->listen(LocaleUpdated::class, function ($locale) {
0 ignored issues
show
Unused Code introduced by
The parameter $locale is not used and could be removed.

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

Loading history...
31 66
            $this->setLocale();
32 144
        });
33
34 144
        $this->setLocale();
35 144
    }
36
37
    /**
38
     * Register the country repository.
39
     *
40
     * @return void
41
     */
42 144 View Code Duplication
    protected function registerCountry()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $this->app->singleton(Country::class, function ($app) {
45 21
            $repository = new Country;
46
47 21
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
48 144
        });
49
50 144
        $this->app->alias(Country::class, 'intl.country');
51 144
    }
52
53
    /**
54
     * Register the currency repository.
55
     *
56
     * @return void
57
     */
58 144 View Code Duplication
    protected function registerCurrency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $this->app->singleton(Currency::class, function ($app) {
61 36
            $repository = new Currency;
62
63 36
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
64 144
        });
65
66 144
        $this->app->alias(Currency::class, 'intl.currency');
67 144
    }
68
69
    /**
70
     * Register the language repository.
71
     *
72
     * @return void
73
     */
74 144 View Code Duplication
    protected function registerLanguage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $this->app->singleton(Language::class, function ($app) {
77 24
            $repository = new Language;
78
79 24
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
80 144
        });
81
82 144
        $this->app->alias(Language::class, 'intl.language');
83 144
    }
84
85
    /**
86
     * Register the number repository.
87
     *
88
     * @return void
89
     */
90 144 View Code Duplication
    protected function registerNumber()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $this->app->singleton(Number::class, function ($app) {
93 24
            $repository = new Number;
94
95 24
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
96 144
        });
97
98 144
        $this->app->alias(Number::class, 'intl.number');
99 144
    }
100
101
    /**
102
     * Register the date handler.
103
     *
104
     * @return void
105
     */
106 144
    protected function registerDate()
107
    {
108 144
        require __DIR__.'/Macros/Carbon.php';
109 144
    }
110
111
    /**
112
     * Set locales on sub-libraries.
113
     *
114
     * @throws \Punic\Exception\InvalidLocale
115
     */
116 144
    protected function setLocale()
117
    {
118 144
        $locale = $this->app['config']['app.locale'];
119 144
        $fallbackLocale = $this->app['config']['app.fallback_locale'];
120
121 144
        Punic::setDefaultLocale($locale);
122 144
        Punic::setFallbackLocale($fallbackLocale);
123 144
    }
124
}
125