MenuComposer::compose()   F
last analyzed

Complexity

Conditions 81
Paths 780

Size

Total Lines 277
Code Lines 223

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 96
CRAP Score 162.9283

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 223
c 1
b 0
f 0
dl 0
loc 277
ccs 96
cts 125
cp 0.768
rs 0.2444
cc 81
nc 780
nop 1
crap 162.9283

How to fix   Long Method    Complexity   

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
namespace App\Http\ViewComposers;
4
5
use Illuminate\View\View;
6
use Illuminate\Support\Facades\Lang;
7
use Illuminate\Support\Facades\Route;
8
use Illuminate\Support\Facades\Auth;
9
use Facades\App\Services\WhichPortal;
10
11
class MenuComposer
12
{
13
    /**
14
     * Bind data to the view.
15
     *
16
     * @param  View  $view
17
     * @return void
18
     */
19 27
    public function compose(View $view)
20
    {
21 27
        if (WhichPortal::isApplicantPortal()) {
22 18
            $menu = Lang::get('applicant/menu');
23
24
            // Set active on the proper item.
25 18
            switch (Route::currentRouteName()) {
26 18
                case 'home':
27 6
                    $menu['items']['home']['active'] = true;
28 6
                    break;
29 12
                case 'jobs.index':
30 12
                case 'jobs.summary':
31 10
                case 'managers.show':
32 2
                    $menu['items']['jobs']['active'] = true;
33 2
                    break;
34 10
                case 'applications.index':
35 10
                case 'applications.edit':
36 10
                case 'applications.edit.1':
37 10
                case 'applications.edit.2':
38 10
                case 'applications.edit.3':
39 10
                case 'applications.edit.4':
40 10
                case 'applications.edit.5':
41 10
                case 'job.application.edit.1':
42 10
                case 'job.application.edit.2':
43 10
                case 'job.application.edit.3':
44 10
                case 'job.application.edit.4':
45 10
                case 'job.application.edit.5':
46 10
                case 'job.application.complete':
47
                    $menu['items']['applications']['active'] = true;
48
                    break;
49 10
                case 'profile':
50 10
                case 'profile.edit':
51 10
                case 'profile.show':
52 10
                case 'profile.about.edit':
53 10
                case 'profile.about.show':
54 10
                case 'profile.experience.edit':
55 10
                case 'profile.experience.show':
56 10
                case 'profile.references.edit':
57 8
                case 'profile.references.show':
58 8
                case 'profile.skills.edit':
59 8
                case 'profile.skills.show':
60 8
                case 'profile.skills-old.edit':
61 8
                case 'profile.skills-old.show':
62 2
                case 'profile.work_samples.edit':
63 2
                case 'profile.work_samples.show':
64 8
                    $menu['items']['profile']['active'] = true;
65 1
                    break;
66 1
                case 'register':
67 7
                    $menu['items']['register']['active'] = true;
68 2
                    break;
69 2
                case 'login':
70 5
                    $menu['items']['login']['active'] = true;
71
                    break;
72
                case 'logout':
73 5
                    $menu['items']['logout']['active'] = true;
74
                    break;
75
                case 'faq':
76
                    $menu['items']['faq']['active'] = true;
77
                    break;
78 5
                case 'itp':
79
                    $menu['items']['itp']['active'] = true;
80
                    break;
81
                case 'settings.edit':
82 18
                    $menu['items']['settings']['active'] = true;
83 18
                    break;
84 18
                default:
85 18
                    // No menu item will be active.
86 18
                    break;
87
            }
88
89 18
            // Set route links.
90 12
            $menu['items']['home']['link'] = route('home');
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            $menu['items']['home']['link'] = /** @scrutinizer ignore-call */ route('home');
Loading history...
91 12
            $menu['items']['jobs']['link'] = route('jobs.index');
92
            $menu['items']['applications']['link'] = route('applications.index');
93
            $menu['items']['profile']['link'] = route('profile');
94 8
            $menu['items']['faq']['link'] = route('faq');
95 8
            $menu['items']['itp']['link'] = route('itp');
96 18
            $menu['items']['settings']['link'] = route('settings.edit');
97
98 9
            // Check if use is logged in, and remove invalid menu items.
99 9
            if (Auth::check()) {
100
                unset($menu['items']['login']);
101
                unset($menu['items']['register']);
102 9
                // TODO set profile like using user slug.
103 9
            } else {
104 1
                unset($menu['items']['logout']);
105 1
                unset($menu['items']['applications']);
106 8
                unset($menu['items']['profile']);
107 7
                unset($menu['items']['settings']);
108 3
            }
109 3
        } elseif (WhichPortal::isManagerPortal()) {
110 2
            $menu = Lang::get('manager/menu');
111 2
112 6
            // Set active on the proper item.
113 6
            switch (Route::currentRouteName()) {
114 2
                case 'manager.home':
115 2
                    $menu['items']['home']['active'] = true;
116
                    break;
117
                case 'manager.jobs.index':
118 2
                case 'manager.jobs.preview':
119 2
                case 'manager.jobs.applications':
120
                case 'manager.applications.show':
121
                case 'manager.applicants.show':
122
                    $menu['items']['jobs']['active'] = true;
123
                    break;
124
                case 'manager.jobs.create':
125
                case 'manager.jobs.edit':
126
                case 'manager.jobs.review':
127
                case 'admin.jobs.update':
128
                    $menu['items']['jobs']['active'] = true;
129
                    break;
130
                case 'manager.profile':
131
                case 'manager.profile.edit':
132
                case 'manager.profile.show':
133
                    $menu['items']['profile']['active'] = true;
134
                    break;
135
                case 'register':
136
                    $menu['items']['register']['active'] = true;
137
                    break;
138
                case 'login':
139
                    $menu['items']['login']['active'] = true;
140
                    break;
141
                case 'logout':
142
                    $menu['items']['logout']['active'] = true;
143 9
                    break;
144 9
                case 'manager.faq':
145
                    $menu['items']['faq']['active'] = true;
146
                    break;
147 9
                case 'manager.settings.edit':
148
                    $menu['items']['settings']['active'] = true;
149
                    break;
150 9
                case 'manager.resources':
151 9
                    $menu['items']['resources']['active'] = true;
152 9
                    break;
153
                default:
154
                    // No menu item will be active.
155
                    break;
156
            }
157
158
            // Set route links.
159
            $menu['items']['home']['link'] = route('manager.home');
160
            $menu['items']['jobs']['link'] = route('manager.jobs.index');
161
            $menu['items']['profile']['link'] = route('manager.profile');
162 27
            $menu['items']['faq']['link'] = route('manager.faq.section');
163
            $menu['items']['settings']['link'] = route('manager.settings.edit');
164 9
            $menu['items']['resources']['link'] = route('manager.resources');
165 9
166 9
            // Check if use is logged in, and remove invalid menu items.
167 9
            if (Auth::check()) {
168
                unset($menu['items']['login']);
169
                unset($menu['items']['register']);
170
                // TODO set profile like using user slug.
171 18
            } else {
172 18
                unset($menu['items']['logout']);
173 18
                unset($menu['items']['jobs']);
174 18
                unset($menu['items']['create_job']);
175
                unset($menu['items']['profile']);
176
                unset($menu['items']['resources']);
177
                unset($menu['items']['settings']);
178 27
            }
179 27
180 27
            if (Auth::user() && !Auth::user()->isUpgradedManager()) {
181
                unset($menu['items']['resources']);
182
            }
183
        } elseif (WhichPortal::isHrPortal()) {
184
            $menu = Lang::get('hr_advisor/menu');
185
186
            // Set active on the proper item
187
            switch (Route::currentRouteName()) {
188
                case 'hr_advisor.home':
189
                    $menu['items']['home']['active'] = true;
190
                    break;
191
                case 'hr_advisor.jobs.index':
192
                    $menu['items']['jobs']['active'] = true;
193
                    break;
194
                case 'hr_advisor.settings.edit':
195
                    $menu['items']['settings']['active'] = true;
196
                    break;
197
                case 'hr_advisor.resources':
198
                    $menu['items']['resources']['active'] = true;
199
                    break;
200
                case 'register':
201
                    $menu['items']['register']['active'] = true;
202
                    break;
203
                case 'login':
204
                    $menu['items']['login']['active'] = true;
205
                    break;
206
                case 'logout':
207
                    $menu['items']['logout']['active'] = true;
208
                    break;
209
                default:
210
                    // No menu item will be active
211
                    break;
212
            }
213
214
            // Set route links
215
            $menu['items']['home']['link'] = route('hr_advisor.home');
216
            $menu['items']['jobs']['link'] = route('hr_advisor.jobs.index');
217
            $menu['items']['settings']['link'] = route('hr_advisor.settings.edit');
218
            $menu['items']['resources']['link'] = route('hr_advisor.resources');
219
220
            // Check if use is logged in, and remove invalid menu items
221
            if (Auth::check()) {
222
                unset($menu['items']['login']);
223
                unset($menu['items']['register']);
224
            } else {
225
                unset($menu['items']['logout']);
226
                unset($menu['items']['jobs']);
227
                unset($menu['items']['settings']);
228
                unset($menu['items']['resources']);
229
            }
230
231
            if (Auth::user() && !Auth::user()->isHrAdvisor()) {
232
                unset($menu['items']['resources']);
233
            }
234
        } elseif (WhichPortal::isAdminPortal()) {
235
            // Use the manager menu, keeping only.
236
            $menu = Lang::get('admin/menu');
237
238
            // Set active on the proper item.
239
            switch (Route::currentRouteName()) {
240
                case 'admin.home':
241
                    $menu['items']['home']['active'] = true;
242
                    break;
243
                default:
244
                    // No menu item will be active.
245
                    break;
246
            }
247
248
            // Set route links.
249
            $menu['items']['home']['link'] = backpack_url();
0 ignored issues
show
Bug introduced by
The function backpack_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

249
            $menu['items']['home']['link'] = /** @scrutinizer ignore-call */ backpack_url();
Loading history...
250
251
            // Check if use is logged in, and remove invalid menu items.
252
            if (Auth::check()) {
253
                unset($menu['items']['login']);
254
                unset($menu['items']['register']);
255
                // TODO: set profile link using user slug.
256
            } else {
257
                unset($menu['items']['logout']);
258
                unset($menu['items']['settings']);
259
            }
260
        }
261
        // Set login modals data.
262
        if (WhichPortal::isManagerPortal()) {
263
            $loginModals = [
264
                'modals' => Lang::get('common/login_modals'),
265
                'register_link' => route('manager.register'),
266
                'login_link' => route('manager.login'),
267
                'logout_link' => route('manager.logout'),
268
                'settings_link' => route('manager.settings.edit'),
269
            ];
270
        } elseif (WhichPortal::isHrPortal()) {
271
            $loginModals = [
272
                'modals' => Lang::get('common/login_modals'),
273
                'register_link' => route('hr_advisor.register'),
274
                'login_link' => route('hr_advisor.login'),
275
                'logout_link' => route('hr_advisor.logout'),
276
            ];
277
        } elseif (WhichPortal::isAdminPortal()) {
278
            $loginModals = [
279
                'modals' => Lang::get('common/login_modals'),
280
                'register_link' => route('register'),
281
                'login_link' => backpack_url('login'),
282
                'logout_link' => backpack_url('logout'),
283
            ];
284
        } else {
285
            $loginModals = [
286
                'modals' => Lang::get('common/login_modals'),
287
                'register_link' => route('register'),
288
                'login_link' => route('login'),
289
                'logout_link' => route('logout'),
290
                'settings_link' => route('settings.edit'),
291
            ];
292
        }
293
294
        $view->with('menu', $menu)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $menu does not seem to be defined for all execution paths leading up to this point.
Loading history...
295
            ->with('login_modals', $loginModals);
296
    }
297
}
298