BreadcrumbListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addBlockService() 0 4 1
A onBlock() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\Event;
15
16
use Sonata\BlockBundle\Block\Service\BlockServiceInterface;
17
use Sonata\BlockBundle\Event\BlockEvent;
18
use Sonata\BlockBundle\Model\Block;
19
20
/**
21
 * BreadcrumbListener for Block Event.
22
 *
23
 * @author Sylvain Deloux <[email protected]>
24
 */
25
final class BreadcrumbListener
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $blockServices = [];
31
32
    /**
33
     * Add a renderer to the status services list.
34
     *
35
     * @param string $type
36
     */
37
    public function addBlockService($type, BlockServiceInterface $blockService): void
38
    {
39
        $this->blockServices[$type] = $blockService;
40
    }
41
42
    /**
43
     * Add context related BlockService, if found.
44
     */
45
    public function onBlock(BlockEvent $event): void
46
    {
47
        $context = $event->getSetting('context', null);
48
49
        if (null === $context) {
50
            return;
51
        }
52
53
        foreach ($this->blockServices as $type => $blockService) {
54
            if ($blockService->handleContext($context)) {
55
                $block = new Block();
56
                $block->setId(uniqid());
57
                $block->setSettings($event->getSettings());
58
                $block->setType($type);
59
60
                $event->addBlock($block);
61
62
                return;
63
            }
64
        }
65
    }
66
}
67