Passed
Push — dev ( 9d591d...c59ba0 )
by Darko
22:01
created

BooksController::index()   F

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 89
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 89
rs 0
c 0
b 0
f 0
cc 20
nc 73728
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 Blacklight\Books;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Arr;
9
10
class BooksController extends BasePageController
11
{
12
    /**
13
     * @param \Illuminate\Http\Request $request
14
     *
15
     * @param string                   $id
16
     *
17
     * @throws \Exception
18
     */
19
    public function index(Request $request, $id = '')
20
    {
21
        $this->setPrefs();
22
        $book = new Books(['Settings' => $this->settings]);
23
24
        $boocats = Category::getChildren(Category::BOOKS_ROOT);
25
26
        $btmp = [];
27
        foreach ($boocats as $bcat) {
28
            $btmp[] =
29
                [
30
                    'id' => $bcat->id,
31
                    'title' => $bcat->title,
32
                ];
33
        }
34
        $category = $request->has('t') ? $request->input('t') : Category::BOOKS_ROOT;
35
        if ($id && \in_array($id, Arr::pluck($btmp, 'title'), false)) {
36
            $cat = Category::query()
37
                ->where('title', $id)
38
                ->where('root_categories_id', '=', Category::BOOKS_ROOT)
39
                ->first(['id']);
40
            $category = $cat !== null ? $cat['id'] : Category::BOOKS_ROOT;
41
        }
42
43
        $catarray = [];
44
        $catarray[] = $category;
45
46
        $this->smarty->assign('catlist', $btmp);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

46
        $this->smarty->/** @scrutinizer ignore-call */ 
47
                       assign('catlist', $btmp);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $this->smarty->assign('category', $category);
48
        $this->smarty->assign('categorytitle', $id);
49
50
        $ordering = $book->getBookOrdering();
51
        $orderby = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : '';
52
53
        $books = [];
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
        $rslt = $book->getBookRange($page, $catarray, $offset, config('nntmux.items_per_cover_page'), $orderby, $this->userdata->categoryexclusions);
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
57
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_cover_page'), $page, $request->url(), $request->query());
58
        $maxwords = 50;
59
        foreach ($results as $result) {
60
            if (! empty($result->overview)) {
61
                $words = explode(' ', $result->overview);
62
                if (\count($words) > $maxwords) {
63
                    $newwords = \array_slice($words, 0, $maxwords);
64
                    $result->overview = implode(' ', $newwords).'...';
65
                }
66
            }
67
            $books[] = $result;
68
        }
69
70
        $author = ($request->has('author') && ! empty($request->input('author'))) ? stripslashes($request->input('author')) : '';
71
        $this->smarty->assign('author', $author);
72
73
        $title = ($request->has('title') && ! empty($request->input('title'))) ? stripslashes($request->input('title')) : '';
74
        $this->smarty->assign('title', $title);
75
76
        $browseby_link = '&amp;title='.$title.'&amp;author='.$author;
77
78
        if ((int) $category === -1) {
79
            $this->smarty->assign('catname', 'All');
80
        } else {
81
            $cdata = Category::find($category);
82
            if ($cdata !== null) {
83
                $this->smarty->assign('catname', $cdata);
84
            } else {
85
                $this->smarty->assign('catname', 'All');
86
            }
87
        }
88
89
        foreach ($ordering as $ordertype) {
90
            $this->smarty->assign('orderby'.$ordertype, url('/books?t='.$category.$browseby_link.'&amp;ob='.$ordertype.'&amp;offset=0'));
91
        }
92
93
        $this->smarty->assign(
94
            [
95
                'resultsadd'=>  $books,
96
                'results' => $results,
97
                'covgroup' => 'books',
98
            ]
99
        );
100
101
        $meta_title = 'Browse Books';
102
        $meta_keywords = 'browse,nzb,books,description,details';
103
        $meta_description = 'Browse for Books';
104
        $content = $this->smarty->fetch('books.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

104
        /** @scrutinizer ignore-call */ 
105
        $content = $this->smarty->fetch('books.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
        $this->smarty->assign(compact('content', 'meta_title', 'meta_keywords', 'meta_description'));
106
107
        $this->pagerender();
108
    }
109
}
110