TeamController   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 327
Duplicated Lines 9.48 %

Importance

Changes 0
Metric Value
wmc 31
dl 31
loc 327
rs 9.8
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A get_teams() 14 19 2
B list_teams_memebers() 0 58 7
A get_team_name() 5 5 1
A update_team_name() 5 5 1
A __construct() 0 3 1
A getTeamMembers() 0 19 3
A deleteTeam() 0 8 1
A activate_invited_member() 0 20 2
B list_teams_and_team_memebers() 0 38 3
B create_team_channels() 0 27 3
B create_team_members() 0 70 6
A update_team_head() 5 5 1

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
use LaravelRealtimeChat\Repositories\Team\TeamRepository;
3
use LaravelRealtimeChat\Repositories\Task\TaskRepository;
4
class TeamController extends \BaseController {
5
 /**
6
         * @var LaravelRealtimeChat\Repositories\TeamRepository
0 ignored issues
show
Bug introduced by
The type LaravelRealtimeChat\Repositories\TeamRepository 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...
7
         */
8
        private $teamRepository;
9
        /**
10
         * @var LaravelRealtimeChat\Repositories\TaskRepository
0 ignored issues
show
Bug introduced by
The type LaravelRealtimeChat\Repositories\TaskRepository 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...
11
         */
12
        private $taskRepository;
13
14
        public function __construct(TeamRepository $teamRepository, TaskRepository $taskRepository) {
15
                $this->teamRepository = $teamRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $teamRepository of type LaravelRealtimeChat\Repo...ies\Team\TeamRepository is incompatible with the declared type LaravelRealtimeChat\Repositories\TeamRepository of property $teamRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
16
                $this->taskRepository = $taskRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $taskRepository of type LaravelRealtimeChat\Repo...ies\Task\TaskRepository is incompatible with the declared type LaravelRealtimeChat\Repositories\TaskRepository of property $taskRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
17
        }
18
    /**
19
     * @function creates team
20
     * @return message string
21
     */
22
    public function create_team_channels() {
23
        $teamName = Input::get('name');
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...
24
        if ($teamName == '')
25
            return View::make('templates/greeting')->with('danger', "Please provide a team name!");
26
//Check for duplicates
27
        $exists = DB::table('team_channels')
28
                        ->select('channel_view_name')
29
                        ->where('channel_view_name', '=', $teamName)
30
                        ->where('author_id', '=', Auth::user()->id)
31
                        ->where('channel_view_name', '=', $teamName)->first();
32
        if (count($exists) == 0) {
33
            $id = DB::table('team_channels')->insertGetId(
34
                    ['channel_name_id' => Session::get('channelId'), 'team_channel_id' => Hash::make(Auth::user()->email), 'author_id' => Auth::user()->id, 'channel_view_name' => $teamName]
35
            );
36
            //Add the admin as user to the team created
37
            DB::table('team_channel_users')->insert(
38
                    ['team_channel_name_id' => $id, 'user_id' => Auth::user()->id]
39
            );
40
            
41
            //Make  admin as default team head
42
             \DB::table('team_heads')->insertGetId(
43
                            [ "team_id" => $id, 'author_id' => \Session::get("userId"), "user_id" =>\Session::get("userId")]
44
                    );
45
            $data = array("create_team" => true, "message" => "Team Created Succesfully.", "teamId" => $id);
46
            return View::make('templates/greeting')->with('data', $data);
47
        } else {
48
            return View::make('templates/greeting')->with('danger', "Team name already in use.Pleas try other!");
49
        }
50
    }
51
52
    public function get_teams() {
53
        $channelId = Input::get('channelId');
54 View Code Duplication
        if (Session::get("role") == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            $teams = DB::table('channels')
56
                            ->select('team_channels.id','team_channels.team_channel_id', 'channels.channel_name', 'team_channels.channel_view_name', 'channels.author_id', "team_channels.created_at",'team_heads.author_id','team_heads.user_id as team_head_id')
57
                            ->leftJoin('team_channels', 'channels.id', '=', 'team_channels.channel_name_id')
58
                            ->leftJoin('team_heads', 'team_heads.team_id', '=', 'team_channels.id')
59
                            ->where('team_channels.id', '!=', '')
60
                            ->where('channels.id', '=', $channelId)->distinct()->get();
61
        } else {
62
            $teams = DB::table('team_channels')
63
                            ->select('team_channels.channel_view_name', 'team_channels.team_channel_id', 'team_channels.id', 'team_channels.created_at','team_heads.author_id','team_heads.user_id as team_head_id')
64
                            ->leftJoin('team_channel_users', 'team_channel_users.team_channel_name_id', '=', 'team_channels.id')
65
                            ->leftJoin('team_heads', 'team_heads.team_id', '=', 'team_channels.id')
66
                            ->where('team_channel_users.user_id', '=', Auth::user()->id)->distinct()->get();
67
        }
68
69
70
        return View::make('templates/team_list_view')->with('teams', $teams);
71
    }
72
73
    public function create_team_members() {
74
75
// Get the value from the form
76
        $teamId = Input::get('teamId');
77
        $emails = Input::get('email');
78
        $failCheck = false;
79
        $failedEmail = '';
80
  
81
        foreach ($emails as $email) {
82
83
            /**
84
             * If user doesnt exists in fm database,trigger a mail for account creation
85
             * if user already exists check if he exists in team else add him to team
86
             * 
87
             */
88
            //get user id
89
90
            $userId = DB::table('users')
91
                            ->select('id')
92
                            ->where('email', '=', $email)->first();
93
94
            if (count($userId) > 0) {//if user already exists
95
                //check if user is already a member of this team
96
                $exists = DB::table('team_channel_users')
97
                                ->select('team_channel_users.user_id')
98
                                ->where('team_channel_users.user_id', '=', $userId['id'])
99
                                ->where('team_channel_users.team_channel_name_id', '=', $teamId)->first();
100
101
                if (count($exists) == 0) {//if exists insert into team_channel_users
102
                    DB::table('team_channel_users')->insert(
103
                            ['team_channel_name_id' => $teamId, 'user_id' => $userId['id']]
104
                    );
105
                   
106
                    $this->teamRepository->updateNewMemberJoinNotification($teamId,$userId['id']);
107
                } else {
108
                    $failCheck = true;
109
                    $failedEmail.=$email;
110
                }
111
            } else {
112
                /* new user,trigger email
113
                 * generate a token for each invitation
114
                 * 
115
                 */
116
117
                $token = Crypt::encrypt(time());
118
                ;
119
                $url = Config::get('constants.constants_list.BASE_URL') . "invitation/" . $token;
120
                $data = array("teamId" => $teamId, "email" => $email, "url" => $url, "token" => $token);
121
122
123
                Mail::send('templates/email_template', $data, function($message)use ($data) {
124
125
                            $message->to($data['email'], '')
126
                                    ->subject('Welcome to Fusion Mate!');
127
                            //->setBody("Hi, <br/> You have an inviation from a team.Click below url and join to collaborate with your team members <br/>".$data['url']);
128
                            if (Mail::failures()) {
129
                                return "There was an error in sending email to one of the recipients";
130
                            } else {
131
                                DB::table('invitation_token')->insert(
132
                                        ['token' => $data['token'],'team_id'=>$data['teamId'],'email'=>$data['email']]
133
                                );
134
                            }
135
                        });
136
            }
137
        }
138
139
        if ($failCheck != true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
140
            return View::make('templates/greeting')->with('message', "Invited Successfully!");
141
        else
142
            return View::make('templates/greeting')->with('danger', "Email already exists." . ":" . $failedEmail);
143
// Create a random  'pass' and send invitation via mail
144
//            $id = DB::table('users')->insertGetId(
145
//                ['first_name' => Input::get('email'),'last_name' => Input::get('email'),'email' => Input::get('email'), 'password' => Hash::make(Input::get('email'))]
146
//        );
147
//        DB::table('user_roles')->insert(
148
//                ['user_id' => $id, 'role_id' => "3"]
149
//        );
150
//            DB::table('team_channel_users')->insert(
151
//                    ['team_channel_name_id' => $teamId, 'user_id' => $id]
152
//            );
153
    }
154
155
    public function list_teams_and_team_memebers() {
156
        /**
157
         * 
158
         * Get all team details where the user is a memeber
159
         * 
160
         * 
161
         * 
162
         */
163
        $teamArr = array();
164
        $teamName = DB::table('team_channels')
165
                        ->select('team_channels.channel_view_name', 'team_channels.team_channel_id', 'team_channels.id')
166
                        ->leftJoin('team_channel_users', 'team_channel_users.team_channel_name_id', '=', 'team_channels.id')
167
                        ->where('team_channel_users.user_id', '=', Auth::user()->id)->get();
168
        /**
169
         * Loothrough each team ids obtained and get team users
170
         * 
171
         * 
172
         */
173
        foreach ($teamName as $values) {
174
            $teamMembers = DB::table('users')
175
                            ->select('users.first_name', 'users.id')
176
                            ->leftJoin('team_channel_users', 'team_channel_users.user_id', '=', 'users.id')
177
                            ->where('team_channel_users.team_channel_name_id', '=', $values["id"])->get();
178
            $queries = DB::getQueryLog();
0 ignored issues
show
Unused Code introduced by
The assignment to $queries is dead and can be removed.
Loading history...
179
//            if (count($teamMembers) > 1) {// atelast there should be one user except the admin who craeted the team
180
                foreach ($teamMembers as $memeber) {
181
                    /**
182
                     * Get memeber login status too
183
                     * 
184
                     */
185
                    $status = DB::table('login_status')->select('status')->where('user_id', $memeber["id"])->first();
186
                    $teamArr[$values["channel_view_name"] . "_" . $values["team_channel_id"]."_".$values["id"]][] = $memeber["first_name"] . "_" . $memeber["id"] . "_" . $status['status'];
187
//                }
188
            }
189
        }
190
       // echo $this->db->last_query(); exit;
191
//        print_r($teamArr);
192
        return json_encode($teamArr);
193
    }
194
195
    /**
196
      @function gets team members
197
      @param team id  - id of team to list its members
198
199
     */
200
    function getTeamMembers() {
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...
201
        $teamId = Input::get('teamId');
202
        $teamArr = array();
203
        $teamMembers = DB::table('users')
204
                        ->select('users.first_name', 'users.id', 'users.last_name')
205
                        ->leftJoin('team_channel_users', 'team_channel_users.user_id', '=', 'users.id')
206
                        ->where('team_channel_users.team_channel_name_id', '=', $teamId)->get();
207
208
209
        foreach ($teamMembers as $memeber) {
210
            /**
211
             * Get memeber login status too
212
             * 
213
             */
214
            $status = DB::table('login_status')->select('status')->where('user_id', $memeber["id"])->first();
215
            if ($memeber['id'] != Session::get('userId'))
216
                $teamArr[] = $memeber["first_name"] . "_" . $memeber["last_name"] . "_" . $memeber["id"] . "_" . $status['status'];
217
        }
218
        return View::make('templates/list_members')->with('members', $teamArr);
219
    }
220
221
    function activate_invited_member( $token) {
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...
222
        //Validate token for security reasons 
223
        $isValidToken = DB::table('invitation_token')
224
                        ->select('token')
225
                        ->where('token', '=', $token)->first();
226
        //Team Name
227
228
        if (count($isValidToken) > 0) {
229
            $invitationDetails= DB::table('invitation_token')
230
                            ->select('team_id','email')
231
                            ->where('token', '=', $token)->first();
232
                    
233
            $teamName = DB::table('team_channels')
234
                            ->select('channel_view_name')
235
                            ->where('id', '=', $invitationDetails['team_id'])->first();
236
            $data = array("teamName" => $teamName['channel_view_name'], "email" => $invitationDetails['email'], "token" => $token, "teamId" => $invitationDetails['team_id']);
237
            return View::make('layouts/invitation_view')->with('data', $data);
238
        }
239
        else
240
            return "your not authorized to access this page";
241
    }
242
function deleteTeam(){
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...
243
       $enTeamId=Input::get("teamId");
244
        $teamId=$this->teamRepository->getTeamDecodedId($enTeamId);
245
        DB::table('team_conversations_read_status')->where('team_channel_id', $teamId)->delete();
246
         DB::table('team_conversations')->where('team_channel_id', $teamId)->delete();
247
        DB::table('team_channel_users')->where('team_channel_name_id', $teamId)->delete();
248
        $status=  DB::table('team_channels')->where('id', $teamId)->delete();
249
        return json_encode($status);
250
    }
251
    
252
         public function list_teams_memebers() {
253
       
254
              $offlineSort=array();
255
            $onlineSort=array();
256
             /**
257
         * 
258
         * Get all team details where the user is a memeber
259
         * 
260
         * 
261
         * 
262
         */
263
        $teamArr = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $teamArr is dead and can be removed.
Loading history...
264
         $data = array();
265
        $teamName = DB::table('team_channels')
266
                        ->select('team_channels.channel_view_name', 'team_channels.team_channel_id', 'team_channels.id')
267
                        ->leftJoin('team_channel_users', 'team_channel_users.team_channel_name_id', '=', 'team_channels.id')
268
                        ->where('team_channel_users.user_id', '=', Auth::user()->id)->get();
269
        /**
270
         * Loothrough each team ids obtained and get team users
271
         * 
272
         * 
273
         */
274
        foreach ($teamName as $values) {
275
           
276
            $teamMembers = DB::table('users')
277
                            ->select('users.first_name','users.last_name', 'users.id','users.profile_pic','team_channels.channel_view_name','team_channels.id as team_id')
278
                            ->leftJoin('team_channel_users', 'team_channel_users.user_id', '=', 'users.id')
279
                            ->leftJoin('team_channels', 'team_channels.id', '=', 'team_channel_users.team_channel_name_id')
280
                            ->where('team_channel_users.team_channel_name_id', '=', $values["id"])->get();
281
            if (count($teamMembers) > 1) {// atelast there should be one user except the admin who craeted the team
282
                foreach ($teamMembers as $memeber) {
283
                    /**
284
                     * Get memeber login status too
285
                     * left join `team_channels` on `team_channels`.`id` = `team_channel_users`.`team_channel_name_id`
286
                     */
287
                    $status = DB::table('login_status')->select('status')->where('user_id', $memeber["id"])->first();
288
                    $memeber['status']=$status;
289
                    if($status['status']=="offline")
290
                        $offlineSort[]=$memeber;
291
                    else
292
                        $onlineSort[]=$memeber;
293
                    
294
                    if(count($onlineSort)==0)//if there are no online users do not merge
295
                        $data[]=$memeber;
296
                
297
                }
298
   
299
              
300
            }
301
        } 
302
  
303
       
304
        if(count($onlineSort)>0){// if online users are there pass merged data else pass non merged data
305
          
306
            return View::make('templates/team_members')->with('data',  array_merge($onlineSort,$offlineSort));
307
        }
308
        else
309
            return View::make('templates/team_members')->with('data',$data  );
310
    }
311
312 View Code Duplication
    public function get_team_name(){
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...
313
        $enTeamId=Input::get("teamId");
314
        $teamId=$this->teamRepository->getTeamDecodedId($enTeamId);
315
        $data = $this->teamRepository->getTeamName($teamId);
316
        return $data;
317
    }
318
    
319 View Code Duplication
    public function update_team_name(){
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...
320
        $enTeamId=Input::get("teamId");
321
        $teamId=$this->teamRepository->getTeamDecodedId($enTeamId);
322
        $data = $this->teamRepository->updateTeamName($teamId);
323
        return $data;
324
    }
325
    
326 View Code Duplication
    public function update_team_head(){
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...
327
        $enTeamId=Input::get("teamId");
328
        $teamId=$this->teamRepository->getTeamDecodedId($enTeamId);
329
        $data = $this->teamRepository->updateTeamHead($teamId);
330
        return $data;
331
    }
332
     
333
334
}