Passed
Push — master ( 8fde47...cc75d5 )
by Georgi
03:09
created

NavMenu::addItems()   C

Complexity

Conditions 16
Paths 1

Size

Total Lines 67
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 39
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 67
rs 5.5666

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php 
2
3
namespace Epesi\Core\Layout\Seeds;
4
5
use Illuminate\Support\Collection;
6
use atk4\ui\Menu as BaseMenu;
7
use atk4\ui\Accordion;
8
use atk4\ui\jsExpressionable;
9
use Epesi\Core\Layout\Integration\Joints\NavMenuJoint;
10
11
class NavMenu extends BaseMenu
12
{
13
	public function init()
14
	{		
15
		parent::init();
16
		
17
		$this->addHeader($this->app->title);
18
		
19
		$items = collect();
20
		foreach(NavMenuJoint::collect() as $joint) {
21
			$items = $items->merge($joint->items());
22
		}
23
24
		$this->addItems($this, $items);
25
		
26
// 		$this->js(true)->find('.toggle-group .header')->click(new jsFunction(['e'], [new jsExpression('$(e.target).next(".menu").slideToggle()')]))->click();
27
		
28
// 		$this->app->addStyle('
29
// 			.toggle-group .header {
30
// 				cursor: pointer;
31
// 			}
32
// 		');
33
	}
34
	
35
	public static function addItems($menu, Collection $items)
36
	{
37
		$items->sort(function ($entry1, $entry2) {
38
			$weight1 = $entry1['weight']?? 10;
39
			$weight2 = $entry2['weight']?? 10;
40
			
41
			return $weight1 <=> $weight2;
42
		})->map(function($item, $caption) use ($menu) {
43
			if (! ($item['access']?? true)) return;
44
			
45
			if (!isset($item['link']) && !is_array($item)) {
46
				$item = [
47
						'link' => $item
48
				];
49
			}
50
			
51
			$item['caption'] = $item['caption']?? $caption;
52
			
53
			if (is_array($item['caption'])) {
54
				$item['caption'] = [$caption] + $item['caption'];
55
			}
56
			
57
			if ($subitems = $item['menu']?? []) {
58
				$submenu = $menu->addMenu($item['caption']);
59
				
60
				self::addItems($submenu, collect($subitems));
61
			}
62
			elseif ($subitems = $item['group']?? []) {
63
				$subgroup = $menu->addGroup($item['caption']);
64
				
65
				if (($item['toggle']?? false) && !$menu->in_dropdown) {
66
					$subgroup->addClass('toggle-group');
67
					$subgroup->add(['Icon', 'dropdown'], 'Icon')->removeClass('item');
68
				}
69
				
70
				self::addItems($subgroup, collect($subitems));
71
			}
72
			elseif ($subitems = $item['accordion']?? []) {
73
				$accordion = $menu->add(['Accordion']);
74
				
75
				$section = $accordion->addSection($item['caption']);
76
				
77
				foreach ($subitems as $subitem) {
78
					$subitem = $section->add(['Item', 'Test', 'ui' => 'item'])->setElement('a');
79
					
80
					$action = null;
81
					$link = $item['link']?? '';
82
					if (is_string($link) || is_array($link)) {
83
						$action = $section->url($link);
84
					}
85
					
86
					if (is_string($link)) {
87
						$subitem->setAttr('href', $link);
88
					}
89
					
90
					if ($action instanceof jsExpressionable) {
91
						$subitem->js('click', $link);
92
					}
93
				}
94
// 				self::addItems($accordion, collect($subitems));
95
			}
96
			else {
97
				if (is_a($menu, Accordion::class)) {
98
					$menu->add(['View', 'Test']);
99
				}
100
				else {
101
					$menu->addItem($item['caption'], $item['link']?? '');
102
				}
103
				
104
			}
105
		});
106
	}
107
}