AlbumController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 34
c 1
b 0
f 0
dl 0
loc 129
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlbumType() 0 3 1
A store() 0 5 1
A getMediaFiles() 0 3 1
A create() 0 5 1
A edit() 0 9 1
A index() 0 6 1
A view() 0 9 1
A update() 0 5 1
A delete() 0 14 3
A getAlbumTitle() 0 3 1
1
<?php
2
3
namespace Itstructure\MFU\Http\Controllers\Albums;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Itstructure\GridView\DataProviders\EloquentDataProvider;
7
use Itstructure\MFU\Http\Controllers\BaseController;
8
use Itstructure\MFU\Http\Requests\{StoreAlbum, UpdateAlbum, Delete};
9
use Itstructure\MFU\Models\Albums\AlbumTyped;
10
use Itstructure\MFU\Models\Mediafile;
11
12
/**
13
 * Class AlbumController
14
 * @package Itstructure\MFU\Http\Controllers\Albums
15
 */
16
abstract class AlbumController extends BaseController
17
{
18
    /**
19
     * @return string|AlbumTyped
20
     */
21
    abstract protected function getModelClass(): string;
22
23
    /**
24
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
25
     */
26
    public function index()
27
    {
28
        return view('uploader::albums.index', [
29
            'title' => $this->getAlbumTitle(true),
30
            'type' => $this->getAlbumType(),
31
            'dataProvider' => new EloquentDataProvider(($this->getModelClass())::query()->where('type', '=', $this->getAlbumType()))
32
        ]);
33
    }
34
35
    /**
36
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
37
     */
38
    public function create()
39
    {
40
        return view('uploader::albums.create', [
41
            'title' => trans('uploader::main.create') . ' ' . mb_strtolower($this->getAlbumTitle()),
0 ignored issues
show
Bug introduced by
Are you sure trans('uploader::main.create') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

41
            'title' => /** @scrutinizer ignore-type */ trans('uploader::main.create') . ' ' . mb_strtolower($this->getAlbumTitle()),
Loading history...
42
            'type' => $this->getAlbumType()
43
        ]);
44
    }
45
46
    /**
47
     * @param StoreAlbum $request
48
     * @return \Illuminate\Http\RedirectResponse
49
     */
50
    public function store(StoreAlbum $request)
51
    {
52
        ($this->getModelClass())::create($request->all());
53
54
        return redirect()->route('uploader_' . $this->getAlbumType() . '_list');
55
    }
56
57
    /**
58
     * @param int $id
59
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
60
     */
61
    public function edit(int $id)
62
    {
63
        $model = ($this->getModelClass())::findOrFail($id);
64
65
        return view('uploader::albums.edit', [
66
            'title' => trans('uploader::main.edit') . ' ' . mb_strtolower($this->getAlbumTitle()),
0 ignored issues
show
Bug introduced by
Are you sure trans('uploader::main.edit') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

66
            'title' => /** @scrutinizer ignore-type */ trans('uploader::main.edit') . ' ' . mb_strtolower($this->getAlbumTitle()),
Loading history...
67
            'type' => $this->getAlbumType(),
68
            'model' => $model,
69
            'mediaFiles' => $this->getMediaFiles($model)
70
        ]);
71
    }
72
73
    /**
74
     * @param int $id
75
     * @param UpdateAlbum $request
76
     * @return \Illuminate\Http\RedirectResponse
77
     */
78
    public function update(int $id, UpdateAlbum $request)
79
    {
80
        ($this->getModelClass())::findOrFail($id)->update($request->all());
81
82
        return redirect()->route('uploader_' . $this->getAlbumType() . '_view', ['id' => $id]);
83
    }
84
85
    /**
86
     * @param Delete $request
87
     * @return \Illuminate\Http\RedirectResponse
88
     */
89
    public function delete(Delete $request)
90
    {
91
        foreach ($request->items as $id) {
92
93
            if (!is_numeric($id)) {
94
                continue;
95
            }
96
97
            ($this->getModelClass())::find($id)
98
                ->setRemoveDependencies(!empty($request->get('remove_dependencies')))
99
                ->delete();
100
        }
101
102
        return redirect()->route('uploader_' . $this->getAlbumType() . '_list');
103
    }
104
105
    /**
106
     * @param int $id
107
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
108
     */
109
    public function view(int $id)
110
    {
111
        $model = ($this->getModelClass())::findOrFail($id);
112
113
        return view('uploader::albums.view', [
114
            'title' => trans('uploader::main.view') . ' ' . mb_strtolower($this->getAlbumTitle()),
0 ignored issues
show
Bug introduced by
Are you sure trans('uploader::main.view') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

114
            'title' => /** @scrutinizer ignore-type */ trans('uploader::main.view') . ' ' . mb_strtolower($this->getAlbumTitle()),
Loading history...
115
            'type' => $this->getAlbumType(),
116
            'model' => $model,
117
            'mediaFiles' => $this->getMediaFiles($model)
118
        ]);
119
    }
120
121
    /**
122
     * @param bool $plural
123
     * @return string
124
     */
125
    protected function getAlbumTitle(bool $plural = false): string
126
    {
127
        return ($this->getModelClass())::getAlbumTitle($this->getAlbumType(), $plural);
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    protected function getAlbumType(): string
134
    {
135
        return ($this->getModelClass())::getAlbumType();
136
    }
137
138
    /**
139
     * @param AlbumTyped $model
140
     * @return Collection|Mediafile[]
141
     */
142
    protected function getMediaFiles(AlbumTyped $model): Collection
143
    {
144
        return $model->getMediaFiles(($this->getModelClass())::getFileType());
145
    }
146
}
147