Completed
Push — master ( 2f5e70...3f2a58 )
by Paweł
21:05 queued 11:13
created

TwigGridRenderer::renderFilter()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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