Complex classes like QueryBuilderEngine often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilderEngine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class QueryBuilderEngine extends BaseEngine implements DataTableEngineContract |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 24 | * @param \Yajra\Datatables\Request $request |
||
| 25 | */ |
||
| 26 | public function __construct(Builder $builder, Request $request) |
||
| 27 | { |
||
| 28 | $this->query = $builder; |
||
| 29 | $this->init($request, $builder); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Initialize attributes. |
||
| 34 | * |
||
| 35 | * @param \Yajra\Datatables\Request $request |
||
| 36 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 37 | * @param string $type |
||
| 38 | */ |
||
| 39 | protected function init($request, $builder, $type = 'builder') |
||
|
|
|||
| 40 | { |
||
| 41 | $this->request = $request; |
||
| 42 | $this->query_type = $type; |
||
| 43 | $this->columns = $builder->columns; |
||
| 44 | $this->connection = $builder->getConnection(); |
||
| 45 | $this->prefix = $this->connection->getTablePrefix(); |
||
| 46 | $this->database = $this->connection->getDriverName(); |
||
| 47 | if ($this->isDebugging()) { |
||
| 48 | $this->connection->enableQueryLog(); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritdoc |
||
| 54 | */ |
||
| 55 | public function filter(Closure $callback) |
||
| 56 | { |
||
| 57 | $this->overrideGlobalSearch($callback, $this->query); |
||
| 58 | |||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @inheritdoc |
||
| 64 | */ |
||
| 65 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | public function totalCount() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Counts current query. |
||
| 80 | * |
||
| 81 | * @return int |
||
| 82 | */ |
||
| 83 | public function count() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Perform global search. |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function filtering() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get eager loads keys if eloquent. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | private function getEagerLoads() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Perform filter column on selected field. |
||
| 164 | * |
||
| 165 | * @param mixed $query |
||
| 166 | * @param string $method |
||
| 167 | * @param mixed $parameters |
||
| 168 | * @param string $column |
||
| 169 | * @param string $keyword |
||
| 170 | */ |
||
| 171 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Build Query Builder Parameters. |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | protected function parameterize() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Add relation query on global search. |
||
| 209 | * |
||
| 210 | * @param mixed $query |
||
| 211 | * @param string $relation |
||
| 212 | * @param string $column |
||
| 213 | * @param string $keyword |
||
| 214 | */ |
||
| 215 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Add a query on global search. |
||
| 228 | * |
||
| 229 | * @param mixed $query |
||
| 230 | * @param string $column |
||
| 231 | * @param string $keyword |
||
| 232 | */ |
||
| 233 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Wrap a column and cast in pgsql. |
||
| 247 | * |
||
| 248 | * @param string $column |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function castColumn($column) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Perform column search. |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function columnSearch() |
||
| 267 | { |
||
| 268 | $columns = $this->request->get('columns'); |
||
| 269 | for ($i = 0, $c = count($columns); $i < $c; $i++) { |
||
| 270 | if ($this->request->isColumnSearchable($i)) { |
||
| 271 | $column = $this->setupColumnName($i); |
||
| 272 | $keyword = $this->getSearchKeyword($i); |
||
| 273 | |||
| 274 | if (isset($this->columnDef['filter'][$column])) { |
||
| 275 | $method = $this->columnDef['filter'][$column]['method']; |
||
| 276 | $parameters = $this->columnDef['filter'][$column]['parameters']; |
||
| 277 | $this->compileColumnQuery($this->getQueryBuilder(), $method, $parameters, $column, $keyword); |
||
| 278 | } else { |
||
| 279 | $column = $this->castColumn($column); |
||
| 280 | if ($this->isCaseInsensitive()) { |
||
| 281 | $this->compileColumnSearch($i, $column, $keyword, true); |
||
| 282 | } else { |
||
| 283 | $col = strstr($column, '(') ? $this->connection->raw($column) : $column; |
||
| 284 | $this->compileColumnSearch($i, $col, $keyword, false); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | $this->isFilterApplied = true; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get proper keyword to use for search. |
||
| 295 | * |
||
| 296 | * @param int $i |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | private function getSearchKeyword($i) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Perform case insensitive column search. |
||
| 310 | * |
||
| 311 | * @param int $i |
||
| 312 | * @param mixed $column |
||
| 313 | * @param string $keyword |
||
| 314 | * @param bool $caseSensitive |
||
| 315 | */ |
||
| 316 | protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Perform regex case insensitive column search. |
||
| 329 | * |
||
| 330 | * @param mixed $column |
||
| 331 | * @param string $keyword |
||
| 332 | * @param bool $caseSensitive |
||
| 333 | */ |
||
| 334 | protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @inheritdoc |
||
| 347 | */ |
||
| 348 | public function ordering() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get order by column name. |
||
| 381 | * |
||
| 382 | * @param array $orderable |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | private function setupOrderColumn(array $orderable) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @inheritdoc |
||
| 400 | */ |
||
| 401 | public function paging() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get results |
||
| 409 | * |
||
| 410 | * @return array|static[] |
||
| 411 | */ |
||
| 412 | public function results() |
||
| 416 | } |
||
| 417 |