ChatsController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Private;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Conversation;
7
use App\Models\Message;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
11
class ChatsController extends Controller
12
{
13
    /**
14
     * Display a listing of the resource.
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function index()
19
    {
20
        $user = Auth::user();
21
22
        return Conversation::with('users')
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Conver...two', $user->id)->get() returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
23
            ->where('user_one', $user->id)
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
24
            ->orWhere('user_two', $user->id)
25
            ->get();
26
    }
27
28
    /**
29
     * Store a newly created resource in storage.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function store(Request $request)
34
    {
35
        $user = Auth::user();
36
        $conversation = Conversation::find($request->get('conversation_id'))->get();
37
        if ($conversation->status) {
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property status does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property status does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
38
            $message = $user->messages()->create([
0 ignored issues
show
Bug introduced by
The method messages() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            $message = $user->/** @scrutinizer ignore-call */ messages()->create([
Loading history...
39
                'message' => $request->input('message'),
40
                'conversation_id' => $request->input('conversation_id'),
41
            ]);
42
            // $message = new Message();
43
            // $message->message = $request->get('message');
44
            // $message->user_id = $user->id;
45
            // $message->conversation_id = $request->get('conversation_id');
46
            // $message->save();
47
            broadcast(new \App\Events\MessageSent($message, $user, $conversation))->toOthers();
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Events\MessageSent::__construct() does only seem to accept App\Models\User, 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

47
            broadcast(new \App\Events\MessageSent($message, /** @scrutinizer ignore-type */ $user, $conversation))->toOthers();
Loading history...
48
49
            return ['message' => 'Message Sent!'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('message' => 'Message Sent!') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
50
        }
51
    }
52
53
    /**
54
     * Fetch all messages.
55
     *
56
     * @param  int  $id
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function show($id)
60
    {
61
        return Conversation::with('message', 'users')
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Conver...ers')->find($id)->get() also could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
62
            ->find($id)
63
            ->get();
64
    }
65
66
    /**
67
     * Update the specified resource in storage.
68
     *
69
     * @param  int  $id
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function update(Request $request, $id)
73
    {
74
        $user = auth()->user();
75
        $conversation = Conversation::find($request->get('conversation_id'))->get();
76
        if ($conversation->status) {
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property status does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property status does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
77
            $message = Message::find($id);
78
            $message->udpate($request->all());
79
            broadcast(new \App\Events\MessageSent($message, $user, $conversation))->toOthers();
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type Illuminate\Database\Eloq...gHasThroughRelationship and null; however, parameter $message of App\Events\MessageSent::__construct() does only seem to accept App\Models\Message, 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

79
            broadcast(new \App\Events\MessageSent(/** @scrutinizer ignore-type */ $message, $user, $conversation))->toOthers();
Loading history...
80
81
            return ['message' => $message->load('user')];
82
        }
83
    }
84
85
    /**
86
     * Remove the specified resource from storage.
87
     *
88
     * @param  int  $id
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

91
    public function destroy(/** @scrutinizer ignore-unused */ $id)

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

Loading history...
92
    {
93
        // $message = Message::destroy($id);
94
        // $user = auth()->user();
95
        // broadcast(new \App\Events\MessageSent($message, $user, $conversation))->toOthers();
96
        // return ['message' => $message];
97
    }
98
99
    public function ping(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function ping(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
100
    {
101
    }
102
}
103