Completed
Push — master ( 6564f1...dbc0ff )
by WEBEWEB
11:12
created

DataTablesController::indexAction()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 64
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 64
rs 7.2058
cc 7
eloc 26
nc 12
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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use WBW\Bundle\JQuery\DatatablesBundle\Exception\BadDataTablesRepositoryException;
18
use WBW\Bundle\JQuery\DatatablesBundle\Exception\UnregisteredDataTablesProviderException;
19
use WBW\Bundle\JQuery\DatatablesBundle\Manager\DataTablesManager;
20
use WBW\Bundle\JQuery\DatatablesBundle\Repository\DataTablesRepositoryInterface;
21
use WBW\Bundle\JQuery\DatatablesBundle\Wrapper\DataTablesWrapper;
22
use WBW\Library\Core\IO\HTTPInterface;
23
24
/**
25
 * DataTables controller.
26
 *
27
 * @author webeweb <https://github.com/webeweb/>
28
 * @package WBW\Bundle\JQuery\DatatablesBundle\Controller
29
 * @final
30
 */
31
final class DataTablesController extends Controller {
32
33
    /**
34
     * Lists all entities.
35
     *
36
     * @param Request $request The request.
37
     * @param string $name The provider name.
38
     * @return Response Returns the response.
39
     * @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception.
40
     */
41
    public function indexAction(Request $request, $name) {
42
43
        // Get the provider.
44
        $dtProvider = $this->get(DataTablesManager::SERVICE_NAME)->getProvider($name);
45
46
        // Initialize the DataTables wrapper.
47
        $dtWrapper = new DataTablesWrapper($dtProvider->getPrefix(), HTTPInterface::HTTP_METHOD_POST, "jquery_datatables_index", ["name" => $name]);
48
        foreach ($dtProvider->getColumns() as $dtColumn) {
49
            $dtWrapper->addColumn($dtColumn);
50
        }
51
52
        // Check if the request is an XML HTTP request.
53
        if (true === $request->isXmlHttpRequest()) {
54
55
            // Get the entities manager.
56
            $em = $this->getDoctrine()->getManager();
57
58
            // Get and check the entities repository.
59
            $repository = $em->getRepository($dtProvider->getEntity());
60
            if (false === ($repository instanceOf DataTablesRepositoryInterface)) {
61
                throw new BadDataTablesRepositoryException($repository);
62
            }
63
64
            // Parse the request.
65
            $dtWrapper->parse($request);
66
67
            //
68
            $filtered = $repository->dataTablesCountFiltered($dtWrapper);
69
            $total    = $repository->dataTablesCountTotal($dtWrapper);
70
            $entities = $repository->dataTablesFindAll($dtWrapper);
71
72
            // Set the response.
73
            $dtWrapper->getResponse()->setRecordsFiltered($filtered);
74
            $dtWrapper->getResponse()->setRecordsTotal($total);
75
76
            // Handle each entity.
77
            foreach ($entities as $entity) {
78
79
                // Create the row.
80
                $dtWrapper->getResponse()->addRow();
81
82
                // Render each column.
83
                foreach ($dtWrapper->getColumns() as $column) {
84
                    $dtWrapper->getResponse()->setRow($column->getName(), $dtProvider->render($column, $entity));
85
                }
86
            }
87
88
            // Return the response.
89
            return new Response(json_encode($dtWrapper->getResponse()));
90
        }
91
92
        // Initialize the default view.
93
        $dtView = "@JQueryDataTables/DataTables/index.html.twig";
94
95
        // Check the provider view.
96
        if (null !== $dtProvider->getView()) {
97
            $dtView = $dtProvider->getView();
98
        }
99
100
        // Return the response.
101
        return $this->render($dtView, [
102
                "dtWrapper" => $dtWrapper,
103
        ]);
104
    }
105
106
}
107