TranslatableServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LaTevaWeb\Translatable;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\ServiceProvider;
8
9
class TranslatableServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Indicates if loading of the provider is deferred.
13
     *
14
     * @var bool
15
     */
16
    protected $defer = false;
17
18
    /**
19
     * Bootstrap services.
20
     *
21
     * @return void
22
     */
23 9
    public function boot(Filesystem $filesystem)
24
    {
25 9
        $this->publishes([
26 9
            __DIR__.'/../config/latevaweb-translatable.php' => config_path('latevaweb-translatable.php'),
27 9
        ], 'config');
28
29 9
        $this->publishes([
30 9
            __DIR__.'/../database/migrations/create_translations_tables.php.stub' => $this->getMigrationFileName($filesystem),
31 9
        ], 'migrations');
32 9
    }
33
34
    /**
35
     * Register the service provider.
36
     *
37
     * @return void
38
     */
39 9
    public function register()
40
    {
41 9
        $this->mergeConfigFrom(
42 9
            __DIR__.'/../config/latevaweb-translatable.php',
43 9
            'latevaweb-translatable'
44
        );
45 9
    }
46
47
    /**
48
     * Returns existing migration file if found, else uses the current timestamp.
49
     *
50
     * @param Filesystem $filesystem
51
     * @return string
52
     */
53 9
    protected function getMigrationFileName(Filesystem $filesystem): string
54
    {
55 9
        $timestamp = date('Y_m_d_His');
56
57 9
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
58
            ->flatMap(function ($path) use ($filesystem) {
59 9
                return $filesystem->glob($path.'*_create_translations_tables.php');
60 9
            })
61 9
            ->push($this->app->databasePath()."/migrations/{$timestamp}_create_translations_tables.php")
62 9
            ->first();
63
    }
64
}
65