Passed
Pull Request — 2.x (#1456)
by Harings
08:37
created

SingletonModuleController::seed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use Illuminate\Support\Facades\Session;
6
7
abstract class SingletonModuleController extends ModuleController
8
{
9
    protected $permalinkBase = '';
10
11
    public function index($parentModuleId = null)
12
    {
13
        throw new \Exception("{$this->getModelName()} has no index");
14
    }
15
16
    public function editSingleton()
17
    {
18
        $model = "App\\Models\\{$this->getModelName()}";
19
20
        $item = app($model)->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        $item = app($model)->/** @scrutinizer ignore-call */ first();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
22
        if (!$item) {
23
            if (config('twill.auto_seed_singletons', false)) {
24
                $this->seed();
25
                return $this->editSingleton();
26
            }
27
            throw new \Exception("$model is not seeded");
28
        }
29
30
        Session::put('pages_back_link', url()->current());
31
32
        return view("admin.{$this->moduleName}.form", $this->form($item->id));
33
    }
34
35
    private function seed(): void
36
    {
37
        $seederName = '\\Database\\Seeders\\' . $this->getModelName() . 'Seeder';
38
        if (!class_exists($seederName)) {
39
            throw new \Exception("$seederName is missing");
40
        }
41
        $seeder = new $seederName();
42
        $seeder->run();
43
    }
44
}
45