Completed
Push — master ( 446e6e...f3d591 )
by Ben
08:37
created

SquantoServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 104
ccs 40
cts 42
cp 0.9524
rs 10

6 Methods

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