| Conditions | 7 |
| Paths | 8 |
| Total Lines | 69 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 98 | public function indexAction(Request $request, $name) { |
||
| 99 | |||
| 100 | // Get the provider. |
||
| 101 | $dtProvider = $this->getDataTablesProvider($name); |
||
| 102 | |||
| 103 | // Get the wrapper. |
||
| 104 | $dtWrapper = $this->getDataTablesWrapper($dtProvider); |
||
| 105 | |||
| 106 | // Check if the request is an XML HTTP request. |
||
| 107 | if (true === $request->isXmlHttpRequest()) { |
||
| 108 | |||
| 109 | // Get the entities manager. |
||
| 110 | $em = $this->getDoctrine()->getManager(); |
||
| 111 | |||
| 112 | // Get and check the entities repository. |
||
| 113 | $repository = $em->getRepository($dtProvider->getEntity()); |
||
| 114 | if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
||
| 115 | throw new BadDataTablesRepositoryException($repository); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Parse the request. |
||
| 119 | $dtWrapper->parse($request); |
||
| 120 | |||
| 121 | // |
||
| 122 | $filtered = $repository->dataTablesCountFiltered($dtWrapper); |
||
| 123 | $total = $repository->dataTablesCountTotal($dtWrapper); |
||
| 124 | $entities = $repository->dataTablesFindAll($dtWrapper); |
||
| 125 | |||
| 126 | // Set the response. |
||
| 127 | $dtWrapper->getResponse()->setRecordsFiltered($filtered); |
||
| 128 | $dtWrapper->getResponse()->setRecordsTotal($total); |
||
| 129 | |||
| 130 | // Handle each entity. |
||
| 131 | foreach ($entities as $entity) { |
||
| 132 | |||
| 133 | // Count the rows. |
||
| 134 | $rows = $dtWrapper->getResponse()->countRows(); |
||
| 135 | |||
| 136 | // Create a row. |
||
| 137 | $dtWrapper->getResponse()->addRow(); |
||
| 138 | |||
| 139 | // Render each optional parameters. |
||
| 140 | foreach (DataTablesResponse::dtRow() as $dtRow) { |
||
| 141 | $dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows)); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Render each column. |
||
| 145 | foreach ($dtWrapper->getColumns() as $dtColumn) { |
||
| 146 | $dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity)); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Return the response. |
||
| 151 | return new Response(json_encode($dtWrapper->getResponse())); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Initialize the default view. |
||
| 155 | $dtView = "@JQueryDataTables/DataTables/index.html.twig"; |
||
| 156 | |||
| 157 | // Check the provider view. |
||
| 158 | if (null !== $dtProvider->getView()) { |
||
| 159 | $dtView = $dtProvider->getView(); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Return the response. |
||
| 163 | return $this->render($dtView, [ |
||
| 164 | "dtWrapper" => $dtWrapper, |
||
| 165 | ]); |
||
| 166 | } |
||
| 167 | |||
| 169 |