Completed
Push — locale-in-url ( b918e2...821ef5 )
by Kamil
48:02 queued 26:12
created

CustomerShowMenuBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createMenu() 0 15 2
A addChildren() 0 47 2
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\UiBundle\Menu\Event\MenuBuilderEvent;
17
use Sylius\Component\Core\Model\CustomerInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
20
/**
21
 * @author Grzegorz Sadowski <[email protected]>
22
 */
23
final class CustomerShowMenuBuilder
24
{
25
    const EVENT_NAME = 'sylius.menu.admin.customer.show';
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(
42
        FactoryInterface $factory,
43
        EventDispatcherInterface $eventDispatcher
44
    ) {
45
        $this->factory = $factory;
46
        $this->eventDispatcher = $eventDispatcher;
47
    }
48
49
    /**
50
     * @param array $options
51
     *
52
     * @return ItemInterface
53
     */
54
    public function createMenu(array $options)
55
    {
56
        $menu = $this->factory->createItem('root');
57
58
        if (!isset($options['customer'])) {
59
            return $menu;
60
        }
61
62
        $customer = $options['customer'];
63
        $this->addChildren($menu, $customer);
64
65
        $this->eventDispatcher->dispatch(self::EVENT_NAME, new MenuBuilderEvent($this->factory, $menu));
66
67
        return $menu;
68
    }
69
70
    /**
71
     * @param ItemInterface $menu
72
     */
73
    private function addChildren(ItemInterface $menu, CustomerInterface $customer)
74
    {
75
        if (null !== $customer->getUser()) {
76
            $menu->setExtra('column_id', 'actions');
77
78
            $menu
79
                ->addChild('update', [
80
                    'route' => 'sylius_admin_customer_update',
81
                    'routeParameters' => ['id' => $customer->getId()]
82
                ])
83
                ->setAttribute('type', 'edit')
84
                ->setLabel('sylius.ui.edit')
85
            ;
86
87
            $menu
88
                ->addChild('order_index', [
89
                    'route' => 'sylius_admin_customer_order_index',
90
                    'routeParameters' => ['id' => $customer->getId()]
91
                ])
92
                ->setAttribute('type', 'show')
93
                ->setLabel('sylius.ui.show_orders')
94
            ;
95
96
            $menu
97
                ->addChild('user_delete', [
98
                    'route' => 'sylius_admin_shop_user_delete',
99
                    'routeParameters' => ['id' => $customer->getUser()->getId()]
100
                ])
101
                ->setAttribute('type', 'delete')
102
                ->setAttribute('resource_id', $customer->getId())
103
                ->setLabel('sylius.ui.delete')
104
            ;
105
106
            return;
107
        }
108
109
        $menu->setExtra('column_id', 'no-account');
110
111
        $menu
112
            ->addChild('order_index', [
113
                'route' => 'sylius_admin_customer_order_index',
114
                'routeParameters' => ['id' => $customer->getId()]
115
            ])
116
            ->setAttribute('type', 'show')
117
            ->setLabel('sylius.ui.show_orders')
118
        ;
119
    }
120
}
121