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

ModelStatusServiceProvider::getStatusModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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