Passed
Push — master ( f28993...808c23 )
by Darko
11:06
created

AdminConsoleController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 15
eloc 48
c 1
b 1
f 0
dl 0
loc 87
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 9 1
C edit() 0 60 13
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Services\ConsoleService;
0 ignored issues
show
Bug introduced by
The type App\Services\ConsoleService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Blacklight\Genres;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Carbon;
11
use Illuminate\View\View;
12
13
class AdminConsoleController extends BasePageController
14
{
15
    protected ConsoleService $consoleService;
16
17
    public function __construct(ConsoleService $consoleService)
18
    {
19
        parent::__construct();
20
        $this->consoleService = $consoleService;
21
    }
22
23
    /**
24
     * Display a listing of console games
25
     */
26
    public function index(): View
27
    {
28
        $this->setAdminPrefs();
29
30
        $meta_title = $title = 'Console List';
31
32
        $consoleList = getRange('consoleinfo');
33
34
        return view('admin.console.index', compact('consoleList', 'title', 'meta_title'));
35
    }
36
37
    /**
38
     * Show the form for editing a console game
39
     */
40
    public function edit(Request $request): View|RedirectResponse
41
    {
42
        $this->setAdminPrefs();
43
        $gen = new Genres;
44
        $meta_title = $title = 'Console Edit';
45
46
        // set the current action
47
        $action = $request->input('action', 'view');
48
49
        if ($request->has('id')) {
50
            $id = $request->input('id');
51
            $con = $this->consoleService->getConsoleInfo($id);
52
53
            if (! $con) {
54
                abort(404);
55
            }
56
57
            switch ($action) {
58
                case 'submit':
59
                    $coverLoc = storage_path('covers/console/'.$id.'.jpg');
60
61
                    if ($request->hasFile('cover') && $request->file('cover')->isValid()) {
62
                        $uploadedFile = $request->file('cover');
63
                        $file_info = getimagesize($uploadedFile->getRealPath());
64
                        if (! empty($file_info)) {
65
                            $uploadedFile->move(storage_path('covers/console'), $id.'.jpg');
66
                        }
67
                    }
68
69
                    $hasCover = file_exists($coverLoc) ? 1 : 0;
70
                    $salesrank = (empty($request->input('salesrank')) || ! ctype_digit($request->input('salesrank'))) ? null : $request->input('salesrank');
71
                    $releasedate = (empty($request->input('releasedate')) || ! strtotime($request->input('releasedate')))
72
                        ? $con['releasedate']
73
                        : Carbon::parse($request->input('releasedate'))->timestamp;
74
75
                    $this->consoleService->update(
76
                        $id,
77
                        $request->input('title'),
78
                        $request->input('asin'),
79
                        $request->input('url'),
80
                        $salesrank,
81
                        $request->input('platform'),
82
                        $request->input('publisher'),
83
                        $releasedate,
84
                        $request->input('esrb'),
85
                        $hasCover,
86
                        $request->input('genre')
87
                    );
88
89
                    return redirect()->route('admin.console-list')->with('success', 'Console game updated successfully');
90
91
                case 'view':
92
                default:
93
                    $genres = $gen->getGenres(Genres::CONSOLE_TYPE);
94
95
                    return view('admin.console.edit', compact('con', 'genres', 'title', 'meta_title'));
96
            }
97
        }
98
99
        return redirect()->route('admin.console-list');
100
    }
101
}
102