Completed
Push — master ( ea0a23...156714 )
by Joachim
12s queued 11s
created

BrandFormMenuBuilder::createMenu()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 2
nop 1
dl 0
loc 27
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\SyliusBrandPlugin\Menu;
6
7
use Knp\Menu\FactoryInterface;
8
use Knp\Menu\ItemInterface;
9
use Loevgaard\SyliusBrandPlugin\Event\BrandMenuBuilderEvent;
10
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
final class BrandFormMenuBuilder
14
{
15
    public const EVENT_NAME = 'loevgaard_sylius_brand.menu.admin.brand.form';
16
17
    /** @var FactoryInterface */
18
    private $factory;
19
20
    /** @var EventDispatcherInterface */
21
    private $eventDispatcher;
22
23
    public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
24
    {
25
        $this->factory = $factory;
26
        $this->eventDispatcher = $eventDispatcher;
27
    }
28
29
    public function createMenu(array $options = []): ItemInterface
30
    {
31
        $menu = $this->factory->createItem('root');
32
33
        if (!array_key_exists('brand', $options) || !$options['brand'] instanceof BrandInterface) {
34
            return $menu;
35
        }
36
37
        $menu
38
            ->addChild('details')
39
            ->setAttribute('template', '@LoevgaardSyliusBrandPlugin/Admin/Brand/Tab/_details.html.twig')
40
            ->setLabel('sylius.ui.details')
41
            ->setCurrent(true)
42
        ;
43
44
        $menu
45
            ->addChild('media')
46
            ->setAttribute('template', '@LoevgaardSyliusBrandPlugin/Admin/Brand/Tab/_media.html.twig')
47
            ->setLabel('sylius.ui.media')
48
        ;
49
50
        $this->eventDispatcher->dispatch(
51
            self::EVENT_NAME,
52
            new BrandMenuBuilderEvent($this->factory, $menu, $options['brand'])
53
        );
54
55
        return $menu;
56
    }
57
}
58