Completed
Pull Request — master (#8491)
by Stefan
18:30 queued 07:04
created

TwigGridRenderer::renderBulkAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\GridBundle\Renderer;
15
16
use Sylius\Bundle\GridBundle\Form\Registry\FormTypeRegistryInterface;
17
use Sylius\Component\Grid\Definition\Action;
18
use Sylius\Component\Grid\Definition\Field;
19
use Sylius\Component\Grid\Definition\Filter;
20
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
21
use Sylius\Component\Grid\Renderer\GridRendererInterface;
22
use Sylius\Component\Grid\View\GridViewInterface;
23
use Sylius\Component\Registry\ServiceRegistryInterface;
24
use Symfony\Component\Form\Extension\Core\Type\FormType;
25
use Symfony\Component\Form\FormFactoryInterface;
26
use Symfony\Component\OptionsResolver\OptionsResolver;
27
28
final class TwigGridRenderer implements GridRendererInterface
29
{
30
    /**
31
     * @var \Twig_Environment
32
     */
33
    private $twig;
34
35
    /**
36
     * @var ServiceRegistryInterface
37
     */
38
    private $fieldsRegistry;
39
40
    /**
41
     * @var FormFactoryInterface
42
     */
43
    private $formFactory;
44
45
    /**
46
     * @var FormTypeRegistryInterface
47
     */
48
    private $formTypeRegistry;
49
50
    /**
51
     * @var string
52
     */
53
    private $defaultTemplate;
54
55
    /**
56
     * @var array
57
     */
58
    private $actionTemplates;
59
60
    /**
61
     * @var array
62
     */
63
    private $filterTemplates;
64
65
    /**
66
     * @var array
67
     */
68
    private $bulkActionTemplates;
69
70
    /**
71
     * @param \Twig_Environment $twig
72
     * @param ServiceRegistryInterface $fieldsRegistry
73
     * @param FormFactoryInterface $formFactory
74
     * @param FormTypeRegistryInterface $formTypeRegistry
75
     * @param string $defaultTemplate
76
     * @param array $actionTemplates
77
     * @param array $filterTemplates
78
     * @param array $bulkActionTemplates
79
     */
80
    public function __construct(
81
        \Twig_Environment $twig,
82
        ServiceRegistryInterface $fieldsRegistry,
83
        FormFactoryInterface $formFactory,
84
        FormTypeRegistryInterface $formTypeRegistry,
85
        string $defaultTemplate,
86
        array $actionTemplates = [],
87
        array $filterTemplates = [],
88
        array $bulkActionTemplates = []
89
    ) {
90
        $this->twig = $twig;
91
        $this->fieldsRegistry = $fieldsRegistry;
92
        $this->formFactory = $formFactory;
93
        $this->formTypeRegistry = $formTypeRegistry;
94
        $this->defaultTemplate = $defaultTemplate;
95
        $this->actionTemplates = $actionTemplates;
96
        $this->filterTemplates = $filterTemplates;
97
        $this->bulkActionTemplates = $bulkActionTemplates;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function render(GridViewInterface $gridView, ?string $template = null)
104
    {
105
        return $this->twig->render($template ?: $this->defaultTemplate, ['grid' => $gridView]);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function renderField(GridViewInterface $gridView, Field $field, $data)
112
    {
113
        /** @var FieldTypeInterface $fieldType */
114
        $fieldType = $this->fieldsRegistry->get($field->getType());
115
        $resolver = new OptionsResolver();
116
        $fieldType->configureOptions($resolver);
117
        $options = $resolver->resolve($field->getOptions());
118
119
        return $fieldType->render($field, $data, $options);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function renderAction(GridViewInterface $gridView, Action $action, $data = null)
126
    {
127
        $type = $action->getType();
128
        if (!isset($this->actionTemplates[$type])) {
129
            throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type));
130
        }
131
132
        return $this->twig->render($this->actionTemplates[$type], [
133
            'grid' => $gridView,
134
            'action' => $action,
135
            'data' => $data,
136
        ]);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function renderBulkAction(GridViewInterface $gridView, Action $bulkAction, $data = null)
143
    {
144
        $type = $bulkAction->getType();
145
        if (!isset($this->bulkActionTemplates[$type])) {
146
            throw new \InvalidArgumentException(sprintf('Missing template for bulk action type "%s".', $type));
147
        }
148
149
        return $this->twig->render($this->bulkActionTemplates[$type], [
150
            'grid' => $gridView,
151
            'action' => $bulkAction,
152
            'data' => $data,
153
        ]);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function renderFilter(GridViewInterface $gridView, Filter $filter)
160
    {
161
        $template = $this->getFilterTemplate($filter);
162
163
        $form = $this->formFactory->createNamed('criteria', FormType::class, [], [
164
            'allow_extra_fields' => true,
165
            'csrf_protection' => false,
166
            'required' => false,
167
        ]);
168
        $form->add(
169
            $filter->getName(),
170
            $this->formTypeRegistry->get($filter->getType(), 'default'),
171
            $filter->getFormOptions()
172
        );
173
174
        $criteria = $gridView->getParameters()->get('criteria', []);
175
        $form->submit($criteria);
176
177
        return $this->twig->render($template, [
178
            'grid' => $gridView,
179
            'filter' => $filter,
180
            'form' => $form->get($filter->getName())->createView(),
181
        ]);
182
    }
183
184
    /**
185
     * @param Filter $filter
186
     *
187
     * @return string
188
     *
189
     * @throws \InvalidArgumentException
190
     */
191
    private function getFilterTemplate(Filter $filter): string
192
    {
193
        $template = $filter->getTemplate();
194
        if (null !== $template) {
195
            return $template;
196
        }
197
198
        $type = $filter->getType();
199
        if (!isset($this->filterTemplates[$type])) {
200
            throw new \InvalidArgumentException(sprintf('Missing template for filter type "%s".', $type));
201
        }
202
203
        return $this->filterTemplates[$type];
204
    }
205
}
206