Passed
Push — feature/admin-only-job-create ( 4243e9...e9cbd9 )
by Tristan
13:05 queued 07:52
created

MenuComposer   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 72.58%

Importance

Changes 0
Metric Value
wmc 57
eloc 131
dl 0
loc 169
ccs 90
cts 124
cp 0.7258
rs 5.04
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F compose() 0 161 57

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 17
    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 17
        if (WhichPortal::isApplicantPortal()) {
22 12
            $menu = Lang::get('applicant/menu');
23
24
            //Set active on the proper item
25 12
            switch(Route::currentRouteName()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after SWITCH keyword; 0 found
Loading history...
26 12
                case 'home':
27 5
                    $menu['items']['home']['active'] = true;
28 5
                    break;
29 7
                case 'jobs.index':
30 7
                case 'jobs.show':
31 5
                case 'managers.show':
32 2
                    $menu['items']['jobs']['active'] = true;
33 2
                    break;
34 5
                case 'applications.index':
35 5
                case 'applications.edit':
36 5
                case 'applications.edit.1':
37 5
                case 'applications.edit.2':
38 5
                case 'applications.edit.3':
39 5
                case 'applications.edit.4':
40 5
                case 'applications.edit.5':
41 5
                case 'job.application.edit.1':
42 5
                case 'job.application.edit.2':
43 5
                case 'job.application.edit.3':
44 5
                case 'job.application.edit.4':
45 5
                case 'job.application.edit.5':
46 5
                case 'job.application.complete':
47
                    $menu['items']['applications']['active'] = true;
48
                    break;
49 5
                case 'profile':
50 5
                case 'profile.edit':
51 5
                case 'profile.show':
52 5
                case 'profile.about.edit':
53 5
                case 'profile.about.show':
54 5
                case 'profile.experience.edit':
55 5
                case 'profile.experience.show':
56 5
                case 'profile.references.edit':
57 3
                case 'profile.references.show':
58 3
                case 'profile.skills.edit':
59 3
                case 'profile.skills.show':
60 3
                case 'profile.work_samples.edit':
61 3
                case 'profile.work_samples.show':
62 2
                    $menu['items']['profile']['active'] = true;
63 2
                    break;
64 3
                case 'register':
65 1
                    $menu['items']['register']['active'] = true;
66 1
                    break;
67 2
                case 'login':
68 2
                    $menu['items']['login']['active'] = true;
69 2
                    break;
70
                case 'logout':
71
                    $menu['items']['logout']['active'] = true;
72
                    break;
73
                case 'faq':
74
                    $menu['items']['faq']['active'] = true;
75
                    break;
76
                default:
77
                    //No menu item will be active
78
                    break;
79
            }
80
81
            //Set route links
82 12
            $menu['items']['home']['link'] = route('home');
83 12
            $menu['items']['jobs']['link'] = route('jobs.index');
84 12
            $menu['items']['applications']['link'] = route('applications.index');
85 12
            $menu['items']['profile']['link'] = route('profile');
86 12
            $menu['items']['faq']['link'] = route('faq');
87
88
            //Check if use is logged in, and remove invalid menu items
89 12
            if (Auth::check()) {
90 6
                unset($menu['items']['login']);
91 6
                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 12
                unset($menu['items']['profile']);
97
            }
98 5
        } else if (WhichPortal::isManagerPortal()) {
99 5
            $menu = Lang::get('manager/menu');
100
101
            //Set active on the proper item
102 5
            switch(Route::currentRouteName()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after SWITCH keyword; 0 found
Loading history...
103 5
                case 'manager.home':
104
                    $menu['items']['home']['active'] = true;
105
                    break;
106 5
                case 'manager.jobs.index':
107 4
                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 3
                    $menu['items']['jobs']['active'] = true;
113 3
                    break;
114 2
                case 'manager.jobs.create':
115 2
                case 'manager.jobs.edit':
116
                case 'manager.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 5
            $menu['items']['home']['link'] = route('manager.home');
144 5
            $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 5
            $menu['items']['profile']['link'] = route('manager.profile');
148
149
            //Check if use is logged in, and remove invalid menu items
150 5
            if (Auth::check()) {
151 5
                unset($menu['items']['login']);
152 5
                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
        }
161
        //Set login modals data
162 17
        if (WhichPortal::isManagerPortal()) {
163
            $loginModals = [
164 5
                'modals' => Lang::get('common/login_modals'),
165 5
                'register_link' => route('manager.register'),
166 5
                'login_link' => route('manager.login'),
167 5
                'logout_link' => route('manager.logout'),
168
            ];
169
        } else {
170
            $loginModals = [
171 12
                'modals' => Lang::get('common/login_modals'),
172 12
                'register_link' => route('register'),
173 12
                'login_link' => route('login'),
174 12
                'logout_link' => route('logout'),
175
            ];
176
        }
177
178 17
        $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...
179 17
            ->with('login_modals', $loginModals);
180 17
    }
181
}
182