AbstractDataTablesProvider   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 17
eloc 47
c 6
b 0
f 0
dl 0
loc 155
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A renderRowButtons() 0 17 4
A getCSVExporter() 0 2 1
A wrapContent() 0 2 1
A getEditor() 0 2 1
A renderRow() 0 17 4
A getMethod() 0 2 1
A __construct() 0 4 1
A renderButtons() 0 7 3
A getOptions() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
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 WBW\Bundle\JQuery\DataTablesBundle\Provider;
13
14
use InvalidArgumentException;
15
use Symfony\Component\Routing\Exception\InvalidParameterException;
16
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
17
use Symfony\Component\Routing\Exception\RouteNotFoundException;
18
use Symfony\Component\Routing\RouterInterface;
19
use Symfony\Contracts\Translation\TranslatorInterface;
20
use WBW\Bundle\BootstrapBundle\Renderer\CenterAlignedRendererTrait;
21
use WBW\Bundle\BootstrapBundle\Renderer\RightAlignedRendererTrait;
22
use WBW\Bundle\BootstrapBundle\Renderer\Strings\CenterAlignedTextRendererTrait;
23
use WBW\Bundle\BootstrapBundle\Renderer\Strings\JustifiedAlignedTextRendererTrait;
24
use WBW\Bundle\BootstrapBundle\Renderer\Strings\LeftAlignedTextRendererTrait;
25
use WBW\Bundle\BootstrapBundle\Renderer\Strings\RightAlignedTextRendererTrait;
26
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension;
27
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtensionTrait;
28
use WBW\Bundle\CoreBundle\Routing\RouterTrait;
29
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOptionsInterface;
30
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
31
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory;
32
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesEntityHelper;
33
use WBW\Bundle\JQuery\DataTablesBundle\Renderer\ColumnWidthInterface;
34
use WBW\Bundle\JQuery\DataTablesBundle\Renderer\Floats\FloatRendererTrait;
35
use WBW\Bundle\JQuery\DataTablesBundle\Translation\TranslatorTrait;
36
use WBW\Library\Symfony\Renderer\DateTimesRendererTrait;
37
use WBW\Library\Symfony\Renderer\Strings\StringWrapperTrait;
38
use WBW\Library\Symfony\Renderer\StringsRendererTrait;
39
40
/**
41
 * Abstract DataTables provider.
42
 *
43
 * @author webeweb <https://github.com/webeweb>
44
 * @package WBW\Bundle\JQuery\DataTablesBundle\Provider
45
 * @abstract
46
 */
47
abstract class AbstractDataTablesProvider implements DataTablesProviderInterface, ColumnWidthInterface {
48
49
    use ButtonTwigExtensionTrait;
50
    use RouterTrait;
51
    use TranslatorTrait;
52
53
    use DataTablesButtonsRendererTrait;
54
55
    use DateTimesRendererTrait;
56
    use FloatRendererTrait;
57
    use StringsRendererTrait;
58
    use StringWrapperTrait;
59
60
    use CenterAlignedTextRendererTrait;
61
    use JustifiedAlignedTextRendererTrait;
62
    use LeftAlignedTextRendererTrait;
63
    use RightAlignedTextRendererTrait;
64
65
    use CenterAlignedRendererTrait;
66
    use RightAlignedRendererTrait;
67
68
    /**
69
     * Constructor.
70
     *
71
     * @param RouterInterface $router The router.
72
     * @param TranslatorInterface $translator The translator.
73
     * @param ButtonTwigExtension $buttonTwigExtension The button Twig extension.
74
     */
75
    public function __construct(RouterInterface $router, TranslatorInterface $translator, ButtonTwigExtension $buttonTwigExtension) {
76
        $this->setButtonTwigExtension($buttonTwigExtension);
77
        $this->setRouter($router);
78
        $this->setTranslator($translator);
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getCSVExporter(): ?DataTablesCSVExporterInterface {
85
        return null;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function getEditor(): ?DataTablesEditorInterface {
92
        return null;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function getMethod(): ?string {
99
        return null;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function getOptions(): DataTablesOptionsInterface {
106
107
        $dtOptions = DataTablesFactory::newOptions();
108
        $dtOptions->addOption("order", [[0, "asc"]]);
109
        $dtOptions->addOption("responsive", true);
110
        $dtOptions->addOption("searchDelay", 1000);
111
112
        return $dtOptions;
113
    }
114
115
    /**
116
     * Render the DataTables buttons.
117
     *
118
     * @param object $entity The entity.
119
     * @param string $editRoute The edit route.
120
     * @param string|null $deleteRoute The delete route.
121
     * @param bool $enableDelete Enable delete ?
122
     * @return string Returns the DataTables buttons.
123
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
124
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
125
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
126
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
127
     * @deprecated since 3.4.0 use {@see WBW\Bundle\JQuery\DataTablesBundle\Provider\AbstractDataTablesProvider::renderRowButtons()} instead
128
     */
129
    protected function renderButtons($entity, string $editRoute, string $deleteRoute = null, bool $enableDelete = true): string {
130
131
        if (null === $deleteRoute && true === $enableDelete) {
132
            $deleteRoute = "wbw_jquery_datatables_delete";
133
        }
134
135
        return $this->renderRowButtons($entity, $editRoute, $deleteRoute);
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141
    public function renderRow(string $dtRow, $entity, int $rowNumber) {
142
143
        DataTablesEntityHelper::isCompatible($entity, true);
144
145
        switch ($dtRow) {
146
147
            case DataTablesResponseInterface::DATATABLES_ROW_ATTR:
148
                return ["data-id" => $entity->getId()];
149
150
            case DataTablesResponseInterface::DATATABLES_ROW_DATA:
151
                return ["pkey" => $entity->getId()];
152
153
            case DataTablesResponseInterface::DATATABLES_ROW_ID:
154
                return implode("-", [$this->getName(), $entity->getId()]);
155
        }
156
157
        return null;
158
    }
159
160
    /**
161
     * Render the DataTables row buttons.
162
     *
163
     * @param object $entity The entity.
164
     * @param string|null $editRoute The edit route.
165
     * @param string|null $deleteRoute The delete route.
166
     * @param string|null $showRoute The show route.
167
     * @return string Returns the DataTables row buttons.
168
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
169
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
170
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
171
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
172
     */
173
    protected function renderRowButtons($entity, string $editRoute = null, string $deleteRoute = null, string $showRoute = null): string {
174
175
        $anchors = [];
176
177
        if (null !== $showRoute) {
178
            $anchors[] = $this->renderActionButtonShow($entity, $showRoute);
179
        }
180
181
        if (null !== $editRoute) {
182
            $anchors[] = $this->renderActionButtonEdit($entity, $editRoute);
183
        }
184
185
        if (null !== $deleteRoute) {
186
            $anchors[] = $this->renderActionButtonDelete($entity, $deleteRoute);
187
        }
188
189
        return implode(" ", $anchors);
190
    }
191
192
    /**
193
     * Wrap a content.
194
     *
195
     * @param string|null $prefix The prefix
196
     * @param string $content The content.
197
     * @param string|null $suffix The suffix.
198
     * @return string|null Returns the wrapped content.
199
     */
200
    protected function wrapContent(?string $prefix, string $content, ?string $suffix): ?string {
201
        return $this->wrapString($content, $prefix, $suffix);
202
    }
203
}
204