Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminListBundle/Twig/AdminListTwigExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Twig;
4
5
use Kunstmaan\AdminListBundle\AdminList\AdminList;
6
use Kunstmaan\AdminListBundle\Service\ExportService;
7
use Twig\Environment;
8
use Twig\Error\LoaderError;
9
use Twig\Error\RuntimeError;
10
use Twig\Error\SyntaxError;
11
use Twig\Extension\AbstractExtension;
12
use Twig\TwigFunction;
13
use Twig\TwigTest;
14
15
/**
16
 * AdminListTwigExtension
17
 *
18
 * @final since 5.4
19
 */
20
class AdminListTwigExtension extends AbstractExtension
21
{
22
    /**
23
     * Returns a list of functions to add to the existing list.
24
     *
25
     * @return array An array of functions
26
     */
27 View Code Duplication
    public function getFunctions()
28
    {
29
        return [
30
            new TwigFunction('adminlist_widget', [$this, 'renderWidget'], ['needs_environment' => true, 'is_safe' => ['html']]),
31
            new TwigFunction('adminthumb_widget', [$this, 'renderThumbWidget'], ['needs_environment' => true, 'is_safe' => ['html']]),
32
            new TwigFunction('supported_export_extensions', [$this, 'getSupportedExtensions']),
33
        ];
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getTests()
40
    {
41
        return [
42
            new TwigTest('instanceof', [$this, 'isInstanceOf']),
43
        ];
44
    }
45
46
    /**
47
     * @param object $object
48
     * @param string $class
49
     *
50
     * @return bool
51
     */
52
    public function isInstanceOf($object, $class)
53
    {
54
        return $object instanceof $class;
55
    }
56
57
    /**
58
     * Renders the HTML for a given view
59
     *
60
     * Example usage in Twig:
61
     *
62
     *     {{ form_widget(view) }}
63
     *
64
     * You can pass options during the call:
65
     *
66
     *     {{ form_widget(view, {'attr': {'class': 'foo'}}) }}
67
     *
68
     *     {{ form_widget(view, {'separator': '+++++'}) }}
69
     *
70
     * @param AdminList $view      The view to render
71
     * @param string    $basepath  The base path
72
     * @param array     $urlparams Additional url params
73
     * @param array     $addparams Add params
74
     *
75
     * @return string The html markup
76
     *
77
     * @throws \Throwable
78
     * @throws LoaderError
79
     * @throws RuntimeError
80
     * @throws SyntaxError
81
     */
82 View Code Duplication
    public function renderWidget(Environment $env, AdminList $view, $basepath, array $urlparams = [], array $addparams = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $filterBuilder = $view->getFilterBuilder();
85
86
        return $env->render(
87
            '@KunstmaanAdminList/AdminListTwigExtension/widget.html.twig',
88
            [
89
                'filter' => $filterBuilder,
90
                'basepath' => $basepath,
91
                'addparams' => $addparams,
92
                'extraparams' => $urlparams,
93
                'adminlist' => $view,
94
            ]
95
        );
96
    }
97
98
    /**
99
     * Renders the HTML for a given view
100
     *
101
     * Example usage in Twig:
102
     *
103
     *     {{ form_widget(view) }}
104
     *
105
     * You can pass options during the call:
106
     *
107
     *     {{ form_widget(view, {'attr': {'class': 'foo'}}) }}
108
     *
109
     *     {{ form_widget(view, {'separator': '+++++'}) }}
110
     *
111
     * @param AdminList $view      The view to render
112
     * @param string    $basepath  The base path
113
     * @param array     $urlparams Additional url params
114
     * @param array     $addparams Add params
115
     *
116
     * @return string The html markup
117
     *
118
     * @throws \Throwable
119
     * @throws LoaderError
120
     * @throws RuntimeError
121
     * @throws SyntaxError
122
     */
123 View Code Duplication
    public function renderThumbWidget(Environment $env, AdminList $view, $basepath, array $urlparams = [], array $addparams = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $filterBuilder = $view->getFilterBuilder();
126
127
        return $env->render(
128
            '@KunstmaanAdminList/AdminListTwigExtension/thumbwidget.html.twig',
129
            [
130
                'filter' => $filterBuilder,
131
                'basepath' => $basepath,
132
                'addparams' => $addparams,
133
                'extraparams' => $urlparams,
134
                'adminlist' => $view,
135
            ]
136
        );
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function getSupportedExtensions()
143
    {
144
        return ExportService::getSupportedExtensions();
145
    }
146
}
147