1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Event\AdaptSimpleFormEvent; |
6
|
|
|
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget; |
7
|
|
|
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab; |
8
|
|
|
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabPane; |
9
|
|
|
use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
10
|
|
|
use Kunstmaan\NodeBundle\Entity\PageTabInterface; |
11
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
14
|
|
|
|
15
|
|
|
class EntityTabListener |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Request |
19
|
|
|
*/ |
20
|
|
|
private $request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var FormFactoryInterface |
24
|
|
|
*/ |
25
|
|
|
private $formFactory; |
26
|
|
|
|
27
|
|
|
public function __construct(RequestStack $requestStack, FormFactoryInterface $formFactory) |
28
|
|
|
{ |
29
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
30
|
|
|
$this->formFactory = $formFactory; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function addTabs(TabPane $tabPane, PageTabInterface $page) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
foreach ($page->getTabs() as $pageTab) { |
36
|
|
|
$formWidget = new FormWidget(); |
37
|
|
|
$formWidget->addType($pageTab->getInternalName(), $pageTab->getFormTypeClass(), $page); |
38
|
|
|
$tabPane->addTab(new Tab($pageTab->getTabTitle(), $formWidget), $pageTab->getPosition()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param AdaptSimpleFormEvent $event |
44
|
|
|
*/ |
45
|
|
|
public function adaptForm(AdaptSimpleFormEvent $event) |
46
|
|
|
{ |
47
|
|
|
$entity = $event->getData(); |
48
|
|
|
$tabPane = $event->getTabPane(); |
49
|
|
|
|
50
|
|
|
if ($entity instanceof HasNodeInterface) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($entity instanceof PageTabInterface === false) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($tabPane instanceof TabPane === false) { |
59
|
|
|
$tabPane = new TabPane('id', $this->request, $this->formFactory); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->addTabs($tabPane, $entity); |
63
|
|
|
|
64
|
|
|
$tabPane->buildForm(); |
65
|
|
|
|
66
|
|
|
$event->setTabPane($tabPane); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.