• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / jquery-datatables-bundle / 17383071357

01 Sep 2025 04:44PM UTC coverage: 99.734% (-0.08%) from 99.814%
17383071357

push

github

webeweb
Fix unit test

3750 of 3760 relevant lines covered (99.73%)

104.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/Controller/AbstractController.php
1
<?php
2

3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 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
declare(strict_types = 1);
13

14
namespace WBW\Bundle\DataTablesBundle\Controller;
15

16
use Doctrine\ORM\EntityNotFoundException;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Throwable;
21
use WBW\Bundle\BootstrapBundle\Controller\AbstractController as BaseController;
22
use WBW\Bundle\DataTablesBundle\Event\DataTablesEvent;
23
use WBW\Bundle\DataTablesBundle\Helper\DataTablesExportHelper;
24
use WBW\Bundle\DataTablesBundle\Manager\DataTablesManagerTrait;
25
use WBW\Bundle\DataTablesBundle\Model\DataTablesWrapperInterface;
26
use WBW\Bundle\DataTablesBundle\Provider\DataTablesCsvExporterInterface;
27
use WBW\Bundle\DataTablesBundle\Provider\DataTablesProviderInterface;
28
use WBW\Bundle\DataTablesBundle\Repository\DataTablesRepositoryInterface;
29
use WBW\Bundle\DataTablesBundle\Service\DataTablesServiceTrait;
30
use WBW\Bundle\DataTablesBundle\WBWDataTablesBundle;
31
use WBW\Library\Common\Database\Paginator;
32
use WBW\Library\Common\Model\Response\SimpleJsonResponseData;
33
use WBW\Library\Common\Model\Response\SimpleJsonResponseDataInterface;
34

35
/**
36
 * Abstract controller.
37
 *
38
 * @author webeweb <https://github.com/webeweb>
39
 * @package WBW\Bundle\DataTablesBundle\Controller
40
 * @abstract
41
 */
