Completed
Pull Request — dev (#321)
by Tristan
06:34
created

MenuComposer   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 45
eloc 107
dl 0
loc 137
ccs 0
cts 100
cp 0
rs 8.8
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F compose() 0 129 45

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