NewsController::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Http\Controllers;
9
10
use App\Models\News;
11
use Illuminate\Http\Request;
12
13
class NewsController extends Controller
14
{
15
    /**
16
     * Display a listing of the resource.
17
     *
18
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
19
     */
20
    public function index()
21
    {
22
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
        $news = \DB::table('news')
24
            ->leftJoin('users', 'news.user_id', '=', 'users.id')
25
            ->leftJoin('comments', function ($join) {
26
                $join->on('comments.content_id', '=', 'news.id');
27
                $join->on('comments.content_type', '=', \DB::raw("'news'"));
28
            })
29
            ->select(['news.id', 'news.title', 'news.user_id', 'users.name', 'news.created_at', 'news.approved', 'news.news_html'])
30
            ->selectRaw('COUNT(comments.id) as counter')
31
            ->orderBy('news.created_at', 'desc')
32
            ->groupBy('news.id')
33
            ->get();
34
*/
35
36
        $news = News::orderBy('created_at', 'desc')->paginate(25);
37
38
        return view('news.index', [
39
            'news' => $news,
40
        ]);
41
    }
42
43
    /**
44
     * Show the form for creating a new resource.
45
     *
46
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...racts\View\Factory|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
     */
48
    public function create()
49
    {
50
        if (\Auth::check()) {
51
            return view('news.create');
52
        }
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param \Illuminate\Http\Request $request
59
     *
60
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62
    public function store(Request $request)
63
    {
64
        if (\Auth::check()) {
65
            if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
66
                $n = new News();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
67
                $n->news_category = $request->get('cat');
68
                $n->user_id = \Auth::id();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
                $n->news_md = $request->get('msg');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
                $n->news_html = \Markdown::convertToHtml($request->get('msg'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
                $n->title = $request->get('title');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
                $n->approved = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
74
                $n->save();
75
76
                return redirect()->action('NewsController@show', $n->id);
0 ignored issues
show
Documentation introduced by
$n->id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
            } else {
78
                return redirect()->action('IndexController@index');
79
            }
80
        } else {
81
            return redirect()->action('IndexController@index');
82
        }
83
    }
84
85
    /**
86
     * Display the specified resource.
87
     *
88
     * @param int $id
89
     *
90
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92
    public function show($id)
93
    {
94
        $news = News::whereId($id)->first();
95
96
        return view('news.show', [
97
            'news' => $news,
98
        ]);
99
    }
100
101
    /**
102
     * Show the form for editing the specified resource.
103
     *
104
     * @param int $id
105
     *
106
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...e\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
107
     */
108
    public function edit($id)
109
    {
110
        if (\Auth::check()) {
111
            if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
112
                $news = News::whereId($id)->first();
113
114
                return view('news.edit', [
115
                    'news' => $news,
116
                ]);
117
            } else {
118
                return redirect()->action('IndexController@index');
119
            }
120
        } else {
121
            return redirect()->action('IndexController@index');
122
        }
123
    }
124
125
    /**
126
     * Update the specified resource in storage.
127
     *
128
     * @param \Illuminate\Http\Request $request
129
     * @param int                      $id
130
     *
131
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
132
     */
133
    public function update(Request $request, $id)
134
    {
135
        if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
136
            $this->validate($request, [
137
                'title' => 'required',
138
                'msg'   => 'required',
139
                'cat'   => 'required',
140
            ]);
141
142
            $news = News::whereId($id)->first();
143
144
            $news->title = $request->get('title');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
145
            $news->news_md = $request->get('msg');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
146
            $news->news_html = \Markdown::convertToHtml($request->get('msg'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
147
            $news->news_category = $request->get('cat');
148
            $news->save();
149
        }
150
151
        return redirect()->action('NewsController@show', $id);
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
152
    }
153
154
    /**
155
     * Remove the specified resource from storage.
156
     *
157
     * @param int $id
158
     *
159
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
160
     */
161
    public function destroy($id)
162
    {
163
        if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
164
            $news = News::whereId($id)->first();
165
            $news->delete();
166
        }
167
168
        return redirect()->action('NewsController@index');
169
    }
170
171
    public function approve($id, $approve)
172
    {
173
        if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
174
            $news = News::whereId($id)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
175
            $news->approved = $approve;
176
            $news->save();
177
        }
178
179
        return redirect()->action('NewsController@show', $id);
180
    }
181
}
182