Total Complexity | 63 |
Total Lines | 252 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GraphController 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 GraphController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class GraphController extends WidgetController |
||
42 | { |
||
43 | protected $title = 'Graph'; |
||
44 | protected $defaults = [ |
||
45 | 'title' => null, |
||
46 | 'refresh' => 60, |
||
47 | 'graph_type' => null, |
||
48 | 'graph_range' => 'oneday', |
||
49 | 'graph_legend' => 'yes', |
||
50 | 'graph_device' => null, |
||
51 | 'graph_port' => null, |
||
52 | 'graph_application' => null, |
||
53 | 'graph_munin' => null, |
||
54 | 'graph_service' => null, |
||
55 | 'graph_ports' => [], |
||
56 | 'graph_custom' => [], |
||
57 | 'graph_manual' => null, |
||
58 | 'graph_bill' => null, |
||
59 | ]; |
||
60 | |||
61 | public function title() |
||
62 | { |
||
63 | $settings = $this->getSettings(); |
||
64 | |||
65 | if (! empty($settings['title'])) { |
||
66 | return $settings['title']; |
||
67 | } |
||
68 | |||
69 | // automatic title |
||
70 | $type = $this->getGraphType(); |
||
71 | if ($type == 'device') { |
||
72 | $device = Device::find($settings['graph_device']); |
||
73 | |||
74 | return ($device ? $device->displayName() : 'Device') . ' / ' . $settings['graph_type']; |
||
75 | } elseif ($type == 'aggregate') { |
||
76 | return 'Overall ' . $this->getGraphType(false) . ' Bits (' . $settings['graph_range'] . ')'; |
||
77 | } elseif ($type == 'port') { |
||
78 | if ($port = Port::find($settings['graph_port'])) { |
||
79 | return $port->device->displayName() . ' / ' . $port->getShortLabel() . ' / ' . $settings['graph_type']; |
||
|
|||
80 | } |
||
81 | } elseif ($type == 'application') { |
||
82 | if ($application = Application::find($settings['graph_application'])) { |
||
83 | return $application->device->displayName() . ' / ' . $application->app_type . ' / ' . $settings['graph_type']; |
||
84 | } |
||
85 | } elseif ($type == 'bill') { |
||
86 | if ($bill = Bill::find($settings['graph_bill'])) { |
||
87 | return 'Bill: ' . $bill->bill_name; |
||
88 | } |
||
89 | } elseif ($type == 'munin') { |
||
90 | if ($munin = MuninPlugin::find($settings['graph_munin'])) { |
||
91 | return $munin->device->displayName() . ' / ' . $munin->mplug_type . ' / ' . $settings['graph_type']; |
||
92 | } |
||
93 | } elseif ($type == 'service') { |
||
94 | if ($service = Service::find($settings['graph_service'])) { |
||
95 | return $service->device->displayName() . ' / ' . $service->service_type . ' (' . $service->service_desc . ')' . ' / ' . $settings['graph_type']; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // fall back for types where we couldn't find the item |
||
100 | if ($settings['graph_type']) { |
||
101 | return 'Device / ' . ucfirst($type) . ' / ' . $settings['graph_type']; |
||
102 | } |
||
103 | |||
104 | return $this->title; |
||
105 | } |
||
106 | |||
107 | public function getSettingsView(Request $request) |
||
108 | { |
||
109 | $data = $this->getSettings(true); |
||
110 | |||
111 | // format display name for selected graph type |
||
112 | $type_parts = explode('_', $data['graph_type']); |
||
113 | $primary = array_shift($type_parts); |
||
114 | $secondary = implode('_', $type_parts); |
||
115 | $name = $primary . ' ' . (Graph::isMibGraph($primary, $secondary) ? $secondary : implode(' ', $type_parts)); |
||
116 | |||
117 | // format display for selected items |
||
118 | if ($primary == 'device' && $data['graph_device']) { |
||
119 | $device = Device::find($data['graph_device']); |
||
120 | } |
||
121 | $data['device_text'] = isset($device) ? $device->displayName() : __('Device does not exist'); |
||
122 | |||
123 | if ($primary == 'port' && $data['graph_port']) { |
||
124 | $port = Port::find($data['graph_port']); |
||
125 | } |
||
126 | $data['port_text'] = isset($port) ? $port->getLabel() : __('Port does not exist'); |
||
127 | |||
128 | if ($primary == 'application' && $data['graph_application']) { |
||
129 | $app = Application::find($data['graph_application']); |
||
130 | } |
||
131 | $data['application_text'] = isset($app) ? $app->displayName() . ' - ' . $app->device->displayName() : __('App does not exist'); |
||
132 | |||
133 | if ($primary == 'bill' && $data['graph_bill']) { |
||
134 | $bill = Bill::find($data['graph_bill']); |
||
135 | } |
||
136 | $data['bill_text'] = isset($bill) ? $bill->bill_name : __('Bill does not exist'); |
||
137 | |||
138 | if ($primary == 'munin' && $data['graph_munin']) { |
||
139 | $mplug = MuninPlugin::with('device')->find($data['graph_munin']); |
||
140 | } |
||
141 | $data['munin_text'] = isset($mplug) ? $mplug->device->displayName() . ' - ' . $mplug->mplug_type : __('Munin plugin does not exist'); |
||
142 | |||
143 | if ($primary == 'service' && $data['graph_service']) { |
||
144 | $service = Service::with('device')->find($data['graph_service']); |
||
145 | } |
||
146 | $data['service_text'] = isset($service) ? $service->device->displayName() . ' - ' . $service->service_type . ' (' . $service->service_desc . ')' : __('Service does not exist'); |
||
147 | |||
148 | $data['graph_ports'] = Port::whereIn('port_id', $data['graph_ports']) |
||
149 | ->select('ports.device_id', 'port_id', 'ifAlias', 'ifName', 'ifDescr') |
||
150 | ->with(['device' => function ($query) { |
||
151 | $query->select('device_id', 'hostname', 'sysName'); |
||
152 | }])->get(); |
||
153 | |||
154 | $data['graph_port_ids'] = $data['graph_ports']->pluck('port_id')->toJson(); |
||
155 | |||
156 | $data['graph_text'] = ucwords($name); |
||
157 | |||
158 | return view('widgets.settings.graph', $data); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param Request $request |
||
163 | * @return View |
||
164 | */ |
||
165 | public function getView(Request $request) |
||
230 | } |
||
231 | |||
232 | private function getGraphType($summarize = true) |
||
241 | } |
||
242 | |||
243 | private function hasInvalidSettings() |
||
251 | } |
||
252 | |||
253 | public function getSettings($settingsView = false) |
||
254 | { |
||
255 | if (is_null($this->settings)) { |
||
256 | $settings = parent::getSettings($settingsView); |
||
257 | |||
258 | // legacy data conversions |
||
259 | if ($settings['graph_type'] == 'manual') { |
||
260 | $settings['graph_type'] = 'custom'; |
||
261 | $settings['graph_custom'] = explode(',', $settings['graph_manual']); |
||
262 | } |
||
263 | if ($settings['graph_type'] == 'transpeer') { |
||
264 | $settings['graph_type'] = 'custom'; |
||
265 | $settings['graph_custom'] = ['transit', 'peer']; |
||
266 | } |
||
267 | |||
268 | $settings['graph_device'] = $this->convertLegacySettingId($settings['graph_device'], 'device_id'); |
||
269 | $settings['graph_port'] = $this->convertLegacySettingId($settings['graph_port'], 'port_id'); |
||
270 | $settings['graph_application'] = $this->convertLegacySettingId($settings['graph_application'], 'app_id'); |
||
271 | $settings['graph_munin'] = $this->convertLegacySettingId($settings['graph_munin'], 'mplug_id'); |
||
272 | $settings['graph_service'] = $this->convertLegacySettingId($settings['graph_service'], 'service_id'); |
||
273 | $settings['graph_bill'] = $this->convertLegacySettingId($settings['graph_bill'], 'bill_id'); |
||
274 | |||
275 | $settings['graph_custom'] = (array) $settings['graph_custom']; |
||
276 | $settings['graph_ports'] = (array) $settings['graph_ports']; |
||
277 | |||
278 | $this->settings = $settings; |
||
279 | } |
||
280 | |||
281 | return $this->settings; |
||
282 | } |
||
283 | |||
284 | private function convertLegacySettingId($setting, $key) |
||
293 | } |
||
294 | } |
||
295 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.