MessagesController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 24 5
A create() 0 21 3
A store() 0 13 1
1
<?php
2
3
namespace Aurawindsurfing\Messenger\Http\Controllers;
4
5
use App\User;
6
use Illuminate\Support\Facades\Auth;
7
use Aurawindsurfing\Messenger\Thread;
8
9
class MessagesController
10
{
11
    public function index(Thread $thread)
12
    {
13
        $user = Auth::user();
14
15
        if (! isset($user)) {
16
            return abort(404);
17
        }
18
19
        if (! $thread->users()->get()->contains($user)) {
20
            return abort(404);
21
        }
22
23
        $contacts = User::all();
24
        $threads = null;
25
        $activeThread = Thread::find($thread->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Aurawindsurfing\Messenger\Thread>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
27
        if (isset($user)) {
28
            if ($user->threads()->exists()) {
0 ignored issues
show
Bug introduced by
The method threads() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
                $threads = $user->threads()->with('messages')->get();
0 ignored issues
show
Bug introduced by
The method threads() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            }
31
        }
32
33
        return view('vendor.messenger.index', compact('threads', 'activeThread', 'contacts'));
34
    }
35
36
    public function create($user)
37
    {
38
        $fromUser = Auth::user();
39
        $contacts = User::all();
40
        $threads = collect([]);
41
42
        $activeThread = Thread::firstOrCreate([
43
            'from_user_id' => $fromUser->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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
44
            'to_user_id'   => $user,
45
        ]);
46
47
        if (isset($fromUser)) {
48
            if ($fromUser->threads()->exists()) {
0 ignored issues
show
Bug introduced by
The method threads() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
                $threads = $fromUser->threads()->with('messages')->get();
0 ignored issues
show
Bug introduced by
The method threads() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
            }
51
        }
52
53
        $threads = collect([$activeThread])->merge($threads);
54
55
        return view('vendor.messenger.index', compact('threads', 'activeThread', 'contacts'));
56
    }
57
58
    public function store(Thread $thread)
59
    {
60
        request()->merge(['user_id' => Auth::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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
61
62
        request()->validate([
63
            'body'    => 'required',
64
            'user_id' => 'required',
65
        ]);
66
67
        $thread->addMessage(request('body'), Auth::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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
68
69
        return redirect($thread->path());
70
    }
71
}
72