BrandFormMenuBuilder::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 3
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 10
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
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
13
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
14
15
final class BrandFormMenuBuilder
16
{
17
    public const EVENT_NAME = 'loevgaard_sylius_brand.menu.admin.brand.form';
18
19
    /** @var FactoryInterface */
20
    private $factory;
21
22
    /** @var EventDispatcherInterface */
23
    private $eventDispatcher;
24
25
    public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
26
    {
27
        $this->factory = $factory;
28
29
        if (class_exists('Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy')) {
30
            /**
31
             * It could return null only if we pass null, but we pass not null in any case
32
             *
33
             * @var ContractsEventDispatcherInterface
34
             */
35
            $eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
36
        }
37
38
        $this->eventDispatcher = $eventDispatcher;
39
    }
40
41
    public function createMenu(array $options = []): ItemInterface
42
    {
43
        $menu = $this->factory->createItem('root');
44
45
        if (!array_key_exists('brand', $options) || !$options['brand'] instanceof BrandInterface) {
46
            return $menu;
47
        }
48
49
        $menu
50
            ->addChild('details')
51
            ->setAttribute('template', '@LoevgaardSyliusBrandPlugin/Admin/Brand/Tab/_details.html.twig')
52
            ->setLabel('sylius.ui.details')
53
            ->setCurrent(true)
54
        ;
55
56
        $menu
57
            ->addChild('media')
58
            ->setAttribute('template', '@LoevgaardSyliusBrandPlugin/Admin/Brand/Tab/_media.html.twig')
59
            ->setLabel('sylius.ui.media')
60
        ;
61
62
        if (class_exists('Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy')) {
63
            $this->eventDispatcher->dispatch(
64
                new BrandMenuBuilderEvent($this->factory, $menu, $options['brand']),
65
                self::EVENT_NAME
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with self::EVENT_NAME. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
66
                                    dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
            );
67
        } else {
68
            $this->eventDispatcher->dispatch(
69
                self::EVENT_NAME,
0 ignored issues
show
Bug introduced by
self::EVENT_NAME of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
                /** @scrutinizer ignore-type */ self::EVENT_NAME,
Loading history...
70
                new BrandMenuBuilderEvent($this->factory, $menu, $options['brand'])
71
            );
72
        }
73
74
        return $menu;
75
    }
76
}
77