Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

NodeBundle/EventListener/EntityTabListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
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
    public function adaptForm(AdaptSimpleFormEvent $event)
43
    {
44
        $entity = $event->getData();
45
        $tabPane = $event->getTabPane();
46
47
        if ($entity instanceof HasNodeInterface) {
48
            return;
49
        }
50
51
        if ($entity instanceof PageTabInterface === false) {
52
            return;
53
        }
54
55
        if ($tabPane instanceof TabPane === false) {
56
            $tabPane = new TabPane('id', $this->request, $this->formFactory);
57
        }
58
59
        $this->addTabs($tabPane, $entity);
60
61
        $tabPane->buildForm();
62
63
        $event->setTabPane($tabPane);
64
    }
65
}
66