MessagesController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 76.19 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 96
loc 126
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 18 18 2
A index() 0 23 1
A create() 14 14 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\User;
4
5
use App\Jobs\SendNotifyMail;
6
use App\Models\User;
7
use App\Phphub\Markdown\Markdown;
8
use Carbon\Carbon;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Input;
13
use Illuminate\Support\Facades\Session;
14
use Support\Http\Requests\MessageRequest;
15
use Transmissor\Http\Controllers\User\Controller;
16
use Transmissor\Models\Messenger\Message;
17
use Transmissor\Models\Messenger\Participant;
18
use Transmissor\Models\Messenger\Thread;
19
20
class MessagesController extends Controller
21
{
22
    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...
23
    {
24
        $currentUserId = Auth::id();
25
        $threads = Thread::participateBy($currentUserId);
26
27
        // @todo Refazer essa parte
28
        // if (Auth::user()->newThreadsCount() == 0) {
29
        //     Auth::user()->message_count = 0;
30
        //     Auth::user()->save();
31
        // }
32
33
        $currentActor = Auth::user();
34
35
36
        return view(
37
            'transmissor::users.messages.index',
38
            compact(
39
                'threads',
40
                'currentUserId',
41
                'currentActor'
42
            )
43
        );
44
    }
45
46 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...
47
    {
48
        $thread = Thread::findOrFail($id);
49
        $participant = $thread->participant();
50
        $messages = $thread->messages()->recent()->get();
51
52
        $this->authorize('show', $thread);
53
54
        // counters
55
        $unread_message_count = $thread->userUnreadMessagesCount(Auth::id());
56
        if ($unread_message_count > 0) {
57
            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...
58
            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...
59
        }
60
        $thread->markAsRead(Auth::id());
61
62
        return view('transmissor::users.messages.show', compact('thread', 'participant', 'messages', 'unread_message_count'));
63
    }
64
65 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...
66
    {
67
        if ($id) {
68
            $recipient = User::findOrFail($id);
69
            $thread = Thread::between([$recipient->id, Auth::id()])->first();
0 ignored issues
show
Bug introduced by
The method between() does not exist on Transmissor\Models\Messenger\Thread. Did you maybe mean scopeBetween()?

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...
70
            if ($thread) {
71
                return redirect()->route('profile.transmissor.messages.show', $thread->id);
72
            }
73
            return view('transmissor::users.messages.create', compact('recipient'));
74
        }
75
76
        $recipient = User::pluck('name');
77
        return view('transmissor::users.messages.create', compact('recipient'));
78
    }
79
80 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...
81
    {
82
        // @todo Fazer com as seguintes opcoes
83
        // timePraAutoDestruicao
84
        // Grava em disco ou só memoria
85
        // Criptografia ponta a ponta com limite de data
86
        $recipient = User::findOrFail($id);
87
88
        $thread = Thread::between([$recipient->id, Auth::id()])->first();
0 ignored issues
show
Bug introduced by
The method between() does not exist on Transmissor\Models\Messenger\Thread. Did you maybe mean scopeBetween()?

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...
89
        if ($thread) {
90
            return redirect()->route('profile.transmissor.messages.show', $thread->id);
91
        }
92
93
        return view('transmissor::users.messages.create', compact('recipient'));
94
    }
95
96 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...
97
    {
98
        $recipient = User::findOrFail($request->recipient_id);
99
100
        if ($request->thread_id) {
101
            $thread = Thread::findOrFail($request->thread_id);
102
        } else {
103
            $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...
104
            $thread = Thread::create(['subject' => $subject]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Transmissor\Models\Messenger\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...
105
        }
106
107
        // Message
108
        $message = $markdown->convertMarkdownToHtml($request->message);
109
        Message::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Transmissor\Models\Messenger\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...
110
            [
111
                'messageable_type' => Thread::class,
112
                'messageable_id' => $thread->id,
113
                'actorable_type' => User::class,
114
                'actorable_id' => Auth::id(),
115
                'body' => $message
116
            ]
117
        );
118
119
        // Sender
120
        $participant = Participant::firstOrCreate(
121
            [
122
                'messageable_type' => Thread::class,
123
                'messageable_id' => $thread->id,
124
                'actorable_type' => User::class,
125
                'actorable_id' => Auth::id()
126
            ]
127
        );
128
        $participant->last_read = new Carbon;
129
        $participant->save();
130
131
        // Recipient
132
        $thread->addParticipant($recipient->id);
133
134
        // Notify user by Email
135
        $job = (new SendNotifyMail('new_message', Auth::user(), $recipient, null, null, $message))
136
                                ->delay(\Illuminate\Support\Facades\Config::get('phphub.notify_delay'));
137
        dispatch($job);
138
139
        // notifications count
140
        $recipient->message_count++;
141
        $recipient->save();
142
143
        return redirect()->route('profile.transmissor.messages.show', $thread->id);
144
    }
145
}
146