BaseBreadcrumbMenuBlockService::getFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Block\Breadcrumb;
15
16
use Knp\Menu\FactoryInterface;
17
use Knp\Menu\ItemInterface;
18
use Sonata\BlockBundle\Block\BlockContextInterface;
19
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
use Twig\Environment;
22
23
/**
24
 * Abstract class for breadcrumb menu services.
25
 *
26
 * @author Sylvain Deloux <[email protected]>
27
 */
28
abstract class BaseBreadcrumbMenuBlockService extends AbstractBlockService
29
{
30
    /**
31
     * @var FactoryInterface
32
     */
33
    private $factory;
34
35
    public function __construct(Environment $twig, FactoryInterface $factory)
36
    {
37
        parent::__construct($twig);
38
39
        $this->factory = $factory;
40
    }
41
42
    /**
43
     * Return true if current BlockService handles the given context.
44
     *
45
     * @param string $context
46
     *
47
     * @return bool
48
     */
49
    public function handleContext($context)
50
    {
51
        return $this->getContext() === $context;
52
    }
53
54
    public function configureSettings(OptionsResolver $resolver): void
55
    {
56
        parent::configureSettings($resolver);
57
58
        $resolver->setDefaults([
59
            'menu_template' => '@SonataSeo/Block/breadcrumb.html.twig',
60
            'include_homepage_link' => true,
61
            'context' => false,
62
        ]);
63
    }
64
65
    protected function getFactory(): FactoryInterface
66
    {
67
        return $this->factory;
68
    }
69
70
    abstract protected function getContext(): string;
71
72
    /**
73
     * Initialize breadcrumb menu.
74
     */
75
    protected function getRootMenu(BlockContextInterface $blockContext): ItemInterface
76
    {
77
        $settings = $blockContext->getSettings();
78
        /*
79
         * @todo : Use the router to get the homepage URI
80
         */
81
82
        $menu = $this->factory->createItem('breadcrumb');
83
84
        $menu->setChildrenAttribute('class', 'breadcrumb');
85
86
        if (method_exists($menu, 'setCurrentUri')) {
87
            $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...
88
        }
89
90
        if (method_exists($menu, 'setCurrent')) {
91
            $menu->setCurrent($settings['current_uri']);
92
        }
93
94
        if ($settings['include_homepage_link']) {
95
            $menu->addChild('sonata_seo_homepage_breadcrumb', ['uri' => '/']);
96
        }
97
98
        return $menu;
99
    }
100
}
101