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

BreadcrumbListener::addBlockService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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