Completed
Push — allow-no-default-tax-zone-in-c... ( 67cea0 )
by Kamil
06:23
created

PromotionUpdateMenuBuilder::createMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\AdminBundle\Menu;
15
16
use Knp\Menu\FactoryInterface;
17
use Knp\Menu\ItemInterface;
18
use Sylius\Bundle\AdminBundle\Event\PromotionMenuBuilderEvent;
19
use Sylius\Component\Core\Model\PromotionInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
final class PromotionUpdateMenuBuilder
23
{
24
    public const EVENT_NAME = 'sylius.menu.admin.promotion.update';
25
26
    /** @var FactoryInterface */
27
    private $factory;
28
29
    /** @var EventDispatcherInterface */
30
    private $eventDispatcher;
31
32
    public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
33
    {
34
        $this->factory = $factory;
35
        $this->eventDispatcher = $eventDispatcher;
36
    }
37
38
    public function createMenu(array $options): ItemInterface
39
    {
40
        $menu = $this->factory->createItem('root');
41
        if (!isset($options['promotion'])) {
42
            return $menu;
43
        }
44
45
        $promotion = $options['promotion'];
46
47
        $this->addChildren($menu, $promotion);
48
        $this->eventDispatcher->dispatch(
49
            self::EVENT_NAME,
50
            new PromotionMenuBuilderEvent($this->factory, $menu, $promotion)
51
        );
52
53
        return $menu;
54
    }
55
56
    private function addChildren(ItemInterface $menu, PromotionInterface $promotions): void
57
    {
58
        $menu
59
            ->addChild('manage_coupons', [
60
                'route' => 'sylius_admin_promotion_coupon_index',
61
                'routeParameters' => ['promotionId' => $promotions->getId()],
62
            ])
63
            ->setAttribute('type', 'link')
64
            ->setLabel('sylius.ui.manage_coupons')
65
            ->setLabelAttribute('icon', 'ticket')
66
            ->setLabelAttribute('color', 'gray')
67
        ;
68
    }
69
}
70