Passed
Push — master ( c35458...7dcfdb )
by Darko
10:35
created

ContentController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 48
dl 0
loc 116
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B show() 0 60 7
A getContentById() 0 6 1
A getActiveContent() 0 5 1
A getIndexContent() 0 3 1
A getAllButFront() 0 6 1
A getFrontPageContent() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Content;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
9
class ContentController extends BasePageController
10
{
11
    /**
12
     * Display content page(s).
13
     *
14
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
15
     * @throws \Exception
16
     */
17
    public function show(Request $request)
18
    {
19
        $role = $this->userdata->role ?? 0;
20
21
        /* The role column in the content table values are:
22
         * 1 = logged in users
23
         * 2 = admins
24
         *
25
         * The user role values are:
26
         * 1 = user
27
         * 2 = admin
28
         * 3 = disabled
29
         * 4 = moderator
30
         *
31
         * Admins and mods should be the only ones to see admin content.
32
         */
33
        $isAdmin = \in_array($role, [User::ROLE_ADMIN, User::ROLE_MODERATOR], true);
34
35
        $contentId = $request->input('id', 0);
36
        $contentPage = $request->input('page', false);
37
38
        if ($contentId === 0 && $contentPage === 'content') {
39
            // Show all content except front page
40
            $content = $this->getAllButFront()->all();
41
            $isFront = false;
42
            $meta_title = 'Contents page';
43
            $meta_keywords = 'contents';
44
            $meta_description = 'This is the contents page.';
45
        } elseif ($contentId !== 0 && $contentPage !== false) {
46
            // Show specific content by ID
47
            $contentItem = $this->getContentById($contentId, $role);
0 ignored issues
show
Bug introduced by
It seems like $role can also be of type Spatie\Permission\Models\Role; however, parameter $role of App\Http\Controllers\Con...oller::getContentById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

47
            $contentItem = $this->getContentById($contentId, /** @scrutinizer ignore-type */ $role);
Loading history...
48
            $content = $contentItem ? [$contentItem] : [];
49
            $isFront = false;
50
            $meta_title = 'Contents page';
51
            $meta_keywords = 'contents';
52
            $meta_description = 'This is the contents page.';
53
        } else {
54
            // Show front page content
55
            $content = $this->getFrontPageContent()->all();
56
            $index = $this->getIndexContent();
57
            $isFront = true;
58
            $meta_title = $index?->title ?? 'Contents page';
59
            $meta_keywords = $index?->metakeywords ?? 'contents';
60
            $meta_description = $index?->metadescription ?? 'This is the contents page.';
61
        }
62
63
        if (empty($content)) {
64
            return response()->json(['message' => 'There is nothing to see here, no content provided.'], 404);
65
        }
66
67
        $this->viewData = array_merge($this->viewData, [
68
            'content' => $content,
69
            'admin' => $isAdmin,
70
            'front' => $isFront,
71
            'meta_title' => $meta_title,
72
            'meta_keywords' => $meta_keywords,
73
            'meta_description' => $meta_description,
74
        ]);
75
76
        return view('content.index', $this->viewData);
77
    }
78
79
    /**
80
     * Get all active content ordered by type and ordinal.
81
     */
82
    protected function getActiveContent(): \Illuminate\Database\Eloquent\Collection
83
    {
84
        return Content::active()
85
            ->orderByRaw('contenttype, COALESCE(ordinal, 1000000)')
86
            ->get();
87
    }
88
89
    /**
90
     * Get all content except the front page.
91
     */
92
    protected function getAllButFront(): \Illuminate\Database\Eloquent\Collection
93
    {
94
        return Content::query()
95
            ->where('id', '<>', 1)
96
            ->orderByRaw('contenttype, COALESCE(ordinal, 1000000)')
97
            ->get();
98
    }
99
100
    /**
101
     * Get content by ID with role-based access control.
102
     */
103
    protected function getContentById(int $id, int $role): ?Content
104
    {
105
        return Content::query()
106
            ->where('id', $id)
107
            ->forRole($role)
108
            ->first();
109
    }
110
111
    /**
112
     * Get front page content.
113
     */
114
    protected function getFrontPageContent(): \Illuminate\Database\Eloquent\Collection
115
    {
116
        return Content::frontPage()->get();
117
    }
118
119
    /**
120
     * Get index content metadata.
121
     */
122
    protected function getIndexContent(): ?Content
123
    {
124
        return Content::active()->ofType(Content::TYPE_INDEX)->first();
125
    }
126
}
127