Completed
Push — master ( eb9412...8cc090 )
by Propa
11:15
created

IntlServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 32.52 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 40
loc 123
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 1
A boot() 0 8 1
A registerCountry() 10 10 1
A registerCurrency() 10 10 1
A registerLanguage() 10 10 1
A registerNumber() 10 10 1
A registerDate() 0 6 1
A setLocale() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Propaganistas\LaravelIntl;
2
3
use Illuminate\Foundation\Events\LocaleUpdated;
4
use Illuminate\Support\ServiceProvider;
5
use Jenssegers\Date\Date;
6
use Jenssegers\Date\DateServiceProvider;
7
use Punic\Data as Punic;
8
9
class IntlServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Register the service provider.
13
     *
14
     * @return void
15
     */
16 147
    public function register()
17
    {
18 147
        $this->registerCountry();
19 147
        $this->registerCurrency();
20 147
        $this->registerLanguage();
21 147
        $this->registerNumber();
22 147
        $this->registerDate();
23 147
    }
24
25
    /**
26
     * Bootstrap the application services.
27
     *
28
     * @return void
29
     */
30 147
    public function boot()
31
    {
32
        $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...
33 69
            $this->setLocale();
34 147
        });
35
36 147
        $this->setLocale();
37 147
    }
38
39
    /**
40
     * Register the country repository.
41
     *
42
     * @return void
43
     */
44 147 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...
45
    {
46
        $this->app->singleton(Country::class, function ($app) {
47 21
            $repository = new Country;
48
49 21
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
50 147
        });
51
52 147
        $this->app->alias(Country::class, 'intl.country');
53 147
    }
54
55
    /**
56
     * Register the currency repository.
57
     *
58
     * @return void
59
     */
60 147 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...
61
    {
62
        $this->app->singleton(Currency::class, function ($app) {
63 36
            $repository = new Currency;
64
65 36
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
66 147
        });
67
68 147
        $this->app->alias(Currency::class, 'intl.currency');
69 147
    }
70
71
    /**
72
     * Register the language repository.
73
     *
74
     * @return void
75
     */
76 147 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...
77
    {
78
        $this->app->singleton(Language::class, function ($app) {
79 24
            $repository = new Language;
80
81 24
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
82 147
        });
83
84 147
        $this->app->alias(Language::class, 'intl.language');
85 147
    }
86
87
    /**
88
     * Register the number repository.
89
     *
90
     * @return void
91
     */
92 147 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...
93
    {
94
        $this->app->singleton(Number::class, function ($app) {
95 24
            $repository = new Number;
96
97 24
            return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']);
98 147
        });
99
100 147
        $this->app->alias(Number::class, 'intl.number');
101 147
    }
102
103
    /**
104
     * Register the date handler.
105
     *
106
     * @return void
107
     */
108 147
    protected function registerDate()
109
    {
110 147
        $this->app->register(DateServiceProvider::class);
111
112 147
        require __DIR__.'/Macros/Carbon.php';
113 147
    }
114
115
    /**
116
     * Set locales on sub-libraries.
117
     *
118
     * @throws \Punic\Exception\InvalidLocale
119
     */
120 147
    protected function setLocale()
121
    {
122 147
        $locale = $this->app['config']['app.locale'];
123 147
        $fallbackLocale = $this->app['config']['app.fallback_locale'];
124
125 147
        Punic::setDefaultLocale($locale);
126 147
        Punic::setFallbackLocale($fallbackLocale);
127
128 147
        Date::setLocale($locale);
129 147
        Date::setFallbackLocale($fallbackLocale);
130 147
    }
131
}
132