RightMenu::init()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 65
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 42
nc 5
nop 0
dl 0
loc 65
rs 8.6257
c 0
b 0
f 0

How to fix   Long Method   

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\View;
4
5
use atk4\ui\Menu as BaseMenu;
6
use atk4\ui\Form\Control\Input;
7
use Epesi\Core\Layout\Integration\Joints\UserMenuJoint;
8
use Illuminate\Support\Facades\Auth;
9
use atk4\ui\View;
10
use atk4\ui\VirtualPage;
11
use Epesi\Core\System\View\LaunchButton;
12
use atk4\ui\JsModal;
13
use Illuminate\Support\Facades\URL;
14
15
class RightMenu extends BaseMenu
16
{
17
	protected $entries;
18
	protected $tools;
19
	
20
	protected $userMenu;
21
	
22
	protected $userMenuLabel;
23
	
24
	protected function init(): void
25
	{		
26
		parent::init();
27
28
		$this->entries = collect();
29
		
30
		$this->tools = collect();
31
		
32
		// credits
33
		$this->addItem(__(':epesi powered version :version', [
34
				'epesi' => config('epesi.ui.credit.title', 'EPESI'), 
35
				'version' => $this->getApp()->version
36
		]))->setStyle('font-size', '80%')->link(config('epesi.ui.credit.link'));
37
		
38
		// global search
39
		$this->addItem()->add(new Input([
40
				'placeholder' => 'Search ' . config('epesi.ui.title', 'EPESI'),
41
				'icon' => 'search'
42
		]))->addClass('transparent');
0 ignored issues
show
Bug introduced by
The method addClass() does not exist on atk4\ui\AbstractView. It seems like you code against a sub-type of atk4\ui\AbstractView such as atk4\ui\View. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
		]))->/** @scrutinizer ignore-call */ addClass('transparent');
Loading history...
43
		
44
		// $messageMenu = $this->menuRight->addMenu(['', 'icon' => 'envelope outline']);
45
		
46
		$this->addItem([
47
				'icon' => 'th'
48
		], Launchpad::addTo($this)->getJsModal());
0 ignored issues
show
Bug introduced by
Epesi\Core\Layout\View\L...To($this)->getJsModal() of type atk4\ui\JsModal is incompatible with the type array|string expected by parameter $action of atk4\ui\Menu::addItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
		], /** @scrutinizer ignore-type */ Launchpad::addTo($this)->getJsModal());
Loading history...
49
		
50
		foreach(UserMenuJoint::collect() as $joint) {
51
			foreach ($joint->tools()?: [] as $tool) {
52
				$this->addTool($tool);
53
			}
54
55
			foreach ($joint->entries()?: [] as $entry) {
56
				$this->addEntry($entry);
57
			}			
58
		}
59
60
		$this->userMenu = $this->addMenu([
61
				$this->getUserMenuLabel(),
62
				'icon' => 'user'
63
		]);
64
		
65
		$this->addEntry(['Perspective', 'icon' => 'users']);
66
		$this->addEntry(['My Contact', 'icon' => 'contact']);
67
		$this->addEntry(['My Company', 'icon' => 'users']);
68
		
69
		$this->addEntry([
70
				'item' => ['Logout', 'icon' => 'sign out', 'attr' => ['onclick' => "event.preventDefault();$('#logout-form').submit();"]],
71
				'action' => url('logout'), 
72
				'group' => '10000:user',
73
				'callback' => function ($item) {
74
					$logoutForm = View::addTo($item, [
75
							'id' => 'logout-form', 
76
							'attr' => [
77
									'method' => 'POST', 
78
									'action' => url('logout'),
79
							],
80
					])->setElement('form')->addStyle(['display' => 'none']);
81
82
					View::addTo($logoutForm, [
83
							'attr' => [
84
									'type' => 'hidden', 
85
									'name' => '_token', 
86
									'value' => csrf_token(),
87
							],
88
					])->setElement('input');
89
				},
90
		]);
91
	}
92
93
	public function setUserMenuLabel($label)
94
	{
95
		$this->userMenuLabel = $label;
96
		
97
		return $this;
98
	}
99
	
100
	public function getUserMenuLabel()
101
	{
102
		return $this->userMenuLabel?: Auth::user()->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
103
	}
104
	
105
	public function addEntry($entry)
106
	{
107
		$entry = collect(array_merge(['item' => $entry, 'group' => '00500:general', 'weight' => 10], $entry));
108
		
109
		if (! $entry->get('item')) return;
110
		
111
		$this->entries->add($entry);
112
	}
113
	
114
	public function addTool($tool)
115
	{
116
		
117
	}
118
	
119
	public function renderView(): void
120
	{
121
		$this->addRegisteredEntries();
122
		
123
		parent::renderView();
124
	}
125
	
126
	protected function addRegisteredEntries()
127
	{
128
		$empty = true;
129
		foreach ($this->entries->groupBy('group')->sortKeys() as $group) {
130
			if (!$empty) $this->userMenu->addDivider();
131
		
132
			foreach ($group->sortBy('weight') as $entry) {
133
				$empty = false;
134
				
135
				$item = $this->userMenu->addItem($entry['item'], $entry->get('action'));
136
				
137
				if (! $callback = $entry->get('callback')) continue;
138
					
139
				$callback($item);
140
			}
141
		}
142
	}
143
}