Completed
Push — master ( f3d591...31e394 )
by Ben
02:18
created

SquantoServiceProvider::getSquantoLangPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 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\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 72
    public function boot()
38
    {
39 72
        $this->loadTranslationsFrom($this->getSquantoCachePath(), 'squanto');
40
//        $this->app['translator']->addNamespace('squanto', $this->getSquantoCachePath());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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