MessageController::send_message()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 74
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 8.4878
c 0
b 0
f 0
cc 5
eloc 39
nc 9
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class MessageController extends \BaseController {
4
5
    /**
6
     * Display a listing of messages.
7
     *
8
     * @return Response
9
     */
10
    public function index() {
11
12
        $conversation = Conversation::where('name', Input::get('conversation'))->first();
0 ignored issues
show
Bug introduced by
The type Input was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
        $messages       = Message::where('conversation_id', $conversation->id)->orderBy('created_at')->get();
14
15
        return View::make('templates/messages')->with('messages', $messages)->render();
16
    }
17
18
    /**
19
     * Store a newly created message in storage.
20
     *
21
     * @return Response
22
     */
23
    public function send_message() {
24
        $isPersonalSet=Input::get('isPersonalFlag');
25
        $rules     = array('body' => 'required');
26
        $validator = Validator::make(Input::all(), $rules);
27
28
        if($validator->fails()) {
29
            return Response::json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return Response::json(ar...validator->messages())) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Response.
Loading history...
30
                'success' => false,
31
                'result' => $validator->messages()
32
            ]);
33
        }
34
35
       
36
37
        $params = array(
0 ignored issues
show
Unused Code introduced by
The assignment to $params is dead and can be removed.
Loading history...
38
            'channel_id' => Input::get('channelId'),
39
            'body'               => Input::get('body'),
40
            'user_id'           => Input::get('user_id')
41
           
42
        );
43
        //decode team id
44
        $team_decoded_id = DB::table('team_channels')->where('team_channel_id', Input::get('channelId'))->first();
45
        /**
46
         * 
47
         * Get the id of the last inserted message
48
         */
49
		if (strpos(Input::get('body'), '<img') !== false) {
50
		   $lastId= DB::table('messages')->insertGetId(
51
                ['message' => strip_tags( Input::get('body'),'<img></img><img /><p></p><br/><br><span></span></code><code><table><td></td><tr></tr><a></a>')]
52
        );
53
		}
54
		else{
55
	       $lastId= DB::table('messages')->insertGetId(
56
                ['message' => strip_tags(preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', Input::get('body')),'<img></img><img /><p></p><br/><br><span></span></code><code><table><td></td><tr></tr><a></a>')]
57
        );}
58
       
59
   
60
       /**
61
        * if personal flag is set to true insert into personal conversations
62
        * 
63
        */
64
       if($isPersonalSet){
65
           $teamChannelId=explode("_",Input::get('channelId'));
66
           
67
           DB::table('personal_conversations')->insert(
68
                     ['team_channel_id' => $teamChannelId[1], 'to_user_id' => Input::get('toUserId'),'from_user_id' => Auth::user()->id,'message_id' => $lastId,'read_status'=>1]
69
             );
70
       }
71
       else{
72
            DB::table('team_conversations')->insert(
73
                     ['team_channel_id' => Input::get('channelId'), 'user_id' => Input::get('userId'),'message_id' => $lastId]
74
             );
75
              $this->insertIntoTeamConversationReadStatus(Input::get('channelId'),$lastId);
76
       }
77
        // Publish Data To Redis
78
        
79
        if($isPersonalSet){
80
            $data = array(
81
                'room'        => Input::get('channelId'),
82
                'message'  => array( 'body' => Input::get('body'),'to_channel_id'=>Auth::user()->id."_".$teamChannelId[1], 'user_id' => Auth::user()->id,'first_name'=> Session::get('firstName'),'last_name'=> Session::get('lastName'),'profile_pic'=> Session::get('profilePic'),'message_id'=>$lastId ,'to_user_id'=>Input::get('toUserId'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $teamChannelId does not seem to be defined for all execution paths leading up to this point.
Loading history...
83
            );
84
            Event::fire(ChatMessagesEventHandler::EVENT, array(json_encode($data)));
85
        }
86
        else{
87
            $data = array(
88
                'room'        => Input::get('channelId'),
89
                'message'  => array( 'body' => Input::get('body'), 'user_id' => Input::get('userId'),'first_name'=> Session::get('firstName'),'last_name'=> Session::get('lastName'),'profile_pic'=> Session::get('profilePic'),'team_encoded_id'=> Input::get('channelId'),'team_decoded_id'=> $team_decoded_id['id'],'message_id'=>$lastId)
90
            );
91
            Event::fire(ChatConversationsEventHandler::EVENT, array(json_encode($data)));
92
93
        }
94
        return Response::json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return Response::json(ar...ue, 'result' => $data)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Response.
Loading history...
95
            'success' => true,
96
            'result' => $data
97
        ]);
98
    }
99
    
100
     function insertIntoTeamConversationReadStatus($channelId,$messageId){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101
       
102
          $teamId = DB::table('team_channels')
103
                        ->select('id')
104
                        
105
                        ->where('team_channel_id', '=', $channelId)->first();
106
        $teamMembers = DB::table('users')
107
                        ->select('users.first_name', 'users.id', 'users.last_name')
108
                        ->leftJoin('team_channel_users', 'team_channel_users.user_id', '=', 'users.id')
109
                        ->where('team_channel_users.team_channel_name_id', '=', $teamId['id'])->get();
110
111
112
        foreach ($teamMembers as $memeber) {
113
           
114
         
115
            if ($memeber['id'] != Session::get('userId'))
116
                DB::table('team_conversations_read_status')->insert(
117
                     ['team_channel_id' => $teamId['id'], 'message_id' => $messageId,'user_id' => $memeber['id']]
118
             );
119
        }
120
    }
121
}
122