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

AbstractBreadcrumbMenuService::buildEditForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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\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 FactoryInterface
39
     */
40
    private $factory;
41
42
    /**
43
     * @param string           $name
44
     * @param EngineInterface  $templating
45
     * @param FactoryInterface $factory
46
     */
47
    public function __construct($name, EngineInterface $templating, FactoryInterface $factory)
48
    {
49
        parent::__construct($name, $templating);
50
51
        $this->factory = $factory;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function execute(BlockContextInterface $blockContext, Response $response = null)
58
    {
59
        $responseSettings = [
60
            'menu' => $this->getMenu($blockContext),
61
            'menu_options' => $this->getMenuOptions($blockContext->getSettings()),
62
            'block' => $blockContext->getBlock(),
63
            'context' => $blockContext,
64
        ];
65
66
        if ('private' === $blockContext->getSetting('cache_policy')) {
67
            return $this->renderPrivateResponse($blockContext->getTemplate(), $responseSettings, $response);
68
        }
69
70
        return $this->renderResponse($blockContext->getTemplate(), $responseSettings, $response);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    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...
77
    {
78
        $form->add('settings', ImmutableArrayType::class, [
79
            'keys' => $this->getFormSettingsKeys(),
80
        ]);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getBlockMetadata($code = null)
87
    {
88
        return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataBlockBundle', [
89
            'class' => 'fa fa-bars',
90
        ]);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function configureSettings(OptionsResolver $resolver)
97
    {
98
        $resolver->setDefaults([
99
            'title' => $this->getName(),
100
            'cache_policy' => 'public',
101
            'template' => 'SonataBlockBundle:Block:block_core_menu.html.twig',
102
            'menu_name' => '',
103
            'safe_labels' => false,
104
            'current_class' => 'active',
105
            'first_class' => false,
106
            'last_class' => false,
107
            'current_uri' => null,
108
            'menu_class' => 'list-group',
109
            'children_class' => 'list-group-item',
110
            'menu_template' => 'SonataSeoBundle:Block:breadcrumb.html.twig',
111
            'include_homepage_link' => true,
112
            'context' => false,
113
        ]);
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    protected function getFormSettingsKeys()
120
    {
121
        return [
122
            ['title', TextType::class, ['required' => false]],
123
            ['cache_policy', ChoiceType::class, ['choices' => ['public', 'private']]],
124
            ['safe_labels', CheckboxType::class, ['required' => false]],
125
            ['current_class', TextType::class, ['required' => false]],
126
            ['first_class', TextType::class, ['required' => false]],
127
            ['last_class', TextType::class, ['required' => false]],
128
            ['menu_class', TextType::class, ['required' => false]],
129
            ['children_class', TextType::class, ['required' => false]],
130
            ['menu_template', TextType::class, ['required' => false]],
131
        ];
132
    }
133
134
    /**
135
     * Gets the menu to render.
136
     *
137
     * @param BlockContextInterface $blockContext
138
     *
139
     * @return ItemInterface|string
140
     */
141
    protected function getMenu(BlockContextInterface $blockContext)
142
    {
143
        return $this->getRootMenu($blockContext);
144
    }
145
146
    /**
147
     * Replaces setting keys with knp menu item options keys.
148
     *
149
     * @param array $settings
150
     *
151
     * @return array
152
     */
153
    protected function getMenuOptions(array $settings)
154
    {
155
        $mapping = [
156
            'current_class' => 'currentClass',
157
            'first_class' => 'firstClass',
158
            'last_class' => 'lastClass',
159
            'safe_labels' => 'allow_safe_labels',
160
            'menu_template' => 'template',
161
        ];
162
163
        $options = [];
164
165
        foreach ($settings as $key => $value) {
166
            if (array_key_exists($key, $mapping) && null !== $value) {
167
                $options[$mapping[$key]] = $value;
168
            }
169
        }
170
171
        return $options;
172
    }
173
174
    /**
175
     * @return FactoryInterface
176
     */
177
    protected function getFactory()
178
    {
179
        return $this->factory;
180
    }
181
182
    /**
183
     * Initialize breadcrumb menu.
184
     *
185
     * @param BlockContextInterface $blockContext
186
     *
187
     * @return ItemInterface
188
     */
189
    protected function getRootMenu(BlockContextInterface $blockContext)
190
    {
191
        $settings = $blockContext->getSettings();
192
        /*
193
         * @todo : Use the router to get the homepage URI
194
         */
195
196
        $menu = $this->factory->createItem('breadcrumb');
197
198
        $menu->setChildrenAttribute('class', 'breadcrumb');
199
200
        if (method_exists($menu, 'setCurrentUri')) {
201
            $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...
202
        }
203
204
        if (method_exists($menu, 'setCurrent')) {
205
            $menu->setCurrent($settings['current_uri']);
206
        }
207
208
        if ($settings['include_homepage_link']) {
209
            $menu->addChild('sonata_seo_homepage_breadcrumb', ['uri' => '/']);
210
        }
211
212
        return $menu;
213
    }
214
}
215