Completed
Push — master ( e1cce4...d2c19b )
by David
09:34 queued 11s
created

NewsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A create() 0 4 1
A store() 0 30 2
A show() 0 4 1
A edit() 0 8 1
A update() 0 4 1
A destroy() 0 4 1
1
<?php
2
3
namespace Torralbodavid\DuckFunkCore\Http\Controllers\Housekeeping;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use Torralbodavid\DuckFunkCore\Http\Request\Housekeeping\News\NewsStoreRequest;
8
use Torralbodavid\DuckFunkCore\Http\Resources\Marketing\NewsResource;
9
use Torralbodavid\DuckFunkCore\Models\Housekeeping\News;
10
11
class NewsController extends Controller
12
{
13
    /**
14
     * Display a listing of the resource.
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function index()
19
    {
20
        //
21
    }
22
23
    /**
24
     * Show the form for creating a new resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function create()
29
    {
30
        return view('housekeeping::news.edit');
31
    }
32
33
    /**
34
     * Store a newly created resource in storage.
35
     *
36
     * @param NewsStoreRequest $request
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function store(NewsStoreRequest $request)
40
    {
41
        $validated = $request->validated();
42
43
        $news = News::firstOrCreate(
44
            [
45
                'title' => $validated['title'],
46
                'subtitle' => $validated['subtitle'],
47
                'body' => $validated['body'],
48
                'author' => core()->user()->id,
49
                'published_at' => $validated['publish_date'],
50
            ]);
51
52
        $news->categories = $validated['allCategories'];
53
        $news->draft = 0;
54
55
        try {
56
            $news->saveOrFail();
57
        } catch (\Throwable $e) {
58
            return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
                'message' => "Ha sucedido un error {$e->getMessage()}",
60
                'error' => true,
61
            ]);
62
        }
63
64
        return response()->json([
65
            'message' => 'Noticia publicada correctamente',
66
            'error' => false,
67
        ]);
68
    }
69
70
    /**
71
     * Display the specified resource.
72
     *
73
     * @param int $id
74
     * @return \Illuminate\Http\Response
75
     */
76
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        //
79
    }
80
81
    /**
82
     * Show the form for editing the specified resource.
83
     *
84
     * @param News $news
85
     * @return \Illuminate\Http\Response
86
     */
87
    public function edit(News $news)
88
    {
89
        $data = [
90
            'news' => new NewsResource($news),
91
        ];
92
93
        return view('housekeeping::news.edit', $data);
94
    }
95
96
    /**
97
     * Update the specified resource in storage.
98
     *
99
     * @param \Illuminate\Http\Request $request
100
     * @param int $id
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        //
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param int $id
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        //
117
    }
118
}
119