GroupController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 6.58 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 10
loc 152
ccs 0
cts 83
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 1
A create() 10 10 1
A store() 0 10 1
A edit() 0 12 1
A update() 0 13 3
A destroy() 0 5 1
A getUserIDs() 0 11 2
A assignTraining() 0 30 4

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 SET\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Event;
8
use Illuminate\Support\Facades\Input;
9
use SET\Events\TrainingAssigned;
10
use SET\Group;
11
use SET\Http\Requests\GroupRequest;
12
use SET\Training;
13
use SET\TrainingUser;
14
use SET\User;
15
16
/**
17
 * Class GroupController.
18
 */
19
class GroupController extends Controller
20
{
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function index()
27
    {
28
        $this->authorize('view');
29
30
        $groups = Group::with([
31
            'users' => function ($q) {
32
                $q->active()->orderBy('last_name');
33
            },
34
            'trainings',
35
        ])->get()->sortBy('name');
36
37
        return view('group.index', compact('groups'));
38
    }
39
40 View Code Duplication
    public function create()
41
    {
42
        $this->authorize('edit');
43
44
        $training = Training::orderBy('name')->get()->pluck('name', 'id')->toArray();
45
        $users = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('UserFullName', 'id')->toArray();
46
        $selectedTraining = $selectedUsers = [];
47
48
        return view('group.create', compact('users', 'training', 'selectedTraining', 'selectedUsers'));
49
    }
50
51
    /**
52
     * @param GroupRequest $request
53
     *
54
     * @return \Illuminate\Http\RedirectResponse
55
     */
56
    public function store(GroupRequest $request)
57
    {
58
        $group = Group::create($request->all());
59
        $group->trainings()->attach($request->input('trainings'));
60
        $group->users()->attach($request->input('users'));
61
62
        $this->assignTraining($group, $request->input('users'));
63
64
        return redirect()->action('GroupController@index');
65
    }
66
67
    public function edit($groupId)
68
    {
69
        $this->authorize('edit');
70
71
        $group = Group::findOrFail($groupId);
72
        $training = Training::orderBy('name')->get()->pluck('name', 'id')->toArray();
73
        $users = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('UserFullName', 'id')->toArray();
74
        $selectedTraining = $group->trainings()->pluck('id')->toArray();
75
        $selectedUsers = $group->users()->pluck('id')->toArray();
76
77
        return view('group.edit', compact('group', 'users', 'training', 'selectedTraining', 'selectedUsers'));
78
    }
79
80
    /**
81
     * @param GroupRequest $request
82
     * @param $groupId
83
     *
84
     * @return \Illuminate\Http\RedirectResponse
85
     */
86
    public function update(GroupRequest $request, $groupId)
87
    {
88
        $group = Group::findOrFail($groupId);
89
        $group->update($request->all());
90
91
        //If we don't get anything back, sync as an empty array.
92
        $group->users()->sync($request->input('users') ?: []);
93
        $group->trainings()->sync($request->input('trainings') ?: []);
94
95
        $this->assignTraining($group, $request->input('users'));
96
97
        return redirect()->action('GroupController@index');
98
    }
99
100
    /**
101
     * Remove the specified resource from storage.
102
     *
103
     * @param int $groupId
104
     *
105
     * @return \Illuminate\Http\Response
106
     */
107
    public function destroy($groupId)
108
    {
109
        $this->authorize('edit');
110
        Group::find($groupId)->delete();
111
    }
112
113
    /**
114
     * Get the users associated with a group.
115
     * Used for ajax/js calls so we can populate a user select field based on group select field.
116
     *
117
     * @return mixed
118
     */
119
    public function getUserIDs()
120
    {
121
        $request = Input::all();
122
        if (empty($request['groups'])) {
123
            return 'nothing sent.';
124
        }
125
126
        return User::whereHas('groups', function ($query) use ($request) {
127
            $query->whereIn('id', $request['groups']);
128
        })->pluck('id');
129
    }
130
131
    /**
132
     * Cycle through each user & determine what training is missing.
133
     * Then assign said training to be due in 1 month.
134
     *
135
     * Called via store & update methods above.
136
     *
137
     * @param $group - Collection
138
     * @param $users - array
139
     */
140
    public function assignTraining($group, $users)
141
    {
142
        //If users is empty, then we don't need to do any more.
143
        if ($users === null) {
144
            return;
145
        }
146
147
        //We need to take the group and get all the training records so we can assign them to users.
148
        $groupList = $group->trainings()->get();
149
        $data = [
150
            'author_id' => Auth::user()->id,
151
            'type'      => 'training',
152
            'due_date'  => Carbon::now()->AddWeeks(2)->format('Y-m-d'),
153
        ];
154
155
        // Cycle through each user & figure what training they are missing.
156
        foreach ($users as $userID) {
157
            $data['user_id'] = $userID;
158
            $userList = User::find($userID)->trainings()->get();
159
            $trainingList = $groupList->diff($userList)->pluck('id');
160
161
            // Assign the missing training to the user & send them an email.
162
            foreach ($trainingList as $trainingId) {
163
                $data['training_id'] = $trainingId;
164
                $trainingUser = TrainingUser::create($data);
165
166
                Event::fire(new TrainingAssigned($trainingUser));
167
            }
168
        }
169
    }
170
}
171