42
abstract class AbstractController extends BaseController {
43

44
    use DataTablesManagerTrait {
45
        setDataTablesManager as public;
46
    }
47

48
    use DataTablesServiceTrait {
49
        setDataTablesService as public;
50
    }
51

52
    /**
53
     * Build a response.
54
     *
55
     * @param Request $request The request.
56
     * @param string $name The provider name.
57
     * @param SimpleJsonResponseDataInterface $output The output.
58
     * @return Response Returns the response.
59
     * @throws Throwable Throws an exception if an error occurs.
60
     */
61
    protected function buildDataTablesResponse(Request $request, string $name, SimpleJsonResponseDataInterface $output): Response {
62

63
        if (true === $request->isXmlHttpRequest()) {
40✔
64
            return new JsonResponse($output);
20✔
65
        }
66

67
        switch ($output->getStatus()) {
20✔
68

69
            case 200:
20✔
70
                $this->notifySuccess($output->getNotify());
10✔
71
                break;
10✔
72

73
            case 404:
10✔
74
                $this->notifyDanger($output->getNotify());
10✔
75
                break;
10✔
76

77
            case 500:
×
78
                $this->notifyWarning($output->getNotify());
×
79
                break;
×
80
        }
81

82
        return $this->redirectToRoute("wbw_datatables_index", ["name" => $name]);
20✔
83
    }
84

85
    /**
86
     * Dispatch an event.
87
     *
88
     * @param object[] $entities The entities.
89
     * @param string $eventName The event name.
90
     * @param DataTablesProviderInterface|null $provider The provider.
91
     * @return DataTablesEvent Returns the event.
92
     * @throws Throwable Throws an exception if an error occurs.
93
     */
94
    protected function dispatchDataTablesEvent(array $entities, string $eventName, ?DataTablesProviderInterface $provider = null): DataTablesEvent {
95

96
        $event = new DataTablesEvent($entities, $eventName, $provider);
160✔
97
        $this->dispatchEvent($event, $eventName);
160✔
98

99
        return $event;
160✔
100
    }
101

102
    /**
103
     * Export callback.
104
     *
105
     * @param DataTablesWrapperInterface $dtWrapper The wrapper.
106
     * @param DataTablesRepositoryInterface $repository The repository.
107
     * @param DataTablesCsvExporterInterface $dtExporter The exporter.
108
     * @param bool $windows Windows ?
109
     * @return void
110
     * @throws Throwable Throws an exception if an error occurs.
111
     */
112
    protected function exportDataTablesCallback(DataTablesWrapperInterface $dtWrapper, DataTablesRepositoryInterface $repository, DataTablesCsvExporterInterface $dtExporter, bool $windows): void {
113

114
        $stream = fopen("php://output", "w+");
10✔
115
        fputcsv($stream, DataTablesExportHelper::convert($dtExporter->exportColumns(), $windows), ";");
10✔
116

117
        // Paginates.
118
        $total = $repository->dataTablesCountExported($dtWrapper);
10✔
119
        $pages = Paginator::countPages($total, DataTablesRepositoryInterface::REPOSITORY_LIMIT);
10✔
120

121
        $em = $this->getEntityManager();
10✔
122

123
        for ($i = 0; $i < $pages; ++$i) {
10✔
124

125
            // Get the offset and limit.
126
            [$offset, $limit] = Paginator::offsetLimit($i, DataTablesRepositoryInterface::REPOSITORY_LIMIT, $total);
10✔
127

128
            // Get the export query with offset and limit.
129
            $query = $repository->dataTablesExportAll($dtWrapper)
10✔
130
                ->setFirstResult($offset)
10✔
131
                ->setMaxResults($limit)
10✔
132
                ->getQuery();
10✔
133

134
            foreach ($query->toIterable() as $entity) {
10✔
135

136
                $this->dispatchDataTablesEvent([$entity], DataTablesEvent::PRE_EXPORT, $dtWrapper->getProvider());
10✔
137

138
                fputcsv($stream, DataTablesExportHelper::convert($dtExporter->exportRow($entity), $windows), ";");
10✔
139

140
                $this->dispatchDataTablesEvent([$entity], DataTablesEvent::POST_EXPORT, $dtWrapper->getProvider());
10✔
141
            }
142

143
            $em->clear(); // Detach the entity to avoid memory consumption.
10✔
144
        }
145

146
        fclose($stream);
10✔
147
    }
1✔
148

149
    /**
150
     * Handle an exception.
151
     *
152
     * @param Throwable $ex The exception.
153
     * @param string $notificationBaseId The notification base id.
154
     * @return SimpleJsonResponseDataInterface Returns the action response.
155
     * @throws Throwable Throws an exception if an error occurs.
156
     */
157
    protected function handleDataTablesException(Throwable $ex, string $notificationBaseId): SimpleJsonResponseDataInterface {
158

159
        $this->logInfo($ex->getMessage());
40✔
160

161
        if (true === ($ex instanceof EntityNotFoundException)) {
40✔
162
            return $this->prepareActionResponse(404, $notificationBaseId . ".danger");
30✔
163
        }
164

165
        return $this->prepareActionResponse(500, $notificationBaseId . ".warning");
10✔
166
    }
167

168
    /**
169
     * Log an info.
170
     *
171
     * @param string $message The message.
172
     * @param mixed[] $context The context.
173
     * @return AbstractController Returns this controller.
174
     * @throws Throwable Throws an exception if an error occurs.
175
     */
176
    protected function logInfo(string $message, array $context = []): AbstractController {
177
        $this->getLogger()->info($message, $context);
60✔
178
        return $this;
60✔
179
    }
180

181
    /**
182
     * Prepare an action response.
183
     *
184
     * @param int $status The status.
185
     * @param string $notificationId The notification id.
186
     * @return SimpleJsonResponseDataInterface Returns the action response.
187
     * @throws Throwable Throws an exception if an error occurs.
188
     */
189
    protected function prepareActionResponse(int $status, string $notificationId): SimpleJsonResponseDataInterface {
190

191
        $notify = $this->getTranslator()->trans($notificationId, [], WBWDataTablesBundle::getTranslationDomain());
70✔
192

193
        $response = new SimpleJsonResponseData();
70✔
194
        $response->setStatus($status);
70✔
195
        $response->setNotify($notify);
70✔
196

197
        return $response;
70✔
198
    }
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc