AuditingServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 20
c 5
b 2
f 0
dl 0
loc 53
ccs 25
cts 25
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 7 1
A register() 0 10 1
A registerPublishing() 0 14 3
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Support\Facades\Event;
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
use OwenIt\Auditing\Events\AuditCustom;
12
use OwenIt\Auditing\Events\DispatchAudit;
13
use OwenIt\Auditing\Listeners\ProcessDispatchAudit;
14
use OwenIt\Auditing\Listeners\RecordCustomAudit;
15
16
class AuditingServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Bootstrap the service provider.
20
     *
21
     * @return void
22
     */
23 17
    public function boot()
24
    {
25 17
        $this->registerPublishing();
26 17
        $this->mergeConfigFrom(__DIR__ . '/../config/audit.php', 'audit');
27
28 17
        Event::listen(AuditCustom::class, RecordCustomAudit::class);
29 17
        Event::listen(DispatchAudit::class, ProcessDispatchAudit::class);
30
    }
31
32
    /**
33
     * Register the service provider.
34
     *
35
     * @return void
36
     */
37 17
    public function register()
38
    {
39 17
        $this->commands([
40 17
            AuditDriverCommand::class,
41 17
            AuditResolverCommand::class,
42 17
            InstallCommand::class,
43 17
        ]);
44
45 17
        $this->app->singleton(Auditor::class, function ($app) {
46 15
            return new \OwenIt\Auditing\Auditor($app);
47 17
        });
48
    }
49
50
    /**
51
     * Register the package's publishable resources.
52
     *
53
     * @return void
54
     */
55 17
    private function registerPublishing()
56
    {
57 17
        if ($this->app->runningInConsole()) {
58
            // Lumen lacks a config_path() helper, so we use base_path()
59 17
            $this->publishes([
60 17
                __DIR__ . '/../config/audit.php' => base_path('config/audit.php'),
61 17
            ], 'config');
62
63 17
            if (!class_exists('CreateAuditsTable')) {
64 2
                $this->publishes([
65 2
                    __DIR__ . '/../database/migrations/audits.stub' => database_path(
66 2
                        sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))
67 2
                    ),
68 2
                ], 'migrations');
69
            }
70
        }
71
    }
72
}
73