Passed
Push — master ( 7b35eb...39d150 )
by Philippe
01:23
created

SquantoServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
dl 0
loc 106
ccs 42
cts 46
cp 0.913
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 10 1
A getSquantoCachePath() 0 5 2
A getSquantoLangPath() 0 5 2
A registerTranslator() 0 19 1
A boot() 0 19 3
B register() 0 25 1
1
<?php
2
3
namespace Thinktomorrow\Squanto;
4
5
use League\Flysystem\Filesystem;
6
use Thinktomorrow\Squanto\Services\CachedTranslationFile;
7
use Thinktomorrow\Squanto\Import\ImportTranslationsCommand;
8
use Illuminate\Translation\TranslationServiceProvider as BaseServiceProvider;
9
use League\Flysystem\Adapter\Local;
10
use Thinktomorrow\Squanto\Services\CacheTranslationsCommand;
11
use Thinktomorrow\Squanto\Services\LaravelTranslationsReader;
12
use Thinktomorrow\Squanto\Translators\SquantoTranslator;
13
14
class SquantoServiceProvider extends BaseServiceProvider
15
{
16
    protected $defer = true;
17
18
    /**
19
     * @return array
20
     */
21
    public function provides()
22
    {
23
        return [
24
            'translator',
25
            'translation.loader',
26
            'Thinktomorrow\\Squanto\\Handlers\\ClearCacheTranslations',
27
            'Thinktomorrow\\Squanto\\Handlers\\WriteTranslationLineToDisk',
28
            'Thinktomorrow\\Squanto\\Services\\LaravelTranslationsReader',
29
        ];
30
    }
31
32
    /**
33
     * Bootstrap any application services.
34
     *
35
     * @return void
36
     */
37 53
    public function boot()
38
    {
39 53
        if ($this->app->runningInConsole()) {
40 53
            $this->publishes([
41 53
                __DIR__.'/../config/squanto.php' => config_path('squanto.php')
42 53
            ], 'config');
43
44 53
            if (!class_exists('CreateSquantoTables')) {
45 1
                $this->publishes([
46 1
                    __DIR__.'/../database/migrations/create_squanto_tables.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_squanto_tables.php'),
47 1
                ], 'migrations');
48
            }
49
50 53
            $this->commands([
51 53
                ImportTranslationsCommand::class,
52
                CacheTranslationsCommand::class,
53
            ]);
54
        }
55 53
    }
56
57
    /**
58
     * Register our translator
59
     *
60
     * @return void
61
     */
62 53
    public function register()
63
    {
64 53
        $this->app['squanto.cache_path'] = $this->getSquantoCachePath();
65 53
        $this->app['squanto.lang_path'] = $this->getSquantoLangPath();
66
67 53
        $this->registerTranslator();
68
69 53
        $this->app->bind(CachedTranslationFile::class, function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app 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...
70
            return new CachedTranslationFile(
71
                new Filesystem(new Local($this->getSquantoCachePath()))
72
            );
73 53
        });
74
75 53
        $this->app->bind(LaravelTranslationsReader::class, function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app 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...
76 5
            return new LaravelTranslationsReader(
77 5
                new Filesystem(new Local($this->getSquantoLangPath()))
78
            );
79 53
        });
80
81 53
        $this->mergeConfigFrom(__DIR__.'/../config/squanto.php', 'squanto');
82
83
        // Load translations in the register method because since 5.4 the boot method for the translation service provider
84
        // doesn't seem to be triggered by the calls to the translator anymore.
85 53
        $this->loadTranslationsFrom($this->getSquantoCachePath(), 'squanto');
86 53
    }
87
88 53
    private function registerTranslator()
89
    {
90 53
        $this->registerLoader();
91
92 53
        $this->app->singleton('translator', function ($app) {
93
94 53
            $loader = $app['translation.loader'];
95 53
            $locale = $app['config']['app.locale'];
96
97 53
            $trans = new SquantoTranslator($loader, $locale);
98
99 53
            $trans->setFallback($app['config']['app.fallback_locale']);
100
101
            // Custom Squanto option to display key or null when translation is not found
102 53
            $trans->setKeyAsDefault($app['config']['squanto.key_as_default']);
103
104 53
            return $trans;
105 53
        });
106 53
    }
107
108 53
    private function getSquantoCachePath()
109
    {
110 53
        $path = config('squanto.cache_path');
111 53
        return is_null($path) ? storage_path('app/trans') : $path;
112
    }
113
114 53
    private function getSquantoLangPath()
115
    {
116 53
        $path = config('squanto.lang_path');
117 53
        return is_null($path) ? app('path.lang') : $path;
118
    }
119
}
120