Passed
Push — feature/closed_apply_ui ( 148195...2419c6 )
by Tristan
09:14
created

MenuComposer   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 42.98%

Importance

Changes 0
Metric Value
wmc 53
eloc 128
dl 0
loc 163
rs 6.96
c 0
b 0
f 0
ccs 52
cts 121
cp 0.4298

1 Method

Rating   Name   Duplication   Size   Complexity  
F compose() 0 155 53

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
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class MenuComposer
Loading history...
12
{
13
    /**
14
     * Bind data to the view.
15
     *
16
     * @param  View  $view
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
17
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
18
     */
19 16
    public function compose(View $view)
20
    {
21 16
        if (WhichPortal::isApplicantPortal()) {
22 8
            $menu = Lang::get('applicant/menu');
23
24
            //Set active on the proper item
25 8
            switch(Route::currentRouteName()) {
26 8
                case 'home':
27 4
                    $menu['items']['home']['active'] = true;
28 4
                    break;
29 4
                case 'jobs.index':
30 4
                case 'jobs.show':
31
                case 'managers.show':
32 4
                    $menu['items']['jobs']['active'] = true;
33 4
                    break;
34
                case 'applications.index':
35
                case 'applications.edit':
36
                case 'applications.edit.1':
37
                case 'applications.edit.2':
38
                case 'applications.edit.3':
39
                case 'applications.edit.4':
40
                case 'applications.edit.5':
41
                case 'job.application.edit.1':
42
                case 'job.application.edit.2':
43
                case 'job.application.edit.3':
44
                case 'job.application.edit.4':
45
                case 'job.application.edit.5':
46
                case 'job.application.complete':
47
                    $menu['items']['applications']['active'] = true;
48
                    break;
49
                case 'profile':
50
                case 'profile.edit':
51
                case 'profile.show':
52
                case 'profile.about.edit':
53
                case 'profile.about.show':
54
                case 'profile.experience.edit':
55
                case 'profile.experience.show':
56
                case 'profile.references.edit':
57
                case 'profile.references.show':
58
                case 'profile.skills.edit':
59
                case 'profile.skills.show':
60
                case 'profile.work_samples.edit':
61
                case 'profile.work_samples.show':
62
                    $menu['items']['profile']['active'] = true;
63
                    break;
64
                case 'register':
65
                    $menu['items']['register']['active'] = true;
66
                    break;
67
                case 'login':
68
                    $menu['items']['login']['active'] = true;
69
                    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 8
            $menu['items']['home']['link'] = route('home');
83 8
            $menu['items']['jobs']['link'] = route('jobs.index');
84 8
            $menu['items']['applications']['link'] = route('applications.index');
85 8
            $menu['items']['profile']['link'] = route('profile');
86 8
            $menu['items']['faq']['link'] = route('faq');
87
88
            //Check if use is logged in, and remove invalid menu items
89 8
            if (Auth::check()) {
90 4
                unset($menu['items']['login']);
91 4
                unset($menu['items']['register']);
92
                //TODO set profile like using user slug
93
            } else {
94 4
                unset($menu['items']['logout']);
95 4
                unset($menu['items']['applications']);
96 8
                unset($menu['items']['profile']);
97
            }
98 8
        } else if (WhichPortal::isManagerPortal()) {
99 8
            $menu = Lang::get('manager/menu');
100
101
            //Set active on the proper item
102 8
            switch(Route::currentRouteName()) {
103 8
                case 'manager.home':
104
                    $menu['items']['home']['active'] = true;
105
                    break;
106 8
                case 'manager.jobs.index':
107 6
                case 'manager.jobs.show':
108 4
                    $menu['items']['jobs']['active'] = true;
109 4
                    break;
110 4
                case 'manager.jobs.create':
111 2
                case 'manager.jobs.edit':
112
                case 'manager.jobs.update':
113 4
                    $menu['items']['create_job']['active'] = true;
114 4
                    break;
115
                case 'manager.profile':
116
                case 'manager.profile.edit':
117
                case 'manager.profile.show':
118
                    $menu['items']['profile']['active'] = true;
119
                    break;
120
                case 'register':
121
                    $menu['items']['register']['active'] = true;
122
                    break;
123
                case 'login':
124
                    $menu['items']['login']['active'] = true;
125
                    break;
126
                case 'logout':
127
                    $menu['items']['logout']['active'] = true;
128
                    break;
129
                case 'faq':
130
                    $menu['items']['faq']['active'] = true;
131
                    break;
132
                default:
133
                    //No menu item will be active
134
                    break;
135
            }
136
137
            //Set route links
138 8
            $menu['items']['home']['link'] = route('manager.home');
139 8
            $menu['items']['jobs']['link'] = route('manager.jobs.index');
140 8
            $menu['items']['create_job']['link'] = route('manager.jobs.create');
141 8
            $menu['items']['profile']['link'] = route('manager.profile');
142
143
            //Check if use is logged in, and remove invalid menu items
144 8
            if (Auth::check()) {
145 8
                unset($menu['items']['login']);
146 8
                unset($menu['items']['register']);
147
                //TODO set profile like using user slug
148
            } else {
149
                unset($menu['items']['logout']);
150
                unset($menu['items']['jobs']);
151
                unset($menu['items']['create_job']);
152
                unset($menu['items']['profile']);
153
            }
154
        }
155
        //Set login modals data
156 16
        if (WhichPortal::isManagerPortal()) {
157
            $loginModals = [
158 8
                'modals' => Lang::get('common/login_modals'),
159 8
                'register_link' => route('manager.register'),
160 8
                'login_link' => route('manager.login'),
161 8
                'logout_link' => route('manager.logout'),
162
            ];
163
        } else {
164
            $loginModals = [
165 8
                'modals' => Lang::get('common/login_modals'),
166 8
                'register_link' => route('register'),
167 8
                'login_link' => route('login'),
168 8
                'logout_link' => route('logout'),
169
            ];
170
        }
171
172 16
        $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...
173 16
            ->with('login_modals', $loginModals);
174 16
    }
175
}
176