| Conditions | 7 |
| Paths | 12 |
| Total Lines | 64 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 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 | |||
| 107 |