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