Completed
Push — master ( 0e5803...f3fa63 )
by
unknown
05:14
created

MenuBuilderListener::addMenuItems()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 34
ccs 0
cts 32
cp 0
rs 8.8571
c 1
b 1
f 0
cc 2
eloc 19
nc 2
nop 1
crap 6
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\EventListener;
4
5
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
6
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
class MenuBuilderListener
10
{
11
    /**
12
     * @var ProviderPool
13
     */
14
    private $providerPool;
15
16
    /**
17
     * @var TranslatorInterface
18
     */
19
    private $translator;
20
21
    /**
22
     * @param ProviderPool $providerPool
23
     * @param TranslatorInterface $translator
24
     */
25
    public function __construct(
26
        ProviderPool $providerPool,
27
        TranslatorInterface $translator
28
    ) {
29
        $this->providerPool = $providerPool;
30
        $this->translator = $translator;
31
    }
32
33
    /**
34
     * @param ConfigureMenuEvent $event
35
     */
36
    public function addMenuItems(ConfigureMenuEvent $event)
37
    {
38
        $menu = $event->getMenu();
39
40
        $child = $menu->getChild('Media');
41
42
        foreach ($this->providerPool->getProviders(
43
        ) as $providerClass => $provider) {
44
            $providerChild = $child->addChild(
45
                'provider_'.spl_object_hash($provider),
46
                [
47
                    'route' => 'admin_mediamonks_sonatamedia_media_create',
48
                    'routeParameters' => [
49
                        'provider' => $providerClass,
50
                    ],
51
                ]
52
            );
53
            $providerChild->setAttribute(
54
                'icon',
55
                sprintf(
56
                    '<i class="%s" aria-hidden="true"></i>',
57
                    $provider->getIcon()
58
                )
59
            );
60
            $providerChild->setLabel(
61
                $this->translator->trans(
62
                    'menu.provider',
63
                    [
64
                        '%provider%' => $this->translator->trans($provider->getName())
65
                    ]
66
                )
67
            );
68
        }
69
    }
70
}
71