Completed
Push — master ( 52052f...b5e4b5 )
by Ben
04:12
created

SquantoServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 110
ccs 42
cts 44
cp 0.9545
rs 10
c 1
b 0
f 0

6 Methods

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