Completed
Push — 2.x-dev-kit ( 971030 )
by
unknown
07:52
created

BreadcrumbListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 5
c 3
b 1
f 1
lcom 1
cbo 2
dl 0
loc 45
rs 10

2 Methods

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