Completed
Push — master ( 490edd...989494 )
by Kamil
49:24 queued 30:26
created

TwigGridRenderer::getFilterTemplate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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