Completed
Push — master ( 15e1a8...208337 )
by Manel
03:15
created

MessagesController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 41
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 5 1
A sendMessage() 0 12 1
A fetchMessages() 0 5 1
1
<?php
2
3
namespace Scool\EnrollmentMobile\Http\Controllers;
4
5
use Auth;
6
use Illuminate\Http\Request;
7
use Manelgavalda\EnrollmentMobileTest\Notifications\MessageSent;
8
use Manelgavalda\EnrollmentMobileTest\Notifications\MessageSent as MessageSentNotification;
9
use Scool\EnrollmentMobile\Models\Message;
10
11
/**
12
 * Class GcmTokensController.
13
 *
14
 * @package ManelGavalda\TodosBackend\Http\Controllers
15
 */
16
/**
17
 * Class MessagesController.
18
 *
19
 * @package ManelGavalda\TodosBackend\Http\Controllers
20
 */
21
class MessagesController extends TodosBaseController
22
{
23
    /**
24
     *
25
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
26
     */
27
    public function index()
28
    {
29
        $data = [];
30
        return view('messages',$data);
31
    }
32
    /**
33
     * Persist message to database
34
     *
35
     * @param  Request $request
36
     *
37
     * @return array
38
     */
39
    public function sendMessage(Request $request)
40
    {
41
//      $user = Auth::user();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
42
        $user = $request->user();
43
        $message = $user->messages()->create([
44
            'message' => $request->input('message')
45
        ]);
46
        //Broadcast
47
        broadcast(new MessageSent($user,$message))->toOthers();
48
        $user->notify(new MessageSentNotification($user,$message));
49
        return ['status' => 'Message Sent!'];
50
    }
51
    /**
52
     * Fetch all messages
53
     *
54
     * @return Message
55
     */
56
    public function fetchMessages()
57
    {
58
        //Lazy loading -> Eager Loading
59
        return Message::with('user')->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
60
    }
61
}