MessagesController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 85.84 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 97
loc 113
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 18 18 2
A index() 0 9 2
A create() 15 15 3
A store() 49 49 2
A createSecureMessage() 15 15 2

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 Transmissor\Http\Controllers;
4
5
use App\Http\Requests\MessageRequest;
6
use App\Jobs\SendNotifyMail;
7
use App\Models\Message;
8
use App\Models\Participant;
9
use App\Models\Thread;
10
use App\Models\User;
11
use App\Phphub\Markdown\Markdown;
12
use Carbon\Carbon;
13
use Illuminate\Database\Eloquent\ModelNotFoundException;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\Support\Facades\Input;
17
use Illuminate\Support\Facades\Session;
18
19
class MessagesController extends Controller
20
{
21
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
22
    {
23
        $threads = Thread::participateBy(Auth::id());
24
        if (Auth::user()->newThreadsCount() == 0) {
0 ignored issues
show
Bug introduced by
The method newThreadsCount() 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...
25
            Auth::user()->message_count = 0;
0 ignored issues
show
Bug introduced by
Accessing message_count 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...
26
            Auth::user()->save();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method save() does only exist in the following implementations of said interface: Illuminate\Foundation\Auth\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
27
        }
28
        return view('transmissor::users.messages.index', compact('threads', 'currentUserId'));
29
    }
30
31 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $thread = Thread::findOrFail($id);
34
        $participant = $thread->participant();
35
        $messages = $thread->messages()->recent()->get();
36
37
        $this->authorize('show', $thread);
38
39
        // counters
40
        $unread_message_count = $thread->userUnreadMessagesCount(Auth::id());
41
        if ($unread_message_count > 0) {
42
            Auth::user()->message_count -= $unread_message_count;
0 ignored issues
show
Bug introduced by
Accessing message_count 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...
43
            Auth::user()->save();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method save() does only exist in the following implementations of said interface: Illuminate\Foundation\Auth\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
44
        }
45
        $thread->markAsRead(Auth::id());
46
47
        return view('transmissor::users.messages.show', compact('thread', 'participant', 'messages', 'unread_message_count'));
48
    }
49
50 View Code Duplication
    public function create($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        if ($id) {
53
            $recipient = User::findOrFail($id);
54
            $thread = Thread::between([$recipient->id, Auth::id()])->first();
55
            if ($thread) {
56
                return redirect()->route('profile.transmissor.messages.show', $thread->id);
57
            }
58
            return view('transmissor::users.messages.create', compact('recipient'));
59
        }
60
61
        $recipient = User::pluck('name');
62
63
        return view('transmissor::users.messages.create', compact('recipient'));
64
    }
65
66 View Code Duplication
    public function createSecureMessage($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        // @todo Fazer com as seguintes opcoes
69
        // timePraAutoDestruicao
70
        // Grava em disco ou só memoria
71
        // Criptografia ponta a ponta com limite de data
72
        $recipient = User::findOrFail($id);
73
74
        $thread = Thread::between([$recipient->id, Auth::id()])->first();
75
        if ($thread) {
76
            return redirect()->route('profile.transmissor.messages.show', $thread->id);
77
        }
78
79
        return view('transmissor::users.messages.create', compact('recipient'));
80
    }
81
82 View Code Duplication
    public function store(MessageRequest $request, Markdown $markdown)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $recipient = User::findOrFail($request->recipient_id);
85
86
        if ($request->thread_id) {
87
            $thread = Thread::findOrFail($request->thread_id);
88
        } else {
89
            $subject = Auth::user()->name . ' 给 ' . $recipient->name . ' 的私信。';
0 ignored issues
show
Bug introduced by
Accessing name 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...
90
            $thread = Thread::create(['subject' => $subject]);
91
        }
92
93
        // Message
94
        $message = $markdown->convertMarkdownToHtml($request->message);
95
        Message::create(
96
            [
97
                'messageable_type' => Thread::class,
98
                'messageable_id' => $thread->id,
99
                'actorable_type' => User::class,
100
                'actorable_id' => Auth::id(),
101
                'body' => $message
102
            ]
103
        );
104
105
        // Sender
106
        $participant = Participant::firstOrCreate(
107
            [
108
                'messageable_type' => Thread::class,
109
                'messageable_id' => $thread->id,
110
                'actorable_type' => User::class,
111
                'actorable_id' => Auth::id()
112
            ]
113
        );
114
        $participant->last_read = new Carbon;
115
        $participant->save();
116
117
        // Recipient
118
        $thread->addParticipant($recipient->id);
119
120
        // Notify user by Email
121
        $job = (new SendNotifyMail('new_message', Auth::user(), $recipient, null, null, $message))
122
                                ->delay(config('phphub.notify_delay'));
123
        dispatch($job);
124
125
        // notifications count
126
        $recipient->message_count++;
127
        $recipient->save();
128
129
        return redirect()->route('profile.transmissor.messages.show', $thread->id);
130
    }
131
}
132