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

RightMenu::init()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 30
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 48
rs 8.8177
1
<?php 
2
3
namespace Epesi\Core\Layout\Seeds;
4
5
use atk4\ui\Menu as BaseMenu;
6
use atk4\ui\FormField\Input;
7
use Illuminate\Support\Facades\URL;
8
use Epesi\Core\Layout\Integration\Joints\UserMenuJoint;
9
use Illuminate\Support\Facades\Auth;
10
11
class RightMenu extends BaseMenu
12
{
13
	protected $entries;
14
	protected $tools;
15
	
16
	protected $userMenu;
17
	
18
	protected $userMenuLabel;
19
	
20
	public function init()
21
	{		
22
		parent::init();
23
24
		$this->entries = collect();
25
		
26
		$this->tools = collect();
27
		
28
		$this->addItem()->add(new Input([
29
				'placeholder' => 'Search',
30
				'icon' => 'search'
31
		]))->addClass('transparent');
32
		
33
		// $messageMenu = $this->menuRight->addMenu(['', 'icon' => 'envelope outline']);
34
		
35
		$this->addItem([
36
				'icon' => 'th'
37
		], new LaunchPad($this))->setAttr([
1 ignored issue
show
Bug introduced by
new Epesi\Core\Layout\Seeds\LaunchPad($this) of type Epesi\Core\Layout\Seeds\LaunchPad 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

37
		], /** @scrutinizer ignore-type */ new LaunchPad($this))->setAttr([
Loading history...
38
				'title' => __('Launchpad'),
39
		]);
40
		
41
		foreach(UserMenuJoint::collect() as $joint) {
42
			foreach ($joint->tools()?: [] as $tool) {
43
				$this->addTool($tool);
44
			}
45
46
			foreach ($joint->entries()?: [] as $entry) {
47
				$this->addEntry($entry);
48
			}			
49
		}
50
51
		$this->userMenu = $this->addMenu([
52
				$this->getUserMenuLabel(),
53
				'icon' => 'user'
54
		]);
55
		
56
		$this->addEntry(['Perspective', 'icon' => 'users']);
57
		$this->addEntry(['My Contact', 'icon' => 'contact']);
58
		$this->addEntry(['My Company', 'icon' => 'users']);
59
		
60
		$this->addEntry([
61
				'item' => ['Logout', 'icon' => 'sign out', 'attr' => ['onclick' => "event.preventDefault();$('#logout-form').submit();"]],
62
				'action' => url('logout'), 
63
				'group' => '10000:user',
64
				'callback' => function ($item){
65
					$logoutForm = $item->add(['View', 'attr' => ['method' => 'POST', 'action' => URL::to('logout')]])->setElement('form')->addStyle(['display' => 'none']);
66
					$logoutForm->id = 'logout-form';
67
					$logoutForm->add(['View', 'attr' => ['type' => 'hidden', 'name' => '_token', 'value' => csrf_token()]])->setElement('input');
68
				},
69
		]);
70
	}
71
72
	public function setUserMenuLabel($label)
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed. ( Ignorable by Annotation )

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

72
	public function setUserMenuLabel(/** @scrutinizer ignore-unused */ $label)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
	{
74
		$label = $this->userMenuLabel;
0 ignored issues
show
Unused Code introduced by
The assignment to $label is dead and can be removed.
Loading history...
75
		
76
		return $this;
77
	}
78
	
79
	public function getUserMenuLabel()
80
	{
81
		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...
82
	}
83
	
84
	public function addEntry($entry)
85
	{
86
		$entry = collect(array_merge(['item' => $entry, 'group' => '00500:general', 'weight' => 10], $entry));
87
		
88
		if (! $entry->get('item')) return;
89
		
90
		$this->entries->add($entry);
91
	}
92
	
93
	public function addTool($tool)
0 ignored issues
show
Unused Code introduced by
The parameter $tool is not used and could be removed. ( Ignorable by Annotation )

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

93
	public function addTool(/** @scrutinizer ignore-unused */ $tool)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
	{
95
		
96
	}
97
	
98
	public function renderView()
99
	{
100
		$this->addRegisteredEntries();
101
		
102
		parent::renderView();
103
	}
104
	
105
	protected function addRegisteredEntries()
106
	{
107
		$empty = true;
108
		foreach ($this->entries->groupBy('group')->sortKeys() as $group) {
109
			if (!$empty) $this->userMenu->addDivider();
110
		
111
			foreach ($group->sortBy('weight') as $entry) {
112
				$empty = false;
113
				
114
				$item = $this->userMenu->addItem($entry['item'], $entry->get('action'));
115
				
116
				if (! $callback = $entry->get('callback')) continue;
117
					
118
				$callback($item);
119
			}
120
		}
121
	}
122
}