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

MultipleBlockEventListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 16 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\Block;
15
16
use Sonata\BlockBundle\Event\BlockEvent;
17
use Sonata\BlockBundle\Model\Block;
18
19
/** @internal */
20
final class MultipleBlockEventListener
21
{
22
    /**
23
     * @var array
24
     *
25
     * @psalm-var array<string, array<array-key, array{template: string, name: string}>>
26
     */
27
    private $blocksForEvents;
28
29
    public function __construct(array $blocksForEvents)
30
    {
31
        $this->blocksForEvents = $blocksForEvents;
32
    }
33
34
    public function __invoke(BlockEvent $event, string $eventName): void
35
    {
36
        $eventName = str_replace('sonata.block.event.', '', $eventName);
37
        $blocks = $this->blocksForEvents[$eventName] ?? [];
38
39
        foreach ($blocks as $block) {
40
            $sonataBlock = new Block();
41
            $sonataBlock->setId(sprintf('%s=%s', $eventName, $block['name']));
42
            $sonataBlock->setSettings(array_replace($event->getSettings(), [
43
                'template' => $block['template'],
44
            ]));
45
            $sonataBlock->setType('sonata.block.service.template');
46
47
            $event->addBlock($sonataBlock);
48
        }
49
    }
50
}
51