| Conditions | 8 |
| Paths | 16 |
| Total Lines | 72 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 42 | public function indexAction(Request $request, $name) { |
||
| 43 | |||
| 44 | // Get the provider. |
||
| 45 | $dtProvider = $this->get(DataTablesManager::SERVICE_NAME)->getProvider($name); |
||
| 46 | |||
| 47 | // Initialize the DataTables wrapper. |
||
| 48 | $dtWrapper = new DataTablesWrapper($dtProvider->getPrefix(), HTTPInterface::HTTP_METHOD_POST, "jquery_datatables_index", ["name" => $name]); |
||
| 49 | foreach ($dtProvider->getColumns() as $dtColumn) { |
||
| 50 | $dtWrapper->addColumn($dtColumn); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Check if the request is an XML HTTP request. |
||
| 54 | if (true === $request->isXmlHttpRequest()) { |
||
| 55 | |||
| 56 | // Get the entities manager. |
||
| 57 | $em = $this->getDoctrine()->getManager(); |
||
| 58 | |||
| 59 | // Get and check the entities repository. |
||
| 60 | $repository = $em->getRepository($dtProvider->getEntity()); |
||
| 61 | if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
||
| 62 | throw new BadDataTablesRepositoryException($repository); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Parse the request. |
||
| 66 | $dtWrapper->parse($request); |
||
| 67 | |||
| 68 | // |
||
| 69 | $filtered = $repository->dataTablesCountFiltered($dtWrapper); |
||
| 70 | $total = $repository->dataTablesCountTotal($dtWrapper); |
||
| 71 | $entities = $repository->dataTablesFindAll($dtWrapper); |
||
| 72 | |||
| 73 | // Set the response. |
||
| 74 | $dtWrapper->getResponse()->setRecordsFiltered($filtered); |
||
| 75 | $dtWrapper->getResponse()->setRecordsTotal($total); |
||
| 76 | |||
| 77 | // Handle each entity. |
||
| 78 | foreach ($entities as $entity) { |
||
| 79 | |||
| 80 | // Count the rows. |
||
| 81 | $rows = count($dtWrapper->getResponse()->getData()); |
||
| 82 | |||
| 83 | // Create a row. |
||
| 84 | $dtWrapper->getResponse()->addRow(); |
||
| 85 | |||
| 86 | // Render each optional parameters. |
||
| 87 | foreach (DataTablesResponse::dtRow() as $dtRow) { |
||
| 88 | $dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows)); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Render each column. |
||
| 92 | foreach ($dtWrapper->getColumns() as $dtColumn) { |
||
| 93 | $dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity)); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // Return the response. |
||
| 98 | return new Response(json_encode($dtWrapper->getResponse())); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Initialize the default view. |
||
| 102 | $dtView = "@JQueryDataTables/DataTables/index.html.twig"; |
||
| 103 | |||
| 104 | // Check the provider view. |
||
| 105 | if (null !== $dtProvider->getView()) { |
||
| 106 | $dtView = $dtProvider->getView(); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Return the response. |
||
| 110 | return $this->render($dtView, [ |
||
| 111 | "dtWrapper" => $dtWrapper, |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | |||
| 116 |