RobotsServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 9 1
A getMigrationFileName() 0 9 1
A register() 0 11 1
1
<?php
2
3
namespace Mguinea\Robots;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\ServiceProvider;
8
9
class RobotsServiceProvider 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
    public function boot(Filesystem $filesystem)
24
    {
25
        $this->publishes([
26
            __DIR__.'/../config/laravel-robots.php' => config_path('laravel-robots.php'),
27
        ], 'config');
28
29
        $this->publishes([
30
            __DIR__.'/../database/migrations/create_robots_tables.php.stub' => $this->getMigrationFileName($filesystem),
31
        ], 'migrations');
32
    }
33
34
    /**
35
     * Register the service provider.
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        $this->app->singleton('robots', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
        $this->app->singleton('robots', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
            return new Robots();
43
        });
44
45
        $this->app->alias('robots', 'Mguinea\Robots');
46
47
        $this->mergeConfigFrom(
48
            __DIR__.'/../config/laravel-robots.php',
49
            'laravel-robots'
50
        );
51
    }
52
53
    /**
54
     * Returns existing migration file if found, else uses the current timestamp.
55
     *
56
     * @param Filesystem $filesystem
57
     * @return string
58
     */
59
    protected function getMigrationFileName(Filesystem $filesystem): string
60
    {
61
        $timestamp = date('Y_m_d_His');
62
63
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
64
            ->flatMap(function ($path) use ($filesystem) {
65
                return $filesystem->glob($path.'*_create_robots_tables.php');
66
            })->push($this->app->databasePath()."/migrations/{$timestamp}_create_robots_tables.php")
67
            ->first();
68
    }
69
}
70