MessagesController::update()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
rs 8.5806
cc 4
eloc 20
nc 4
nop 1
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 Carbon\Carbon;
11
use App\Models\User;
12
use Cmgmyr\Messenger\Models\Thread;
13
use Cmgmyr\Messenger\Models\Message;
14
use Illuminate\Support\Facades\Input;
15
use Illuminate\Support\Facades\Session;
16
use Cmgmyr\Messenger\Models\Participant;
17
use Illuminate\Database\Eloquent\ModelNotFoundException;
18
19
class MessagesController extends Controller
20
{
21
    public function index()
22
    {
23
        $currentUserId = \Auth::id();
24
25
        //Alle threads laden, abgesehen von gelöscht und archivierten empfängern
26
        //$threads = Thread::getAllLatest()->get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
27
28
        // All threads that user is participating in
29
        $threads = Thread::forUser($currentUserId)->latest('updated_at')->paginate(25);
0 ignored issues
show
Bug introduced by
The method forUser() does not exist on Cmgmyr\Messenger\Models\Thread. Did you maybe mean scopeForUser()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
30
        // All threads that user is participating in, with new messages
31
        // $threads = Thread::forUserWithNewMessages($currentUserId)->latest('updated_at')->get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
32
33
        $users = User::where('id', '!=', \Auth::id())->get();
34
35
        return view('messenger.index', compact('threads', 'currentUserId', 'users'));
36
    }
37
38
    public function create()
39
    {
40
        $users = User::where('id', '!=', \Auth::id())->get();
41
42
        return view('messenger.create', compact('users'));
43
    }
44
45
    public function store()
46
    {
47
        $input = Input::get();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
48
        $thread = Thread::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Cmgmyr\Messenger\Models\Thread. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
            [
50
                'subject' => $input['subject'],
51
            ]
52
        );
53
        // Message
54
        Message::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Cmgmyr\Messenger\Models\Message. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
55
            [
56
                'thread_id' => $thread->id,
57
                'user_id'   => \Auth::user()->id,
58
                'body'      => $input['msg'],
59
            ]
60
        );
61
        // Sender
62
        Participant::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Cmgmyr\Messenger\Models\Participant. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
63
            [
64
                'thread_id' => $thread->id,
65
                'user_id'   => \Auth::user()->id,
66
                'last_read' => new Carbon(),
67
            ]
68
        );
69
        // Recipients
70
        foreach ($input['recipients'] as $rec) {
71
            Participant::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Cmgmyr\Messenger\Models\Participant. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
72
                [
73
                    'thread_id' => $thread->id,
74
                    'user_id'   => $rec,
75
                ]
76
            );
77
        }
78
79
        return redirect('messages');
80
    }
81
82
    public function show($id)
83
    {
84
        if (\Auth::check()) {
85
            try {
86
                $thread = Thread::findOrFail($id);
87
            } catch (ModelNotFoundException $e) {
88
                Session::flash('error_message', 'Thema mit der ID: '.$id.' konnte nicht gefunden werden.');
89
90
                return redirect('messages');
91
            }
92
            // show current user in list if not a current participant
93
            // $users = User::whereNotIn('id', $thread->participantsUserIds())->get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
94
            // don't show the current user in list
95
            $userId = \Auth::user()->id;
96
            if ($thread->hasParticipant($userId)) {
97
                $users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get();
98
                $thread->markAsRead($userId);
99
                $messages = $thread->messages()->paginate(25);
100
101
                if (! Input::get('page')) {
102
                    return redirect('messages/'.$id.'?page='.$messages->lastPage());
103
                } else {
104
                    return view('messenger.show', compact('thread', 'users', 'messages'));
105
                }
106
            }
107
            //Todo:View für Keine Berechtigung.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
108
        }
109
    }
110
111
    public function update($id)
112
    {
113
        if (\Auth::check()) {
114
            try {
115
                $thread = Thread::findOrFail($id);
116
            } catch (ModelNotFoundException $e) {
117
                Session::flash('error_message', 'Thema mit der ID: '.$id.' konnte nicht gefunden werden.');
118
119
                return redirect('messages');
120
            }
121
            $thread->activateAllParticipants();
122
            // Message
123
            Message::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Cmgmyr\Messenger\Models\Message. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
124
                [
125
                    'thread_id' => $thread->id,
126
                    'user_id'   => \Auth::id(),
127
                    'body'      => Input::get('msg'),
128
                ]
129
            );
130
            // Add replier as a participant
131
            $participant = Participant::firstOrCreate(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
132
                [
133
                    'thread_id' => $thread->id,
134
                    'user_id'   => \Auth::user()->id,
135
                ]
136
            );
137
            $participant->last_read = new Carbon();
138
            $participant->save();
139
            // Recipients
140
            if (Input::has('recipients')) {
141
                $thread->addParticipant(Input::get('recipients'));
142
            }
143
144
            return redirect('messages/'.$id);
145
        }
146
147
        //Todo:View für Keine Berechtigung
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
148
    }
149
}
150