Completed
Push — new-template-events-architectu... ( 533634 )
by Kamil
05:23
created

SyliusUiExtension::loadEvents()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
cc 5
nc 7
nop 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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UiBundle\DependencyInjection;
15
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Zend\Stdlib\SplPriorityQueue;
21
22
final class SyliusUiExtension extends Extension
23
{
24
    public function load(array $config, ContainerBuilder $container): void
25
    {
26
        $config = $this->processConfiguration($this->getConfiguration([], $container), $config);
27
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
28
29
        $loader->load('services.xml');
30
31
        $this->loadEvents($config['events'], $container);
32
    }
33
34
    /**
35
     * @psalm-param array<string, array{blocks: array<string, array{template: string, priority: int, enabled: bool}>}> $eventsConfig
36
     */
37
    private function loadEvents(array $eventsConfig, ContainerBuilder $container): void
38
    {
39
        $multipleBlockEventListenerDefinition = $container->getDefinition('sylius.ui.sonata_multiple_block_event_listener');
40
41
        $blocksForEvents = [];
42
        foreach ($eventsConfig as $eventName => $eventConfiguration) {
43
            $blocksPriorityQueue = new SplPriorityQueue();
44
45
            foreach ($eventConfiguration['blocks'] as $blockName => $details) {
46
                if ($details['enabled'] === false) {
47
                    continue;
48
                }
49
50
                $blocksPriorityQueue->insert(
51
                    [
52
                        'template' => $details['template'],
53
                        'name' => $blockName,
54
                    ],
55
                    $details['priority']
56
                );
57
            }
58
59
            if ($blocksPriorityQueue->count() === 0) {
60
                continue;
61
            }
62
63
            $blocksForEvents[$eventName] = $blocksPriorityQueue->toArray();
64
65
            $multipleBlockEventListenerDefinition->addTag(
66
                'kernel.event_listener',
67
                [
68
                    'event' => sprintf('sonata.block.event.%s', $eventName),
69
                    'method' => '__invoke',
70
                ]
71
            );
72
        }
73
74
        $multipleBlockEventListenerDefinition->setArgument(0, $blocksForEvents);
75
    }
76
}
77