Test Failed
Pull Request — master (#684)
by Morten
16:23
created

AuditingServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 2
c 2
b 2
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Contracts\Support\DeferrableProvider;
6
use Illuminate\Support\ServiceProvider;
7
use OwenIt\Auditing\Console\AuditDriverCommand;
8
use OwenIt\Auditing\Console\AuditResolverCommand;
9
use OwenIt\Auditing\Console\InstallCommand;
10
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...
11
12
class AuditingServiceProvider extends ServiceProvider implements DeferrableProvider
13
{
14
    /**
15
     * Bootstrap the service provider.
16
     *
17
     * @return void
18
     */
19 152
    public function boot()
20
    {
21 152
        $this->registerPublishing();
22 152
        $this->mergeConfigFrom(__DIR__ . '/../config/audit.php', 'audit');
23
    }
24
25
    /**
26
     * Register the service provider.
27
     *
28
     * @return void
29
     */
30 152
    public function register()
31
    {
32 152
        $this->commands([
33
            AuditDriverCommand::class,
34
            AuditResolverCommand::class,
35
            InstallCommand::class,
36
        ]);
37
38 152
        $this->app->singleton(Auditor::class, function ($app) {
39 94
            return new \OwenIt\Auditing\Auditor($app);
40
        });
41
42 152
        $this->app->register(AuditingEventServiceProvider::class);
43
    }
44
45
    /**
46
     * Register the package's publishable resources.
47
     *
48
     * @return void
49
     */
50 152
    private function registerPublishing()
51
    {
52 152
        if ($this->app->runningInConsole()) {
53
            // Lumen lacks a config_path() helper, so we use base_path()
54 152
            $this->publishes([
55 152
                __DIR__ . '/../config/audit.php' => base_path('config/audit.php'),
56 152
            ], 'config');
57
58 152
            $this->publishes([
59 152
                __DIR__ . '/../database/migrations/audits.stub' => database_path(
60 152
                    sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))
61
                ),
62 152
            ], 'migrations');
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function provides()
70
    {
71
        return [
72
            Auditor::class,
73
        ];
74
    }
75
}
76