Refactored

This commit is contained in:
Kevin Frantz 2018-10-04 22:22:47 +02:00
parent 064ca5981c
commit 368cbebbc1

View File

@ -1,5 +1,4 @@
<?php <?php
namespace App\Menu; namespace App\Menu;
use Knp\Menu\FactoryInterface; use Knp\Menu\FactoryInterface;
@ -11,12 +10,15 @@ use App\DBAL\Types\MenuEventType;
class Menu class Menu
{ {
/** /**
*
* @var EventDispatcherInterface * @var EventDispatcherInterface
*/ */
private $dispatcher; private $dispatcher;
/** /**
*
* @var FactoryInterface * @var FactoryInterface
*/ */
private $factory; private $factory;
@ -29,30 +31,32 @@ class Menu
public function sourceNavbar(RequestStack $request): ItemInterface public function sourceNavbar(RequestStack $request): ItemInterface
{ {
$menu = $this->createBasicMenuItem(); return $this->createMenu(MenuEventType::SOURCE, $request);
$this->dispatcher->dispatch(MenuEventType::SOURCE, new MenuEvent($this->factory, $menu, $request));
return $menu;
} }
public function nodeSubbar(RequestStack $request): ItemInterface public function nodeSubbar(RequestStack $request): ItemInterface
{ {
$menu = $this->createBasicMenuItem(); return $this->createMenu(MenuEventType::NODE, $request);
$this->dispatcher->dispatch(MenuEventType::NODE, new MenuEvent($this->factory, $menu, $request));
return $menu;
} }
public function userTopbar(RequestStack $request): ItemInterface public function userTopbar(RequestStack $request): ItemInterface
{ {
$menu = $this->createBasicMenuItem(); return $this->createMenu(MenuEventType::USER, $request);
$this->dispatcher->dispatch(MenuEventType::USER, new MenuEvent($this->factory, $menu, $request)); }
private function createMenu(string $type, RequestStack $request): ItemInterface
{
$menu = $this->createBasicMenuItem();
$this->dispatcher->dispatch($type, new MenuEvent($this->factory, $menu, $request));
return $menu; return $menu;
} }
private function createBasicMenuItem():ItemInterface{ private function createBasicMenuItem(): ItemInterface
{
return $this->factory->createItem('root', [ return $this->factory->createItem('root', [
'childrenAttributes' => [ 'childrenAttributes' => [
'class' => 'navbar-nav mr-auto', 'class' => 'navbar-nav mr-auto'
], ]
]); ]);
} }
} }