Completed
Pull Request — 2.x (#246)
by Christian
03:32
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 Sonata\AdminBundle\Form\FormMapper;
17
use Sonata\BlockBundle\Block\BlockContextInterface;
18
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
19
use Sonata\BlockBundle\Model\BlockInterface;
20
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
21
use Sonata\CoreBundle\Model\Metadata;
22
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
23
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
24
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
25
use Symfony\Component\Form\Extension\Core\Type\TextType;
26
use Symfony\Component\HttpFoundation\Response;
27
use Symfony\Component\OptionsResolver\OptionsResolver;
28
29
/**
30
 * Abstract class for breadcrumb menu services.
31
 *
32
 * @author Sylvain Deloux <[email protected]>
33
 * @author Christian Gripp <[email protected]>
34
 */
35
abstract class AbstractBreadcrumbMenuService extends AbstractBlockService
36
{
37
    /**
38
     * @var string
39
     */
40
    private $context;
41
42
    /**
43
     * @var FactoryInterface
44
     */
45
    private $factory;
46
47
    /**
48
     * @param string           $name
49
     * @param string           $context
50
     * @param EngineInterface  $templating
51
     * @param FactoryInterface $factory
52
     */
53
    public function __construct($name, $context, EngineInterface $templating, FactoryInterface $factory)
54
    {
55
        parent::__construct($name, $templating);
56
57
        $this->context = $context;
58
        $this->factory = $factory;
59
    }
60
61
    /**
62
     * Return true if current BlockService handles the given context.
63
     *
64
     * @param string $context
65
     *
66
     * @return bool
67
     */
68
    public function handleContext($context)
69
    {
70
        return $this->context === $context;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function execute(BlockContextInterface $blockContext, Response $response = null)
77
    {
78
        $responseSettings = [
79
            'menu' => $this->getMenu($blockContext),
80
            'menu_options' => $this->getMenuOptions($blockContext->getSettings()),
81
            'block' => $blockContext->getBlock(),
82
            'context' => $blockContext,
83
        ];
84
85
        if ('private' === $blockContext->getSetting('cache_policy')) {
86
            return $this->renderPrivateResponse($blockContext->getTemplate(), $responseSettings, $response);
87
        }
88
89
        return $this->renderResponse($blockContext->getTemplate(), $responseSettings, $response);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function buildEditForm(FormMapper $form, BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        $form->add('settings', ImmutableArrayType::class, [
98
            'keys' => $this->getFormSettingsKeys(),
99
        ]);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getBlockMetadata($code = null)
106
    {
107
        return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataBlockBundle', [
108
            'class' => 'fa fa-bars',
109
        ]);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function configureSettings(OptionsResolver $resolver)
116
    {
117
        $resolver->setDefaults([
118
            'title' => $this->getName(),
119
            'cache_policy' => 'public',
120
            'template' => 'SonataBlockBundle:Block:block_core_menu.html.twig',
121
            'menu_name' => '',
122
            'safe_labels' => false,
123
            'current_class' => 'active',
124
            'first_class' => false,
125
            'last_class' => false,
126
            'current_uri' => null,
127
            'menu_class' => 'list-group',
128
            'children_class' => 'list-group-item',
129
            'menu_template' => 'SonataSeoBundle:Block:breadcrumb.html.twig',
130
            'include_homepage_link' => true,
131
            'context' => false,
132
        ]);
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    protected function getFormSettingsKeys()
139
    {
140
        return [
141
            ['title', TextType::class, ['required' => false]],
142
            ['cache_policy', ChoiceType::class, ['choices' => ['public', 'private']]],
143
            ['safe_labels', CheckboxType::class, ['required' => false]],
144
            ['current_class', TextType::class, ['required' => false]],
145
            ['first_class', TextType::class, ['required' => false]],
146
            ['last_class', TextType::class, ['required' => false]],
147
            ['menu_class', TextType::class, ['required' => false]],
148
            ['children_class', TextType::class, ['required' => false]],
149
            ['menu_template', TextType::class, ['required' => false]],
150
        ];
151
    }
152
153
    /**
154
     * Gets the menu to render.
155
     *
156
     * @param BlockContextInterface $blockContext
157
     *
158
     * @return ItemInterface|string
159
     */
160
    protected function getMenu(BlockContextInterface $blockContext)
161
    {
162
        return $this->getRootMenu($blockContext);
163
    }
164
165
    /**
166
     * Replaces setting keys with knp menu item options keys.
167
     *
168
     * @param array $settings
169
     *
170
     * @return array
171
     */
172
    protected function getMenuOptions(array $settings)
173
    {
174
        $mapping = [
175
            'current_class' => 'currentClass',
176
            'first_class' => 'firstClass',
177
            'last_class' => 'lastClass',
178
            'safe_labels' => 'allow_safe_labels',
179
            'menu_template' => 'template',
180
        ];
181
182
        $options = [];
183
184
        foreach ($settings as $key => $value) {
185
            if (array_key_exists($key, $mapping) && null !== $value) {
186
                $options[$mapping[$key]] = $value;
187
            }
188
        }
189
190
        return $options;
191
    }
192
193
    /**
194
     * @return FactoryInterface
195
     */
196
    protected function getFactory()
197
    {
198
        return $this->factory;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    protected function getContext()
205
    {
206
        return $this->context;
207
    }
208
209
    /**
210
     * Initialize breadcrumb menu.
211
     *
212
     * @param BlockContextInterface $blockContext
213
     *
214
     * @return ItemInterface
215
     */
216
    protected function getRootMenu(BlockContextInterface $blockContext)
217
    {
218
        $settings = $blockContext->getSettings();
219
        /*
220
         * @todo : Use the router to get the homepage URI
221
         */
222
223
        $menu = $this->factory->createItem('breadcrumb');
224
225
        $menu->setChildrenAttribute('class', 'breadcrumb');
226
227
        if (method_exists($menu, 'setCurrentUri')) {
228
            $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...
229
        }
230
231
        if (method_exists($menu, 'setCurrent')) {
232
            $menu->setCurrent($settings['current_uri']);
233
        }
234
235
        if ($settings['include_homepage_link']) {
236
            $menu->addChild('sonata_seo_homepage_breadcrumb', ['uri' => '/']);
237
        }
238
239
        return $menu;
240
    }
241
}
242