Completed
Pull Request — 2.x (#246)
by Christian
01:20
created

AbstractBreadcrumbMenuService::getRootMenu()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
 * @author Christian Gripp <[email protected]>
27
 */
28
abstract class AbstractBreadcrumbMenuService extends MenuBlockService
29
{
30
    /**
31
     * @var string
32
     */
33
    private $context;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $factory;
39
40
    /**
41
     * @param string                $name
42
     * @param string                $context
43
     * @param EngineInterface       $templating
44
     * @param MenuProviderInterface $menuProvider
45
     * @param FactoryInterface      $factory
46
     */
47
    public function __construct($name, $context, EngineInterface $templating, MenuProviderInterface $menuProvider, FactoryInterface $factory)
48
    {
49
        parent::__construct($name, $templating, $menuProvider);
50
51
        $this->context = $context;
52
        $this->factory = $factory;
53
    }
54
55
    /**
56
     * Return true if current BlockService handles the given context.
57
     *
58
     * @param string $context
59
     *
60
     * @return bool
61
     */
62
    public function handleContext($context)
63
    {
64
        return $this->context === $context;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function configureSettings(OptionsResolver $resolver)
71
    {
72
        parent::configureSettings($resolver);
73
74
        $resolver->setDefaults([
75
            'menu_template' => 'SonataSeoBundle:Block:breadcrumb.html.twig',
76
            'include_homepage_link' => true,
77
            'context' => false,
78
        ]);
79
    }
80
81
    /**
82
     * @return FactoryInterface
83
     */
84
    protected function getFactory()
85
    {
86
        return $this->factory;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected function getContext()
93
    {
94
        return $this->context;
95
    }
96
97
    /**
98
     * Initialize breadcrumb menu.
99
     *
100
     * @param BlockContextInterface $blockContext
101
     *
102
     * @return ItemInterface
103
     */
104
    protected function getRootMenu(BlockContextInterface $blockContext)
105
    {
106
        $settings = $blockContext->getSettings();
107
        /*
108
         * @todo : Use the router to get the homepage URI
109
         */
110
111
        $menu = $this->factory->createItem('breadcrumb');
112
113
        $menu->setChildrenAttribute('class', 'breadcrumb');
114
115
        if (method_exists($menu, 'setCurrentUri')) {
116
            $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...
117
        }
118
119
        if (method_exists($menu, 'setCurrent')) {
120
            $menu->setCurrent($settings['current_uri']);
121
        }
122
123
        if ($settings['include_homepage_link']) {
124
            $menu->addChild('sonata_seo_homepage_breadcrumb', ['uri' => '/']);
125
        }
126
127
        return $menu;
128
    }
129
}
130