Completed
Push — master ( f2c61d...01b1a9 )
by Thomas
05:04
created

ModelStatusServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 18 3
A register() 0 4 1
A getStatusModel() 0 10 2
1
<?php
2
3
namespace Spatie\ModelStatus;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\ServiceProvider;
7
use Spatie\ModelStatus\Exceptions\InvalidStatusModel;
8
9
class ModelStatusServiceProvider extends ServiceProvider
10
{
11
    public function boot()
12
    {
13
        if ($this->app->runningInConsole()) {
14
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations/');
15
        }
16
17
        if (! class_exists('CreateStatusesTable')) {
18
            $timestamp = date('Y_m_d_His', time());
19
20
            $this->publishes([
21
                __DIR__.'/../database/migrations/create_statuses_table.php.stub.stub' => database_path('migrations/'.$timestamp.'create_statuses_table.php'),
22
            ], 'migrations');
23
        }
24
25
        $this->publishes([
26
            __DIR__.'/../config/model-status.php' => config_path('model-status.php'),
27
        ], 'config');
28
    }
29
30
    public function register()
31
    {
32
        $this->mergeConfigFrom(__DIR__.'/../config/model-status.php', 'model-status');
33
    }
34
35
    public static function getStatusModel(): string
36
    {
37
        $statusModel = config('model-status.status_model') ?? Status::class;
38
39
        if (!is_a($statusModel, Status::class, true)) {
40
            throw InvalidStatusModel::create($statusModel);
41
        }
42
43
        return $statusModel;
44
    }
45
}
46