NoteController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 51.61 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 32
loc 62
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A store() 16 16 2
A edit() 0 8 1
A update() 16 16 2
A destroy() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SET\Http\Controllers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Facades\Request;
7
use Illuminate\Support\Facades\Storage;
8
use Krucas\Notification\Facades\Notification;
9
use SET\Attachment;
10
use SET\Http\Requests\StoreNoteRequest;
11
use SET\Http\Requests\UpdateNoteRequest;
12
use SET\Note;
13
use SET\User;
14
15
class NoteController extends Controller
16
{
17
    public function create(User $user)
18
    {
19
        $this->authorize('edit');
20
21
        return view('note.create', compact('user'));
22
    }
23
24 View Code Duplication
    public function store(StoreNoteRequest $request, User $user)
25
    {
26
        $this->authorize('edit');
27
        $data = $request->all();
28
        $data['author_id'] = Auth::user()->id;
29
30
        $note = $user->notes()->create($data);
31
32
        if (Request::hasFile('files')) {
33
            Attachment::upload($note, Request::file('files'), $data['encrypt']);
34
        }
35
36
        Notification::container()->success('Note successfully created');
37
38
        return redirect()->action('UserController@show', $user->id);
39
    }
40
41
    public function edit(User $user, $noteID)
42
    {
43
        $this->authorize('edit');
44
45
        $note = Note::findOrFail($noteID);
46
47
        return view('note.edit', compact('user', 'note'));
48
    }
49
50 View Code Duplication
    public function update(UpdateNoteRequest $request, $userID, $noteId)
51
    {
52
        $this->authorize('edit');
53
        $note = Note::findOrFail($noteId);
54
        $note['author_id'] = Auth::user()->id;
55
        $data = $request->all();
56
        $note->update($data);
57
58
        if (Request::hasFile('files')) {
59
            Attachment::upload($note, Request::file('files'), $data['encrypt']);
60
        }
61
62
        Notification::container()->success('Note successfully updated');
63
64
        return redirect()->action('UserController@show', $userID);
65
    }
66
67
    public function destroy($userID, Note $note)
68
    {
69
        $this->authorize('edit');
70
71
        $note->delete();
72
        Storage::deleteDirectory('note_'.$note->id);
73
74
        return back();
75
    }
76
}
77