Passed
Pull Request — master (#644)
by
unknown
06:06
created

AuditingServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 24
dl 0
loc 75
ccs 31
cts 31
cp 1
rs 10
c 5
b 2
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A migrationAlreadyPublished() 0 7 1
A provides() 0 4 1
A boot() 0 4 1
A register() 0 9 1
A registerPublishing() 0 15 4
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Contracts\Support\DeferrableProvider;
6
use Illuminate\Support\ServiceProvider;
7
8
use OwenIt\Auditing\Contracts\Auditor;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OwenIt\Auditing\Auditor. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use OwenIt\Auditing\Console\AuditDriverCommand;
10
use OwenIt\Auditing\Console\InstallCommand;
11
use Illuminate\Filesystem\Filesystem;
12
use Illuminate\Support\Collection;
13
14
class AuditingServiceProvider extends ServiceProvider implements DeferrableProvider
15
{
16
    /**
17
     * Bootstrap the service provider.
18
     *
19
     * @return void
20
     */
21 192
    public function boot(Filesystem $filesystem)
22
    {
23 192
        $this->registerPublishing($filesystem);
24 192
        $this->mergeConfigFrom(__DIR__.'/../config/audit.php', 'audit');
25 192
    }
26
27
    /**
28
     * Register the service provider.
29
     *
30
     * @return void
31
     */
32 192
    public function register()
33
    {
34 192
        $this->commands([
35 192
            AuditDriverCommand::class,
36
            InstallCommand::class,
37
        ]);
38
39 192
        $this->app->singleton(Auditor::class, function ($app) {
40 100
            return new \OwenIt\Auditing\Auditor($app);
41 192
        });
42 192
    }
43
44
    /**
45
     * Register the package's publishable resources.
46
     *
47
     * @return void
48
     */
49 192
    private function registerPublishing($filesystem)
50
    {
51 192
        if ($this->app->runningInConsole()) {
52
            // Lumen lacks a config_path() helper, so we use base_path()
53 192
            $this->publishes([
54 192
                __DIR__.'/../config/audit.php' => base_path('config/audit.php'),
55 192
            ], 'config');
56
57 192
            if (!class_exists('CreateAuditsTable') && !$this->migrationAlreadyPublished($filesystem,
58 192
                '_create_audits_table.php')) {
59 192
                $this->publishes([
60 192
                    __DIR__.'/../database/migrations/audits.stub' => database_path(
61 192
                        sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))
62
                    ),
63 192
                ], 'migrations');
64
            }
65
        }
66 192
    }
67
68
    /**
69
     * @param  Filesystem  $filesystem
70
     * @param $filename
71
     * @return bool
72
     */
73 192
    protected function migrationAlreadyPublished(Filesystem $filesystem, $filename): bool
74
    {
75 192
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
76 192
                ->flatMap(function ($path) use ($filesystem, $filename) {
77 192
                    return $filesystem->glob($path.'*'.$filename);
78 192
                })
79 192
                ->count() > 0;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function provides()
86
    {
87
        return [
88 2
            Auditor::class,
89
        ];
90
    }
91
}
92