Completed
Push — master ( 1c3cb2...337d33 )
by Sebastian
03:55
created

ModuleController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 1
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Back;
4
5
use Activity;
6
use App\Http\Controllers\Controller;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Http\Request;
9
10
abstract class ModuleController extends Controller
11
{
12
    /** @var string */
13
    protected $modelName = null;
14
15
    /** @var string */
16
    protected $moduleName = null;
17
18
    /** @return \Illuminate\Database\Eloquent\Model */
19
    abstract protected function make();
20
21
    public function index()
22
    {
23
        $models = $this->query()->nonDraft()->get();
24
25
        $data = [
26
            $this->moduleName => $models,
27
        ];
28
29
        return view("back.{$this->moduleName}.index", $data);
30
    }
31
32
    public function create()
33
    {
34
        $model = $this->make();
35
36
        return redirect()->action("Back\\{$this->modelName}Controller@edit", [$model->id]);
37
    }
38
39
    public function show($id)
40
    {
41
        return redirect()->action("Back\\{$this->modelName}Controller@edit", [$id]);
42
    }
43
44
    public function edit(Request $request, $id)
45
    {
46
        $model = $this->query()->find($id);
47
48
        if ($request->has('revert')) {
49
            $model->clearTemporaryMedia();
50
51
            return redirect()->action("Back\\{$this->modelName}Controller@edit", [$id]);
52
        }
53
54
        $data = [
55
            'model' => $model,
56
            'module' => $this->moduleName,
57
        ];
58
59
        return view("back.{$this->moduleName}.edit", $data);
60
    }
61
62
    public function update(int $id)
63
    {
64
        $request = app()->make("App\\Http\\Requests\\Back\\{$this->modelName}Request");
65
66
        $model = $this->query()->find($id);
67
68
        $this->createUpdater($model, $request)->update();
69
70
        $model->save();
71
        app('cache')->flush();
72
73
        $eventDescription = $this->getUpdatedEventDescription($model);
74
        Activity::log($eventDescription);
75
        flash()->success(strip_tags($eventDescription));
76
77
        return redirect()->action("Back\\{$this->modelName}Controller@edit", [$model->id]);
78
    }
79
80
    protected function createUpdater(Model $model, Request $request)
81
    {
82
        $className = "App\\Models\\Updaters\\{$this->modelName}Updater";
83
84
        return $className::create($model, $request);
85
    }
86
87
    public function destroy($id)
88
    {
89
        $model = $this->query()->find($id);
90
91
        $eventDescription = $this->getDeletedEventDescription($model);
92
        Activity::log($eventDescription);
93
        flash()->success(strip_tags($eventDescription));
94
95
        $model->delete();
96
        app('cache')->flush();
97
98
        return redirect()->action("Back\\{$this->modelName}Controller@index");
99
    }
100
101
    public function changeOrder(Request $request)
102
    {
103
        $model = "\\App\\Models\\{$this->modelName}";
104
105
        $model::setNewOrder($request->get('ids'));
106
    }
107
108
    protected function getUpdatedEventDescription($model)
109
    {
110
        $modelName = trans("back-{$this->moduleName}.singular");
111
112
        $linkToModel = link_to_action("Back\\{$this->modelName}Controller@edit", $model->name, ['id' => $model->id]);
113
114
        if ($model->wasDraft) {
115
            return trans('back.events.created', ['model' => $modelName, 'name' => $linkToModel]);
116
        }
117
118
        return trans('back.events.updated', ['model' => $modelName, 'name' => $linkToModel]);
119
    }
120
121
    protected function getDeletedEventDescription($model)
122
    {
123
        $modelName = trans("back-{$this->moduleName}.singular");
124
125
        return trans('back.events.deleted', ['model' => $modelName, 'name' => $model->name]);
126
    }
127
128
    protected function query()
129
    {
130
        return call_user_func("App\\Models\\{$this->modelName}::query");
131
    }
132
}
133