Passed
Pull Request — master (#644)
by
unknown
10:38 queued 04:04
created

migrationAlreadyPublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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