Passed
Push — dev ( c55b95...34fb50 )
by Darko
06:54
created

AdminContentController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Models\User;
7
use Blacklight\Contents;
8
use Illuminate\Http\Request;
9
10
class AdminContentController extends BasePageController
11
{
12
    /**
13
     * @throws \Exception
14
     */
15
    public function index()
16
    {
17
        $this->setAdminPrefs();
18
        $contentList = (new Contents())->getAll();
19
        $this->smarty->assign('contentlist', $contentList);
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

19
        $this->smarty->/** @scrutinizer ignore-call */ 
20
                       assign('contentlist', $contentList);

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...
20
21
        $meta_title = 'Content List';
22
23
        $content = $this->smarty->fetch('content-list.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

23
        /** @scrutinizer ignore-call */ 
24
        $content = $this->smarty->fetch('content-list.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...
24
25
        $this->smarty->assign(compact('meta_title', 'content'));
26
27
        $this->adminrender();
28
    }
29
30
    /**
31
     * @param Request $request
32
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
33
     * @throws \Exception
34
     */
35
    public function create(Request $request)
36
    {
37
        $this->setAdminPrefs();
38
        $contents = new Contents();
39
        $meta_title = 'Content Add';
40
41
        // Set the current action.
42
        $action = $request->input('action') ?? 'view';
43
44
        $content = [
45
            'id' => '',
46
            'title' => '',
47
            'url' => '',
48
            'body' => '',
49
            'metadescription' => '',
50
            'metakeywords' => '',
51
            'contenttype' => '',
52
            'showinmenu' => '',
53
            'status' => '',
54
            'ordinal' => '',
55
            'created_at' => '',
56
            'role' => '',
57
        ];
58
59
        switch ($action) {
60
            case 'add':
61
                $meta_title = 'Content Add';
62
                $content['showinmenu'] = '1';
63
                $content['status'] = '1';
64
                $content['contenttype'] = '2';
65
                break;
66
67
            case 'submit':
68
                // Validate and add or update.
69
                if ($request->missing('id') || empty($request->input('id'))) {
70
                    $returnid = $contents->add($request->all());
71
                } else {
72
                    $content = $contents->update($request->all());
73
                    $returnid = $content['id'];
74
                }
75
76
                return redirect('admin/content-add?id='.$returnid);
77
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
78
79
            case 'view':
80
            default:
81
                if ($request->has('id')) {
82
                    $meta_title = 'Content Edit';
83
                    $id = $request->input('id');
84
85
                    $content = $contents->getByID($id, User::ROLE_ADMIN);
86
                }
87
                break;
88
        }
89
90
        $this->smarty->assign('status_ids', [1, 0]);
91
        $this->smarty->assign('status_names', ['Enabled', 'Disabled']);
92
93
        $this->smarty->assign('yesno_ids', [1, 0]);
94
        $this->smarty->assign('yesno_names', ['Yes', 'No']);
95
96
        $contenttypelist = [1 => 'Useful Link', 2 => 'Article', 3 => 'Homepage'];
97
        $this->smarty->assign('contenttypelist', $contenttypelist);
98
99
        $this->smarty->assign('content', $content);
100
101
        $rolelist = [1 => 'Everyone', 2 => 'Logged in Users', 3 => 'Admins'];
102
        $this->smarty->assign('rolelist', $rolelist);
103
104
        $content = $this->smarty->fetch('content-add.tpl');
105
106
        $this->smarty->assign(compact('meta_title', 'content'));
107
108
        $this->adminrender();
109
    }
110
111
    /**
112
     * @param \Illuminate\Http\Request $request
113
     *
114
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
115
     */
116
    public function destroy(Request $request)
117
    {
118
        if ($request->has('id')) {
119
            $contents = new Contents();
120
            $contents->delete($request->input('id'));
121
        }
122
123
        $referrer = $request->server('HTTP_REFERER');
124
125
        return redirect($referrer);
0 ignored issues
show
Bug introduced by
It seems like $referrer can also be of type array; however, parameter $to of redirect() does only seem to accept null|string, 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

125
        return redirect(/** @scrutinizer ignore-type */ $referrer);
Loading history...
126
    }
127
}
128