Completed
Pull Request — master (#467)
by
unknown
09:05 queued 02:54
created

AuditingServiceProvider::deprecatedFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Support\ServiceProvider;
6
use OwenIt\Auditing\Console\AuditDriverMakeCommand;
7
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...
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use RecursiveRegexIterator;
11
12
class AuditingServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $defer = true;
18
19
    /**
20
     * Bootstrap the service provider.
21
     *
22
     * @return void
23
     */
24 282
    public function boot()
25
    {
26 282
        $config = __DIR__.'/../config/audit.php';
27 282
        $migration = __DIR__.'/../database/migrations/audits.stub';
28
29
        // Lumen lacks a config_path() helper, so we use base_path()
30 282
        if ($this->app->runningInConsole()) {
31 282
            $this->publishes([
32 282
                $config => base_path('config/audit.php'),
33 282
            ], 'config');
34
35 282
            $this->publishes([
36 282
                $migration => database_path('migrations/'.$this->deprecatedFileName()),
37 282
            ], 'migrations');
38
        }
39
40 282
        $this->mergeConfigFrom($config, 'audit');
41 282
    }
42
43
    /**
44
     * Check to see if package has been used previously.
45
     * If it has, use that file name, if not, use a file name that timestamps the day of this pull request.
46
     *
47
     * @return string
48
     */
49 282
    protected function deprecatedFileName()
50
    {
51 282
        $iterator = iterator_to_array(new RecursiveIteratorIterator(
52 282
            new RecursiveRegexIterator(
53 282
                new RecursiveDirectoryIterator(database_path('migrations'), RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS),
54 282
                '/(\d{4})_(\d{2})_(\d{2})_(\d{6})_create_audits_table\.php/i', RecursiveRegexIterator::MATCH
55
            ),
56 282
            RecursiveIteratorIterator::SELF_FIRST
57
        ));
58
59 282
        return count($iterator) ? reset($iterator)->getFilename() : '2018_11_22_000000_create_audits_table.php';
60
    }
61
62
    /**
63
     * Register the service provider.
64
     *
65
     * @return void
66
     */
67 282
    public function register()
68
    {
69 282
        $this->commands([
70 282
            AuditDriverMakeCommand::class,
71
        ]);
72
73 282
        $this->app->singleton(Auditor::class, function ($app) {
74 147
            return new \OwenIt\Auditing\Auditor($app);
75 282
        });
76 282
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function provides()
82
    {
83
        return [
84 3
            Auditor::class,
85
        ];
86
    }
87
}
88