Test Failed
Pull Request — master (#644)
by
unknown
07:16 queued 02:20
created

AuditingServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 18
c 4
b 2
f 0
dl 0
loc 58
ccs 23
cts 23
cp 1
rs 10
wmc 5
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Contracts\Support\DeferrableProvider;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Collection;
9
10
use OwenIt\Auditing\Console\AuditDriverCommand;
11
use OwenIt\Auditing\Console\InstallCommand;
12
use OwenIt\Auditing\Contracts\Auditor;
13
14
class AuditingServiceProvider extends ServiceProvider implements DeferrableProvider
15
{
16
    /**
17
     * Bootstrap the service provider.
18
     *
19 192
     * @return void
20
     */
21 192
    public function boot(Filesystem $filesystem)
22 192
    {
23 192
        $this->registerPublishing($filesystem);
24
        $this->mergeConfigFrom(__DIR__.'/../config/audit.php', 'audit');
25
    }
26
27
    /**
28
     * Register the service provider.
29
     *
30 192
     * @return void
31
     */
32 192
    public function register()
33 192
    {
34
        $this->commands([
35
            AuditDriverCommand::class,
36
            InstallCommand::class,
37 192
        ]);
38 100
39 192
        $this->app->singleton(Auditor::class, function ($app) {
40 192
            return new \OwenIt\Auditing\Auditor($app);
41
        });
42
    }
43
44
    /**
45
     * Register the package's publishable resources.
46
     *
47 192
     * @return void
48
     */
49 192
    private function registerPublishing($filesystem)
50
    {
51 192
        if ($this->app->runningInConsole()) {
52 192
            // Lumen lacks a config_path() helper, so we use base_path()
53 192
            $this->publishes([
54
                __DIR__.'/../config/audit.php' => base_path('config/audit.php'),
55 192
            ], 'config');
56 192
57 192
            if (!class_exists('CreateAuditsTable') && !$this->migrationAlreadyPublished(
58
+                $filesystem,
59 192
+                '_create_audits_table.php'
60
+            )) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 60 at column 13
Loading history...
61 192
                $this->publishes([
62
                    __DIR__.'/../database/migrations/audits.stub' => database_path(
63
                        sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))
64
                    ),
65
                ], 'migrations');
66 2
            }
67
        }
68
    }
69 2
70
    /**
71
     * @param Filesystem  $filesystem
72
     * @param $filename
73
     * @return bool
74
     */
75
    protected function migrationAlreadyPublished(Filesystem $filesystem, $filename): bool
76
    {
77
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
78
                ->flatMap(function ($path) use ($filesystem, $filename) {
79
                    return $filesystem->glob($path.'*'.$filename);
80
                })
81
                ->count() > 0;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function provides()
88
    {
89
        return [
90
            Auditor::class,
91
        ];
92
    }
93
}
94