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

BaseBreadcrumbMenuBlockService::getRootMenu()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 25
rs 8.5806
cc 4
eloc 11
nc 8
nop 1
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\Block\Breadcrumb;
13
14
use Knp\Menu\FactoryInterface;
15
use Knp\Menu\ItemInterface;
16
use Knp\Menu\Provider\MenuProviderInterface;
17
use Sonata\BlockBundle\Block\BlockContextInterface;
18
use Sonata\BlockBundle\Block\Service\MenuBlockService;
19
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
/**
23
 * Abstract class for breadcrumb menu services.
24
 *
25
 * @author Sylvain Deloux <[email protected]>
26
 */
27
abstract class BaseBreadcrumbMenuBlockService extends MenuBlockService
28
{
29
    /**
30
     * @var string
31
     */
32
    private $context;
33
34
    /**
35
     * @var FactoryInterface
36
     */
37
    private $factory;
38
39
    /**
40
     * @param string                $context
41
     * @param string                $name
42
     * @param EngineInterface       $templating
43
     * @param MenuProviderInterface $menuProvider
44
     * @param FactoryInterface      $factory
45
     */
46
    public function __construct($context, $name, EngineInterface $templating, MenuProviderInterface $menuProvider, FactoryInterface $factory)
47
    {
48
        parent::__construct($name, $templating, $menuProvider, array());
49
50
        $this->context = $context;
51
        $this->factory = $factory;
52
    }
53
54
    /**
55
     * Return true if current BlockService handles the given context.
56
     *
57
     * @param string $context
58
     *
59
     * @return bool
60
     */
61
    public function handleContext($context)
62
    {
63
        return $this->context === $context;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getName()
70
    {
71
        return sprintf('Breadcrumb %s', $this->context);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function configureSettings(OptionsResolver $resolver)
78
    {
79
        parent::configureSettings($resolver);
80
81
        $resolver->setDefaults(array(
82
            'menu_template' => 'SonataSeoBundle:Block:breadcrumb.html.twig',
83
            'include_homepage_link' => true,
84
            'context' => false,
85
        ));
86
    }
87
88
    /**
89
     * @return FactoryInterface
90
     */
91
    protected function getFactory()
92
    {
93
        return $this->factory;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    protected function getContext()
100
    {
101
        return $this->context;
102
    }
103
104
    /**
105
     * Initialize breadcrumb menu.
106
     *
107
     * @param BlockContextInterface $blockContext
108
     *
109
     * @return ItemInterface
110
     */
111
    protected function getRootMenu(BlockContextInterface $blockContext)
112
    {
113
        $settings = $blockContext->getSettings();
114
        /*
115
         * @todo : Use the router to get the homepage URI
116
         */
117
118
        $menu = $this->factory->createItem('breadcrumb');
119
120
        $menu->setChildrenAttribute('class', 'breadcrumb');
121
122
        if (method_exists($menu, 'setCurrentUri')) {
123
            $menu->setCurrentUri($settings['current_uri']);
0 ignored issues
show
Bug introduced by
The method setCurrentUri() does not exist on Knp\Menu\ItemInterface. Did you maybe mean setCurrent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
124
        }
125
126
        if (method_exists($menu, 'setCurrent')) {
127
            $menu->setCurrent($settings['current_uri']);
128
        }
129
130
        if ($settings['include_homepage_link']) {
131
            $menu->addChild('sonata_seo_homepage_breadcrumb', array('uri' => '/'));
132
        }
133
134
        return $menu;
135
    }
136
}
137