Passed
Push — dev ( 39070a...d6bbaa )
by Chris
08:00
created

MenuComposer   F

Complexity

Total Complexity 61

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 76.8%

Importance

Changes 0
Metric Value
wmc 61
eloc 151
dl 0
loc 202
ccs 96
cts 125
cp 0.768
rs 3.52
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F compose() 0 194 61

How to fix   Complexity   

Complex Class

Complex classes like MenuComposer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MenuComposer, and based on these observations, apply Extract Interface, too.

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
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
17
     * @return void
18
     */
19 27
    public function compose(View $view)
0 ignored issues
show
introduced by
Method \App\Http\ViewComposers\MenuComposer::compose() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
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.show':
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.work_samples.edit':
61 8
                case 'profile.work_samples.show':
62 2
                    $menu['items']['profile']['active'] = true;
63 2
                    break;
64 8
                case 'register':
65 1
                    $menu['items']['register']['active'] = true;
66 1
                    break;
67 7
                case 'login':
68 2
                    $menu['items']['login']['active'] = true;
69 2
                    break;
70 5
                case 'logout':
71
                    $menu['items']['logout']['active'] = true;
72
                    break;
73 5
                case 'faq':
74
                    $menu['items']['faq']['active'] = true;
75
                    break;
76
                default:
77
                    // No menu item will be active
78 5
                    break;
79
            }
80
81
            // Set route links
82 18
            $menu['items']['home']['link'] = route('home');
83 18
            $menu['items']['jobs']['link'] = route('jobs.index');
84 18
            $menu['items']['applications']['link'] = route('applications.index');
85 18
            $menu['items']['profile']['link'] = route('profile');
86 18
            $menu['items']['faq']['link'] = route('faq');
87
88
            // Check if use is logged in, and remove invalid menu items
89 18
            if (Auth::check()) {
90 12
                unset($menu['items']['login']);
91 12
                unset($menu['items']['register']);
92
                // TODO set profile like using user slug
93
            } else {
94 8
                unset($menu['items']['logout']);
95 8
                unset($menu['items']['applications']);
96 18
                unset($menu['items']['profile']);
97
            }
98 9
        } elseif (WhichPortal::isManagerPortal()) {
99 9
            $menu = Lang::get('manager/menu');
100
101
            // Set active on the proper item
102 9
            switch (Route::currentRouteName()) {
103 9
                case 'manager.home':
104 1
                    $menu['items']['home']['active'] = true;
105 1
                    break;
106 8
                case 'manager.jobs.index':
107 7
                case 'manager.jobs.show':
108 3
                case 'manager.jobs.applications':
109 3
                case 'manager.jobs.review':
110 2
                case 'manager.applications.show':
111 2
                case 'manager.applicants.show':
112 6
                    $menu['items']['jobs']['active'] = true;
113 6
                    break;
114 2
                case 'manager.jobs.create':
115 2
                case 'manager.jobs.edit':
116
                case 'admin.jobs.update':
117
                    // $menu['items']['create_job']['active'] = true;
118 2
                    $menu['items']['jobs']['active'] = true; // TODO: restore when job poster builder complete
119 2
                    break;
120
                case 'manager.profile':
121
                case 'manager.profile.edit':
122
                case 'manager.profile.show':
123
                    $menu['items']['profile']['active'] = true;
124
                    break;
125
                case 'register':
126
                    $menu['items']['register']['active'] = true;
127
                    break;
128
                case 'login':
129
                    $menu['items']['login']['active'] = true;
130
                    break;
131
                case 'logout':
132
                    $menu['items']['logout']['active'] = true;
133
                    break;
134
                case 'faq':
135
                    $menu['items']['faq']['active'] = true;
136
                    break;
137
                default:
138
                    // No menu item will be active
139
                    break;
140
            }
141
142
            // Set route links
143 9
            $menu['items']['home']['link'] = route('manager.home');
144 9
            $menu['items']['jobs']['link'] = route('manager.jobs.index');
145
            // TODO: restore when job poster builder complete
146
            // $menu['items']['create_job']['link'] = route('manager.jobs.create');
147 9
            $menu['items']['profile']['link'] = route('manager.profile');
148
149
            // Check if use is logged in, and remove invalid menu items
150 9
            if (Auth::check()) {
151 9
                unset($menu['items']['login']);
152 9
                unset($menu['items']['register']);
153
                // TODO set profile like using user slug
154
            } else {
155
                unset($menu['items']['logout']);
156
                unset($menu['items']['jobs']);
157
                unset($menu['items']['create_job']);
158
                unset($menu['items']['profile']);
159
            }
160
        } elseif (WhichPortal::isAdminPortal()) {
161
            // Use the manager menu, keeping only
162 27
            $menu = Lang::get('admin/menu');
163
164 9
165 9
            // Set active on the proper item
166 9
            switch (Route::currentRouteName()) {
167 9
                case 'admin.home':
168
                    $menu['items']['home']['active'] = true;
169
                    break;
170
                default:
171 18
                    // No menu item will be active
172 18
                    break;
173 18
            }
174 18
175
            // Set route links
176
            $menu['items']['home']['link'] = backpack_url();
177
178 27
            // Check if use is logged in, and remove invalid menu items
179 27
            if (Auth::check()) {
180 27
                unset($menu['items']['login']);
181
                unset($menu['items']['register']);
182
                // TODO set profile like using user slug
183
            } else {
184
                unset($menu['items']['logout']);
185
            }
186
        }
187
        // Set login modals data
188
        if (WhichPortal::isManagerPortal()) {
189
            $loginModals = [
190
                'modals' => Lang::get('common/login_modals'),
191
                'register_link' => route('manager.register'),
192
                'login_link' => route('manager.login'),
193
                'logout_link' => route('manager.logout'),
194
            ];
195
        } elseif (WhichPortal::isAdminPortal()) {
196
             $loginModals = [
197
                'modals' => Lang::get('common/login_modals'),
198
                'register_link' => route('register'),
199
                'login_link' => backpack_url('login'),
200
                'logout_link' => backpack_url('logout'),
201
             ];
202
        } else {
203
            $loginModals = [
204
                'modals' => Lang::get('common/login_modals'),
205
                'register_link' => route('register'),
206
                'login_link' => route('login'),
207
                'logout_link' => route('logout'),
208
            ];
209
        }
210
211
        $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...
212
            ->with('login_modals', $loginModals);
213
    }
214
}
215