Completed
Pull Request — dev (#330)
by Josh
06:54
created

MenuComposer   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 45.71%

Importance

Changes 0
Metric Value
wmc 50
eloc 112
dl 0
loc 142
ccs 48
cts 105
cp 0.4571
rs 8.4
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F compose() 0 134 50

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 2
    public function compose(View $view)
20
    {
21 2
        if (WhichPortal::isApplicantPortal()) {
22 2
            $menu = Lang::get('applicant/menu');
23
24
            //Set active on the proper item
25 2
            switch(Route::currentRouteName()) {
26 2
                case 'home':
27
                    $menu['items']['home']['active'] = true;
28
                    break;
29 2
                case 'jobs.index':
30 2
                case 'jobs.show':
31 2
                case 'managers.show':
32
                    $menu['items']['jobs']['active'] = true;
33
                    break;
34 2
                case 'applications.index':
35 2
                case 'applications.edit':
36 2
                case 'applications.edit.1':
37 2
                case 'applications.edit.2':
38 2
                case 'applications.edit.3':
39 2
                case 'applications.edit.4':
40 2
                case 'applications.edit.5':
41 2
                case 'job.application.edit.1':
42 2
                case 'job.application.edit.2':
43 2
                case 'job.application.edit.3':
44 2
                case 'job.application.edit.4':
45 2
                case 'job.application.edit.5':
46
                    $menu['items']['applications']['active'] = true;
47
                    break;
48 2
                case 'profile':
49 2
                case 'profile.edit':
50 2
                case 'profile.show':
51 2
                case 'profile.about.edit':
52 2
                case 'profile.about.show':
53 2
                case 'profile.experience.edit':
54 2
                case 'profile.experience.show':
55 2
                case 'profile.references.edit':
56 2
                case 'profile.references.show':
57 2
                case 'profile.skills.edit':
58 2
                case 'profile.skills.show':
59 2
                case 'profile.work_samples.edit':
60 2
                case 'profile.work_samples.show':
61
                    $menu['items']['profile']['active'] = true;
62
                    break;
63 2
                case 'register':
64
                    $menu['items']['register']['active'] = true;
65
                    break;
66 2
                case 'login':
67
                    $menu['items']['login']['active'] = true;
68
                    break;
69 2
                case 'logout':
70
                    $menu['items']['logout']['active'] = true;
71
                    break;
72
                default:
73
                    //No menu item will be active
74 2
                    break;
75
            }
76
77
            //Check if use is logged in, and remove invalid menu items
78 2
            if (Auth::check()) {
79
                unset($menu['items']['login']);
80
                unset($menu['items']['register']);
81
                //TODO set profile like using user slug
82
            } else {
83 2
                unset($menu['items']['logout']);
84 2
                unset($menu['items']['applications']);
85 2
                unset($menu['items']['profile']);
86
            }
87
        } else if (WhichPortal::isManagerPortal()) {
88
            $menu = Lang::get('manager/menu');
89
90
            //Set active on the proper item
91
            switch(Route::currentRouteName()) {
92
                case 'manager.home':
93
                    $menu['items']['home']['active'] = true;
94
                    break;
95
                case 'manager.jobs.index':
96
                case 'manager.jobs.show':
97
                    $menu['items']['jobs']['active'] = true;
98
                    break;
99
                case 'manager.jobs.create':
100
                case 'manager.jobs.edit':
101
                case 'manager.jobs.update':
102
                    $menu['items']['create_job']['active'] = true;
103
                    break;
104
                case 'manager.profile':
105
                case 'manager.profile.edit':
106
                case 'manager.profile.show':
107
                    $menu['items']['profile']['active'] = true;
108
                    break;
109
                case 'register':
110
                    $menu['items']['register']['active'] = true;
111
                    break;
112
                case 'login':
113
                    $menu['items']['login']['active'] = true;
114
                    break;
115
                case 'logout':
116
                    $menu['items']['logout']['active'] = true;
117
                    break;
118
                default:
119
                    //No menu item will be active
120
                    break;
121
            }
122
123
            //Check if use is logged in, and remove invalid menu items
124
            if (Auth::check()) {
125
                unset($menu['items']['login']);
126
                unset($menu['items']['register']);
127
                //TODO set profile like using user slug
128
            } else {
129
                unset($menu['items']['logout']);
130
                unset($menu['items']['jobs']);
131
                unset($menu['items']['create_job']);
132
                unset($menu['items']['profile']);
133
            }
134
        }
135 2
        if (WhichPortal::isManagerPortal()) {
136
            $loginModals = [
137
                'modals' => Lang::get('common/login_modals'),
138
                'register_link' => route('manager.register'),
139
                'login_link' => route('manager.login'),
140
                'logout_link' => route('manager.logout'),
141
            ];
142
        } else {
143
            $loginModals = [
144 2
                'modals' => Lang::get('common/login_modals'),
145 2
                'register_link' => route('register'),
146 2
                'login_link' => route('login'),
147 2
                'logout_link' => route('logout'),
148
            ];
149
        }
150
151 2
        $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...
152 2
            ->with('login_modals', $loginModals);
153 2
    }
154
}
155