SeriesController::index()   F
last analyzed

Complexity

Conditions 24
Paths 176

Size

Total Lines 126
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 88
c 3
b 1
f 0
dl 0
loc 126
rs 3.5333
cc 24
nc 176
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\UserSerie;
6
use App\Models\Video;
7
use Blacklight\Releases;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Arr;
10
11
class SeriesController extends BasePageController
12
{
13
    /**
14
     * @throws \Exception
15
     */
16
    public function index(Request $request, string $id = ''): void
17
    {
18
        $this->setPreferences();
19
        $releases = new Releases;
20
        $title = 'Series';
21
        $meta_title = 'View TV Series';
22
        $meta_keywords = 'view,series,tv,show,description,details';
23
        $meta_description = 'View TV Series';
24
25
        if ($id && ctype_digit($id)) {
26
            $category = -1;
27
            if ($request->has('t') && ctype_digit($request->input('t'))) {
28
                $category = $request->input('t');
29
            }
30
31
            $catarray = [];
32
            $catarray[] = $category;
33
34
            $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
35
            $offset = ($page - 1) * config('nntmux.items_per_page');
36
37
            $rel = $releases->tvSearch(['id' => $id], '', '', '', $offset, 1000, '', $catarray, -1);
38
39
            $show = Video::getByVideoID($id);
40
41
            if (! $show) {
42
                $this->smarty->assign('nodata', 'No video information for this series.');
43
            } elseif (! $rel) {
44
                $this->smarty->assign('nodata', 'No releases for this series.');
45
            } else {
46
                $myshows = UserSerie::getShow($this->userdata->id, $show['id']);
47
48
                // Sort releases by season, episode, date posted.
49
                $series = $episode = $posted = [];
50
                foreach ($rel as $rlk => $rlv) {
51
                    $series[$rlk] = $rlv->series;
52
                    $episode[$rlk] = $rlv->episode;
53
                    $posted[$rlk] = $rlv->postdate;
54
                }
55
                Arr::sort($series, [[$episode, false], [$posted, false], $rel]);
56
57
                $series = [];
58
                foreach ($rel as $r) {
59
                    $series[$r->series][$r->episode][] = $r;
60
                }
61
62
                $this->smarty->assign('seasons', Arr::sortRecursive($series));
63
                $this->smarty->assign('show', $show);
64
                $this->smarty->assign('myshows', $myshows);
65
66
                // get series name(s), description, country and genre
67
                $seriestitles = $seriessummary = $seriescountry = [];
68
                $seriestitles[] = $show['title'];
69
70
                if (! empty($show['summary'])) {
71
                    $seriessummary[] = $show['summary'];
72
                }
73
74
                if (! empty($show['countries_id'])) {
75
                    $seriescountry[] = $show['countries_id'];
76
                }
77
78
                $seriestitles = implode('/', array_map('trim', $seriestitles));
79
                $this->smarty->assign('seriestitles', $seriestitles);
80
                $this->smarty->assign('seriessummary', $seriessummary ? array_shift($seriessummary) : '');
81
                $this->smarty->assign('seriescountry', $seriescountry ? array_shift($seriescountry) : '');
82
83
                $title = 'Series';
84
                $meta_title = 'View TV Series';
85
                $meta_keywords = 'view,series,tv,show,description,details';
86
                $meta_description = 'View TV Series';
87
88
                if ($category !== -1) {
89
                    $catid = $category;
90
                } else {
91
                    $catid = '';
92
                }
93
                $this->smarty->assign('category', $catid);
94
                $this->smarty->assign('nodata', '');
95
            }
96
            $content = $this->smarty->fetch('viewseries.tpl');
97
            $this->smarty->assign([
98
                'title' => $title,
99
                'content' => $content,
100
                'meta_title' => $meta_title,
101
                'meta_keywords' => $meta_keywords,
102
                'meta_description' => $meta_description,
103
            ]);
104
            $this->pagerender();
105
        } else {
106
            $letter = ($id && preg_match('/^(0\-9|[A-Z])$/i', $id)) ? $id : '0-9';
107
108
            $showname = ($request->has('title') && ! empty($request->input('title'))) ? $request->input('title') : '';
109
110
            if ($showname !== '' && ! $id) {
111
                $letter = '';
112
            }
113
114
            $masterserieslist = Video::getSeriesList($this->userdata->id, $letter, $showname);
115
116
            $title = 'Series List';
117
            $meta_title = 'View Series List';
118
            $meta_keywords = 'view,series,tv,show,description,details';
119
            $meta_description = 'View Series List';
120
121
            $serieslist = [];
122
            foreach ($masterserieslist as $s) {
123
                if (preg_match('/^[0-9]/', $s['title'])) {
124
                    $thisrange = '0-9';
125
                } else {
126
                    preg_match('/([A-Z]).*/i', $s['title'], $hits);
127
                    $thisrange = strtoupper($hits[1]);
128
                }
129
                $serieslist[$thisrange][] = $s;
130
            }
131
            ksort($serieslist);
132
133
            $this->smarty->assign('serieslist', $serieslist);
134
            $this->smarty->assign('seriesrange', range('A', 'Z'));
135
            $this->smarty->assign('seriesletter', $letter);
136
            $this->smarty->assign('showname', $showname);
137
138
            $content = $this->smarty->fetch('viewserieslist.tpl');
139
140
            $this->smarty->assign(compact('title', 'content', 'meta_title', 'meta_keywords', 'meta_description'));
141
            $this->pagerender();
142
        }
143
    }
144
}
145