Completed
Push — master ( cc2b3d...e0d8bb )
by WEBEWEB
01:34
created

buildDataTablesResponse()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
cc 5
nc 5
nop 3
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
namespace WBW\Bundle\JQuery\DataTablesBundle\Controller;
13
14
use Doctrine\ORM\EntityNotFoundException;
15
use Doctrine\ORM\EntityRepository;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use WBW\Bundle\BootstrapBundle\Controller\AbstractBootstrapController;
19
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper;
20
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException;
21
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException;
22
use WBW\Bundle\JQuery\DataTablesBundle\Manager\DataTablesManager;
23
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
24
use WBW\Bundle\JQuery\DataTablesBundle\Repository\DataTablesRepositoryInterface;
25
26
/**
27
 * Abstract jQuery DataTables controller.
28
 *
29
 * @author webeweb <https://github.com/webeweb/>
30
 * @package WBW\Bundle\JQuery\DataTablesBundle\Controller
31
 * @abstract
32
 */
33
abstract class AbstractDataTablesController extends AbstractBootstrapController {
34
35
    /**
36
     * Build a DataTables response.
37
     *
38
     * @param Request $request The request.
39
     * @param string $name The provider name.
40
     * @param array $output The output.
41
     * @return Response Returns the DataTables response.
42
     */
43
    protected function buildDataTablesResponse(Request $request, $name, array $output) {
44
45
        // Determines if the request is an XML HTTP request.
46
        if (true === $request->isXmlHttpRequest()) {
47
48
            // Return the response.
49
            return new Response(json_encode($output));
50
        }
51
52
        // Notify the user.
53
        switch ($output["status"]) {
54
            case 200:
55
                $this->notifySuccess($output["notify"]);
56
                break;
57
            case 404:
58
                $this->notifyDanger($output["notify"]);
59
                break;
60
            case 500:
61
                $this->notifyWarning($output["notify"]);
62
                break;
63
        }
64
65
        // Return the response.
66
        return $this->redirectToRoute("jquery_datatables_index", ["name" => $name]);
67
    }
68
69
    /**
70
     * Get a DataTables entity by id.
71
     *
72
     * @param DataTablesProviderInterface $dtProvider The DataTables provider.
73
     * @param integer $id The entity id.
74
     * @return mixed Returns the DataTables entity.
75
     * @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception.
76
     * @throws EntityNotFoundException Throws an Entity not found exception.
77
     */
78
    protected function getDataTablesEntityById(DataTablesProviderInterface $dtProvider, $id) {
79
80
        // Get the DataTables repository.
81
        $repository = $this->getDataTablesRepository($dtProvider);
82
83
        // Find and check the entity.
84
        $entity = $repository->find($id);
85
        if (null === $entity) {
86
            throw EntityNotFoundException::fromClassNameAndIdentifier($dtProvider->getEntity(), [$id]);
87
        }
88
89
        // Return the DataTables entity.
90
        return $entity;
91
    }
92
93
    /**
94
     * Get the DataTables provider.
95
     *
96
     * @return DataTablesProviderInterface Returns the DataTables provider.
97
     * @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception.
98
     */
99
    protected function getDataTablesProvider($name) {
100
101
        // Log a debug trace.
102
        $this->getLogger()->debug(sprintf("DataTables controller search for a DataTables provider with name \"%s\"", $name));
103
104
        // Get the DataTables provider.
105
        $dtProvider = $this->get(DataTablesManager::SERVICE_NAME)->getProvider($name);
106
107
        // Log a debug trace.
108
        $this->getLogger()->debug(sprintf("DataTables controller found a DataTables provider with name \"%s\"", $name));
109
110
        // Return the DataTables provider.
111
        return $dtProvider;
112
    }
113
114
    /**
115
     * Get a DataTables repository.
116
     *
117
     * @param DataTablesProviderInterface The DataTables provider.
118
     * @return EntityRepository Returns the DataTables repository.
119
     * @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception.
120
     */
121
    protected function getDataTablesRepository(DataTablesProviderInterface $dtProvider) {
122
123
        // Get the entities manager.
124
        $em = $this->getDoctrine()->getManager();
125
126
        // Get and check the entities repository.
127
        $repository = $em->getRepository($dtProvider->getEntity());
128
        if (false === ($repository instanceOf DataTablesRepositoryInterface)) {
129
            throw new BadDataTablesRepositoryException($repository);
130
        }
131
132
        // Return the DataTables repository.
133
        return $repository;
134
    }
135
136
    /**
137
     * Get a DataTables wrapper.
138
     *
139
     * @param DataTablesProviderInterface $dtProvider The DataTables provider.
140
     * @return DataTablesWrapper Returns the DataTables wrapper.
141
     */
142
    protected function getDataTablesWrapper(DataTablesProviderInterface $dtProvider) {
143
144
        // Initialize the URL.
145
        $url = $this->getRouter()->generate("jquery_datatables_index", ["name" => $dtProvider->getName()]);
146
147
        // Initialize the DataTables wrapper.
148
        $dtWrapper = new DataTablesWrapper($dtProvider->getMethod(), $url, $dtProvider->getName());
149
        $dtWrapper->getMapping()->setPrefix($dtProvider->getPrefix());
150
151
        // Handle each column.
152
        foreach ($dtProvider->getColumns() as $dtColumn) {
153
154
            // Log a debug trace.
155
            $this->getLogger()->debug(sprintf("DataTables provider add a DataTables column \"%s\"", $dtColumn->getData()));
156
157
            // Add.
158
            $dtWrapper->addColumn($dtColumn);
159
        }
160
161
        // Return the DataTables wrapper.
162
        return $dtWrapper;
163
    }
164
165
    /**
166
     * Get the notification.
167
     *
168
     * @param string $id The notification id.
169
     * @return string Returns the notification.
170
     */
171
    protected function getNotification($id) {
172
        return $this->getTranslator()->trans($id, [], "JQueryDataTablesBundle");
173
    }
174
175
}
176