Completed
Push — master ( 91aba6...5f2cf6 )
by Tom
07:07
created

ActivitylogServiceProvider::bootConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Str;
8
use Spatie\Activitylog\Contracts\Activity;
9
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
10
use Spatie\Activitylog\Exceptions\InvalidConfiguration;
11
use Spatie\Activitylog\Models\Activity as ActivityModel;
12
13
class ActivitylogServiceProvider extends ServiceProvider
14 384
{
15
    public function boot()
16 384
    {
17 384
        $this->bootConfig();
18 384
        $this->bootMigrations();
19
    }
20 384
21
    public function register()
22 384
    {
23 4
        $this->app->bind('command.activitylog:clean', CleanActivitylogCommand::class);
24
25 4
        $this->commands([
26 4
            'command.activitylog:clean',
27 4
        ]);
28
29 384
        $this->app->bind(ActivityLogger::class);
30
31 384
        $this->app->singleton(ActivityLogStatus::class);
32
    }
33 384
34
    public static function determineActivityModel(): string
35 384
    {
36 384
        $activityModel = config('activitylog.activity_model') ?? ActivityModel::class;
37
38
        if (! is_a($activityModel, Activity::class, true)
39 384
            || ! is_a($activityModel, Model::class, true)) {
40
            throw InvalidConfiguration::modelIsNotValid($activityModel);
41 384
        }
42 384
43
        return $activityModel;
44 347
    }
45
46 347
    public static function getActivityModelInstance(): ActivityContract
47
    {
48 347
        $activityModelClassName = self::determineActivityModel();
49 347
50 8
        return new $activityModelClassName();
51
    }
52
53 347
    protected function bootConfig(): void
54
    {
55
        $this->publishes([
56 347
            __DIR__.'/../config/activitylog.php' => config_path('activitylog.php'),
57
        ], 'config');
58 347
59
        $this->mergeConfigFrom(__DIR__.'/../config/activitylog.php', 'activitylog');
60 347
    }
61
62
    protected function bootMigrations(): void
63
    {
64
        foreach([
65
            'CreateActivityLogTable',
66
            'AddEventColumnToActivityLogTable',
67
        ] as $i => $migration) {
68
            if (! class_exists($migration)) {
69
                $this->publishes([
70
                    __DIR__.'/../migrations/'.Str::snake($migration).'.php.stub' => database_path(sprintf(
71
                        '/migrations/%s_%s.php',
72
                        date('Y_m_d_His', time() + $i),
73
                        Str::snake($migration)
74
                    )),
75
                ], 'migrations');
76
            }
77
        }
78
    }
79
}
80