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

ProductUpdateMenuBuilder::createMenu()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 8.8727
c 0
b 0
f 0
cc 4
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ProductMenuBuilderEvent;
19
use Sylius\Component\Core\Model\ProductInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
final class ProductUpdateMenuBuilder
23
{
24
    public const EVENT_NAME = 'sylius.menu.admin.product.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
42
        if (!isset($options['product'])) {
43
            return $menu;
44
        }
45
46
        $product = $options['product'];
47
        if (!$product instanceof ProductInterface) {
48
            return $menu;
49
        }
50
51
        $manageVariantsItem = $this->factory
52
            ->createItem('manage_variants')
53
            ->setAttribute('type', 'links')
54
            ->setLabel('sylius.ui.manage_variants')
55
            ->setLabelAttribute('icon', 'cubes')
56
        ;
57
58
        $manageVariantsItem
59
            ->addChild('product_variant_index', [
60
                'route' => 'sylius_admin_product_variant_index',
61
                'routeParameters' => ['productId' => $product->getId()],
62
            ])
63
            ->setAttribute('type', 'link')
64
            ->setLabel('sylius.ui.list_variants')
65
            ->setLabelAttribute('icon', 'list')
66
        ;
67
        $manageVariantsItem
68
            ->addChild('product_variant_create', [
69
                'route' => 'sylius_admin_product_variant_create',
70
                'routeParameters' => ['productId' => $product->getId()],
71
            ])
72
            ->setAttribute('type', 'link')
73
            ->setLabel('sylius.ui.create')
74
            ->setLabelAttribute('icon', 'plus')
75
        ;
76
77
        if ($product->hasOptions()) {
78
            $manageVariantsItem
79
                ->addChild('product_variant_generate', [
80
                    'route' => 'sylius_admin_product_variant_generate',
81
                    'routeParameters' => ['productId' => $product->getId()],
82
                ])
83
                ->setAttribute('type', 'link')
84
                ->setLabel('sylius.ui.generate')
85
                ->setLabelAttribute('icon', 'random')
86
            ;
87
        }
88
89
        $menu->addChild($manageVariantsItem);
90
91
        $this->eventDispatcher->dispatch(
92
            self::EVENT_NAME,
93
            new ProductMenuBuilderEvent($this->factory, $menu, $product)
94
        );
95
96
        return $menu;
97
    }
98
}
99