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