Completed
Push — master ( 9de44c...cc28ed )
by Paweł
21:07 queued 20:52
created

ProductFormMenuBuilder::createMenu()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
cc 3
eloc 29
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\AdminBundle\Menu;
13
14
use Knp\Menu\FactoryInterface;
15
use Knp\Menu\ItemInterface;
16
use Sylius\Bundle\AdminBundle\Event\ProductMenuBuilderEvent;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
20
/**
21
 * @author Jan Góralski <[email protected]>
22
 */
23
final class ProductFormMenuBuilder
24
{
25
    const EVENT_NAME = 'sylius.menu.admin.product.form';
26
27
    /**
28
     * @var FactoryInterface
29
     */
30
    private $factory;
31
32
    /**
33
     * @var EventDispatcherInterface
34
     */
35
    private $eventDispatcher;
36
37
    /**
38
     * @param FactoryInterface $factory
39
     * @param EventDispatcherInterface $eventDispatcher
40
     */
41
    public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
42
    {
43
        $this->factory = $factory;
44
        $this->eventDispatcher = $eventDispatcher;
45
    }
46
47
    /**
48
     * @param array $options
49
     *
50
     * @return ItemInterface
51
     */
52
    public function createMenu(array $options = [])
53
    {
54
        $menu = $this->factory->createItem('root');
55
56
        if (!array_key_exists('product', $options) || !$options['product'] instanceof ProductInterface) {
57
            return $menu;
58
        }
59
60
        $menu
61
            ->addChild('details')
62
            ->setAttribute('template', '@SyliusAdmin/Product/Tab/_details.html.twig')
63
            ->setLabel('sylius.ui.details')
64
            ->setCurrent(true)
65
        ;
66
67
        $menu
68
            ->addChild('taxonomy')
69
            ->setAttribute('template', '@SyliusAdmin/Product/Tab/_taxonomy.html.twig')
70
            ->setLabel('sylius.ui.taxonomy')
71
        ;
72
73
        $menu
74
            ->addChild('attributes')
75
            ->setAttribute('template', '@SyliusAdmin/Product/Tab/_attributes.html.twig')
76
            ->setLabel('sylius.ui.attributes')
77
        ;
78
79
        $menu
80
            ->addChild('associations')
81
            ->setAttribute('template', '@SyliusAdmin/Product/Tab/_associations.html.twig')
82
            ->setLabel('sylius.ui.associations')
83
        ;
84
85
        $menu
86
            ->addChild('media')
87
            ->setAttribute('template', '@SyliusAdmin/Product/Tab/_media.html.twig')
88
            ->setLabel('sylius.ui.media')
89
        ;
90
91
        $this->eventDispatcher->dispatch(
92
            self::EVENT_NAME,
93
            new ProductMenuBuilderEvent($this->factory, $menu, $options['product'])
94
        );
95
96
        return $menu;
97
    }
98
}
99