Conditions | 45 |
Paths | 78 |
Total Lines | 238 |
Code Lines | 167 |
Lines | 12 |
Ratio | 5.04 % |
Changes | 10 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
41 | public function widget_navigation($widget = []) { |
||
42 | $this->load->module('core'); |
||
43 | |||
44 | $segmentGallery = $this->pathGallery(); |
||
45 | if ($this->core->core_data['data_type'] === '404' || !$this->core->core_data['data_type'] || $segmentGallery === 'gallery') { |
||
46 | $data_type = $segmentGallery; |
||
47 | } else { |
||
48 | $data_type = $this->core->core_data['data_type']; |
||
49 | } |
||
50 | |||
51 | switch ($data_type) { |
||
52 | case 'category': |
||
53 | $cur_category = $this->core->cat_content; |
||
54 | $path_categories = $this->lib_category->get_category(array_keys($cur_category['path'])); |
||
55 | |||
56 | $tpl_data = ['navi_cats' => $path_categories]; |
||
57 | |||
58 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
59 | |||
60 | case 'page': |
||
61 | $cur_category = $this->core->cat_content; |
||
62 | $path_categories = $this->lib_category->get_category(array_keys($cur_category['path'])); |
||
63 | |||
64 | // Insert Page data |
||
65 | $path_categories[] = [ |
||
66 | 'path_url' => $this->core->page_content['cat_url'] . $this->core->page_content['url'], |
||
67 | 'name' => $this->core->page_content['title'], |
||
68 | ]; |
||
69 | |||
70 | $tpl_data = ['navi_cats' => $path_categories]; |
||
71 | |||
72 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
73 | |||
74 | case 'brand': |
||
75 | if ($this->core->core_data['id'] != null) { |
||
76 | $brand = SBrandsQuery::create()->joinWithI18n(MY_Controller::getCurrentLocale())->findOneById($this->core->core_data['id']); |
||
77 | |||
78 | $navi_cats[] = [ |
||
79 | 'path_url' => 'shop/brand/', |
||
80 | 'name' => lang('Brands', 'navigation'), |
||
81 | ]; |
||
82 | $navi_cats[] = [ |
||
83 | 'path_url' => 'shop/brand/' . $brand->getUrl(), |
||
84 | 'name' => $brand->getName(), |
||
85 | ]; |
||
86 | |||
87 | $tpl_data = ['navi_cats' => $navi_cats]; |
||
88 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
89 | } else { |
||
90 | |||
91 | if ($data_type == 'brand') { |
||
92 | return $this->make(lang('Brands', 'navigation'), 'shop/brand/', $widget); |
||
93 | } |
||
94 | } |
||
95 | break; |
||
96 | case 'compare'; |
||
97 | return $this->make(lang('Compare', 'navigation'), 'shop/compare/', $widget); |
||
98 | case 'order'; |
||
99 | return $this->make(lang('Order details', 'navigation'), 'shop/order/', $widget); |
||
100 | case 'wish_list': |
||
101 | return $this->make(lang('Wish list', 'navigation'), 'shop/wish_list/', $widget); |
||
102 | |||
103 | case 'profile': |
||
104 | return $this->make(lang('Profile', 'navigation'), 'shop/profile/', $widget); |
||
105 | |||
106 | case 'search': |
||
107 | return $this->make(lang('Search', 'navigation'), 'shop/search/', $widget); |
||
108 | |||
109 | case 'callbacks': |
||
110 | return $this->make(lang('Callbacks', 'navigation'), 'callbacks', $widget); |
||
111 | |||
112 | case 'shop': |
||
113 | return $this->make(lang('Compare', 'navigation'), 'shop/compare', $widget); |
||
114 | |||
115 | case 'wishlist': |
||
116 | return $this->make(lang('Wishlist', 'navigation'), 'wishlist', $widget); |
||
117 | |||
118 | case 'cart': |
||
119 | return $this->make(lang('Cart', 'navigation'), 'shop/cart/', $widget); |
||
120 | |||
121 | case 'feedback': |
||
122 | return $this->make(lang('Feedback', 'navigation'), 'feedback', $widget); |
||
123 | |||
124 | case 'action_type': |
||
125 | return $this->make(lang('Action type', 'navigation'), 'shop/action_type/show', $widget); |
||
126 | |||
127 | case 'shop_category': |
||
128 | if ($this->core->core_data['id'] !== null && $this->core->core_data > 0) { |
||
129 | |||
130 | $category = SCategoryQuery::create()->findOneById($this->core->core_data['id']); |
||
131 | $categories = $category->buildCategoryPath(Criteria::ASC, MY_Controller::getCurrentLocale()); |
||
132 | $paths = []; |
||
133 | |||
134 | View Code Duplication | foreach ($categories as $category) { |
|
135 | $paths[] = [ |
||
136 | 'path_url' => 'shop/category/' . $category->getFullPath(), |
||
137 | 'name' => $category->getName(), |
||
138 | ]; |
||
139 | } |
||
140 | |||
141 | $tpl_data = ['navi_cats' => $paths]; |
||
142 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
143 | } else { |
||
144 | throw new Exception('Category not found'); |
||
145 | } |
||
146 | |||
147 | break; |
||
148 | case 'product': |
||
149 | if ($this->core->core_data['id'] != null && $this->core->core_data['id'] > 0) { |
||
150 | //get product model |
||
151 | $product = SProductsQuery::create() |
||
152 | ->joinWithI18n(MY_Controller::getCurrentLocale()) |
||
153 | ->findOneById($this->core->core_data['id']); |
||
154 | |||
155 | if ($product) { |
||
156 | |||
157 | if ($product->getCategoryId() == null && $product->getCategoryId() == 0) { |
||
158 | throw new Exception('Category not found'); |
||
159 | } |
||
160 | |||
161 | $category = SCategoryQuery::create()->findOneById($product->getCategoryId()); |
||
162 | $categories = $category->buildCategoryPath(Criteria::ASC, MY_Controller::getCurrentLocale()); |
||
163 | |||
164 | View Code Duplication | foreach ($categories as $category) { |
|
165 | $path[] = [ |
||
166 | 'path_url' => 'shop/category/' . $category->getFullPath(), |
||
167 | 'name' => $category->getName(), |
||
168 | ]; |
||
169 | } |
||
170 | |||
171 | $path[] = [ |
||
172 | 'path_url' => '', |
||
173 | 'name' => $product->getName(), |
||
174 | ]; |
||
175 | |||
176 | $tpl_data = ['navi_cats' => $path]; |
||
177 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
178 | } else { |
||
179 | throw new Exception('Product not found'); |
||
180 | } |
||
181 | } |
||
182 | break; |
||
183 | case 'gallery': |
||
184 | $uri_segments = $this->uri->segment_array(); |
||
185 | $template_vars = $this->template->get_vars(); |
||
186 | |||
187 | $path = []; |
||
188 | foreach ($uri_segments as $segment) { |
||
189 | switch ($segment) { |
||
190 | case 'gallery': |
||
191 | $path[] = [ |
||
192 | 'path_url' => $segment, |
||
193 | 'name' => lang('Gallery', 'navigation'), |
||
194 | ]; |
||
195 | break; |
||
196 | case 'category': |
||
197 | $path[] = [ |
||
198 | 'path_url' => 'gallery/category/' . $template_vars['current_category']['id'], |
||
199 | 'name' => $template_vars['current_category']['name'], |
||
200 | ]; |
||
201 | break; |
||
202 | case 'album': |
||
203 | $path[] = [ |
||
204 | 'path_url' => 'gallery/category/' . $template_vars['current_category']['id'], |
||
205 | 'name' => $template_vars['current_category']['name'], |
||
206 | ]; |
||
207 | |||
208 | $path[] = [ |
||
209 | 'path_url' => "gallery/$segment/" . $template_vars['album']['id'], |
||
210 | 'name' => $template_vars['album']['name'], |
||
211 | ]; |
||
212 | break; |
||
213 | case 'albums': |
||
214 | $path[] = [ |
||
215 | 'path_url' => "gallery/$segment/", |
||
216 | 'name' => lang('All albums', 'navigation'), |
||
217 | ]; |
||
218 | break; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | $tpl_data = ['navi_cats' => $path]; |
||
223 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
224 | |||
225 | case 'auth': |
||
226 | $uri_segments = $this->uri->segment_array(); |
||
227 | |||
228 | $path = []; |
||
229 | foreach ($uri_segments as $segment) { |
||
230 | switch ($segment) { |
||
231 | case 'auth': |
||
232 | $path[] = [ |
||
233 | 'path_url' => $segment, |
||
234 | 'name' => lang('Login', 'navigation'), |
||
235 | ]; |
||
236 | break; |
||
237 | case 'register': |
||
238 | $path = []; |
||
239 | $path[] = [ |
||
240 | 'path_url' => "auth/$segment", |
||
241 | 'name' => lang('Registration', 'navigation'), |
||
242 | ]; |
||
243 | break; |
||
244 | case 'activate': |
||
245 | $path = []; |
||
246 | $path[] = [ |
||
247 | 'path_url' => "auth/$segment", |
||
248 | 'name' => lang('Activation', 'navigation'), |
||
249 | ]; |
||
250 | break; |
||
251 | case 'forgot_password': |
||
252 | $path = []; |
||
253 | $path[] = [ |
||
254 | 'path_url' => "auth/$segment", |
||
255 | 'name' => lang('Remind password', 'navigation'), |
||
256 | ]; |
||
257 | break; |
||
258 | case 'reset_password': |
||
259 | $path = []; |
||
260 | $path[] = [ |
||
261 | 'path_url' => "auth/$segment", |
||
262 | 'name' => lang('Reset password', 'navigation'), |
||
263 | ]; |
||
264 | break; |
||
265 | case 'change_password': |
||
266 | $path = []; |
||
267 | $path[] = [ |
||
268 | 'path_url' => "auth/$segment", |
||
269 | 'name' => lang('Change password', 'navigation'), |
||
270 | ]; |
||
271 | break; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | $tpl_data = ['navi_cats' => $path]; |
||
276 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
277 | } |
||
278 | } |
||
279 | |||
298 | /* End of file widgets.php */ |