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

DataTablesController::indexAction()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 8.2286
c 0
b 0
f 0
cc 6
nc 7
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\DBAL\Exception\ForeignKeyConstraintViolationException;
15
use Doctrine\ORM\EntityNotFoundException;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesResponse;
19
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException;
20
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException;
21
22
/**
23
 * DataTables controller.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Bundle\JQuery\DataTablesBundle\Controller
27
 */
28
class DataTablesController extends AbstractDataTablesController {
29
30
    /**
31
     * Delete an existing entity.
32
     *
33
     * @param Request $request The request.
34
     * @param string $name The provider name.
35
     * @param string $id The entity id.
36
     * @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception.
37
     * @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception.
38
     */
39
    public function deleteAction(Request $request, $name, $id) {
40
41
        // Get the provider.
42
        $dtProvider = $this->getDataTablesProvider($name);
43
44
        // Initialize the output.
45
        $output = [
46
            "status" => null,
47
            "notify" => null,
48
        ];
49
50
        try {
51
52
            // Get the entity.
53
            $entity = $this->getDataTablesEntityById($dtProvider, $id);
54
55
            // Get the entities manager and delete the entity.
56
            $em = $this->getDoctrine()->getManager();
57
            $em->remove($entity);
58
            $em->flush();
59
60
            // Set the output.
61
            $output["status"] = 200;
62
            $output["notify"] = $this->getNotification("DataTablesController.deleteAction.success");
63
        } catch (EntityNotFoundException $ex) {
64
65
            // Log a debug trace.
66
            $this->getLogger()->debug($ex->getMessage());
67
68
            // Set the output.
69
            $output["status"] = 404;
70
            $output["notify"] = $this->getNotification("DataTablesController.deleteAction.danger");
71
        } catch (ForeignKeyConstraintViolationException $ex) {
72
73
            // Log a debug trace.
74
            $this->getLogger()->debug(sprintf("%s:%s %s", $ex->getErrorCode(), $ex->getSQLState(), $ex->getMessage()));
75
76
            // Set the output.
77
            $output["status"] = 500;
78
            $output["notify"] = $this->getNotification("DataTablesController.deleteAction.warning");
79
        }
80
81
        // Return the response.
82
        return $this->buildDataTablesResponse($request, $name, $output);
83
    }
84
85
    /**
86
     * Lists all entities.
87
     *
88
     * @param Request $request The request.
89
     * @param string $name The provider name.
90
     * @return Response Returns the response.
91
     * @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception.
92
     * @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception.
93
     */
94
    public function indexAction(Request $request, $name) {
95
96
        // Get the provider.
97
        $dtProvider = $this->getDataTablesProvider($name);
98
99
        // Get the wrapper.
100
        $dtWrapper = $this->getDataTablesWrapper($dtProvider);
101
102
        // Check if the request is an XML HTTP request.
103
        if (false === $request->isXmlHttpRequest()) {
104
105
            // Get and check the provider view.
106
            $dtView = $dtProvider->getView();
107
            if (null === $dtProvider->getView()) {
108
                $dtView = "@JQueryDataTables/DataTables/index.html.twig";
109
            }
110
111
            // Return the response.
112
            return $this->render($dtView, [
113
                    "dtWrapper" => $dtWrapper,
114
            ]);
115
        }
116
117
        // Parse the request.
118
        $dtWrapper->parse($request);
119
120
        // Get the entities repository.
121
        $repository = $this->getDataTablesRepository($dtProvider);
122
123
        //
124
        $filtered = $repository->dataTablesCountFiltered($dtWrapper);
125
        $total    = $repository->dataTablesCountTotal($dtWrapper);
126
        $entities = $repository->dataTablesFindAll($dtWrapper);
127
128
        // Set the response.
129
        $dtWrapper->getResponse()->setRecordsFiltered($filtered);
130
        $dtWrapper->getResponse()->setRecordsTotal($total);
131
132
        // Handle each entity.
133
        foreach ($entities as $entity) {
134
135
            // Count the rows.
136
            $rows = $dtWrapper->getResponse()->countRows();
137
138
            // Create a row.
139
            $dtWrapper->getResponse()->addRow();
140
141
            // Render each optional parameter.
142
            foreach (DataTablesResponse::dtRow() as $dtRow) {
143
                $dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows));
144
            }
145
146
            // Render each column.
147
            foreach ($dtWrapper->getColumns() as $dtColumn) {
148
                $dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity));
149
            }
150
        }
151
152
        // Return the response.
153
        return new Response(json_encode($dtWrapper->getResponse()));
154
    }
155
156
}
157