Completed
Push — master ( d6560c...fcbc5c )
by
unknown
06:59
created

SquantoServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 97.1%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 106
ccs 67
cts 69
cp 0.971
rs 10
wmc 10
lcom 1
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerTranslator() 0 19 1
A getSquantoLangPath() 0 5 2
A provides() 0 10 1
A boot() 0 19 3
A getSquantoCachePath() 0 5 2
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 1
    public function provides()
22
    {
23
        return [
24 1
            '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 93
    public function boot()
38
    {
39 93
        if ($this->app->runningInConsole()) {
40 47
            $this->publishes([
41 93
                __DIR__.'/../config/squanto.php' => config_path('squanto.php')
42 93
            ], 'config');
43 46
44 93
            if (!class_exists('CreateSquantoTables')) {
45 1
                $this->publishes([
46 47
                    __DIR__.'/../database/migrations/create_squanto_tables.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_squanto_tables.php'),
47 47
                ], 'migrations');
48 46
            }
49
50 93
            $this->commands([
51 47
                ImportTranslationsCommand::class,
52
                CacheTranslationsCommand::class,
53
            ]);
54
        }
55 47
    }
56
57 46
    /**
58
     * Register our translator
59 46
     *
60 46
     * @return void
61
     */
62 93
    public function register()
63
    {
64 47
        $this->app['squanto.cache_path'] = $this->getSquantoCachePath();
65 49
        $this->app['squanto.lang_path'] = $this->getSquantoLangPath();
66 2
67 47
        $this->registerTranslator();
68 46
69
        $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 47
        });
74 46
75
        $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 23
                new Filesystem(new Local($this->getSquantoLangPath()))
78 18
            );
79 47
        });
80 46
81 47
        $this->mergeConfigFrom(__DIR__.'/../config/squanto.php', 'squanto');
82 46
83 46
        // 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 47
        $this->loadTranslationsFrom($this->getSquantoCachePath(), 'squanto');
86 93
    }
87 46
88 47
    private function registerTranslator()
89 46
    {
90 47
        $this->registerLoader();
91 46
92 47
        $this->app->singleton('translator', function ($app) {
93 46
94 47
            $loader = $app['translation.loader'];
95 93
            $locale = $app['config']['app.locale'];
96 46
97 47
            $trans = new SquantoTranslator($loader, $locale);
98 46
99 47
            $trans->setFallback($app['config']['app.fallback_locale']);
100 46
101
            // Custom Squanto option to display key or null when translation is not found
102 47
            $trans->setKeyAsDefault($app['config']['squanto.key_as_default']);
103 46
104 47
            return $trans;
105 93
        });
106 93
    }
107 46
108 47
    private function getSquantoCachePath()
109 46
    {
110 47
        $path = config('squanto.cache_path');
111 93
        return is_null($path) ? storage_path('app/trans') : $path;
112 46
    }
113
114 47
    private function getSquantoLangPath()
115 46
    {
116 47
        $path = config('squanto.lang_path');
117 93
        return is_null($path) ? app('path.lang') : $path;
118 46
    }
119
}
120