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

ConsoleController::show()   F

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 88
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 64
c 0
b 0
f 0
dl 0
loc 88
rs 0
cc 23
nc 589824
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Category;
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\Request;
9
use Illuminate\Support\Arr;
10
11
class ConsoleController extends BasePageController
12
{
13
    protected ConsoleService $consoleService;
14
15
    public function __construct(ConsoleService $consoleService)
16
    {
17
        parent::__construct();
18
        $this->consoleService = $consoleService;
19
    }
20
21
    /**
22
     * @throws \Exception
23
     */
24
    public function show(Request $request, string $id = '')
25
    {
26
        if ($id === 'WiiVare') {
27
            $id = 'WiiVareVC';
28
        }
29
        $gen = new Genres;
30
31
        $concats = Category::getChildren(Category::GAME_ROOT);
32
        $ctmp = [];
33
        foreach ($concats as $ccat) {
34
            $ctmp[] =
35
                [
36
                    'id' => $ccat->id,
37
                    'title' => $ccat->title,
38
                ];
39
        }
40
        $category = $request->has('t') ? $request->input('t') : Category::GAME_ROOT;
41
        if ($id && \in_array($id, Arr::pluck($ctmp, 'title'), false)) {
42
            $cat = Category::query()
43
                ->where('title', $id)
44
                ->where('root_categories_id', '=', Category::GAME_ROOT)
45
                ->first(['id']);
46
            $category = $cat !== null ? $cat['id'] : Category::GAME_ROOT;
47
        }
48
49
        $catarray = [];
50
        $catarray[] = $category;
51
52
        $ordering = $this->consoleService->getConsoleOrdering();
53
        $orderby = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : '';
54
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
55
        $offset = ($page - 1) * config('nntmux.items_per_cover_page');
56
57
        $consoles = [];
58
        $rslt = $this->consoleService->getConsoleRange($page, $catarray, $offset, config('nntmux.items_per_cover_page'), $orderby, $this->userdata->categoryexclusions);
59
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_cover_page'), $page, $request->url(), $request->query());
60
61
        $maxwords = 50;
62
        foreach ($results as $result) {
63
            if (! empty($result->review)) {
64
                $words = explode(' ', $result->review);
65
                if (\count($words) > $maxwords) {
66
                    $newwords = \array_slice($words, 0, $maxwords);
67
                    $result->review = implode(' ', $newwords).'...';
68
                }
69
            }
70
            $consoles[] = $result;
71
        }
72
73
        $platform = ($request->has('platform') && ! empty($request->input('platform'))) ? stripslashes($request->input('platform')) : '';
74
        $title = ($request->has('title') && ! empty($request->input('title'))) ? stripslashes($request->input('title')) : '';
75
76
        $genres = $gen->getGenres(Genres::CONSOLE_TYPE, true);
77
        $tmpgnr = [];
78
        foreach ($genres as $gn) {
79
            $tmpgnr[$gn->id] = $gn->title;
80
        }
81
        $genre = ($request->has('genre') && array_key_exists($request->input('genre'), $tmpgnr)) ? $request->input('genre') : '';
82
83
        if ((int) $category === -1) {
84
            $catname = 'All';
85
        } else {
86
            $cdata = Category::find($category);
87
            if ($cdata !== null) {
88
                $catname = $cdata;
89
            } else {
90
                $catname = 'All';
91
            }
92
        }
93
94
        $this->viewData = array_merge($this->viewData, [
95
            'catlist' => $ctmp,
96
            'category' => $category,
97
            'categorytitle' => $id,
98
            'platform' => $platform,
99
            'title' => $title,
100
            'genres' => $genres,
101
            'genre' => $genre,
102
            'catname' => $catname,
103
            'resultsadd' => $consoles,
104
            'results' => $results,
105
            'covgroup' => 'console',
106
            'meta_title' => 'Browse Console',
107
            'meta_keywords' => 'browse,nzb,console,games,description,details',
108
            'meta_description' => 'Browse for Console Games',
109
        ]);
110
111
        return view('console.index', $this->viewData);
112
    }
113
}
114