| Total Complexity | 43 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SearchContext 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.
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 SearchContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class SearchContext |
||
| 43 | { |
||
| 44 | use Injectable; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * DataObject subclass to which search parameters relate to. |
||
| 48 | * Also determines as which object each result is provided. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $modelClass; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * FormFields mapping to {@link DataObject::$db} properties |
||
| 56 | * which are supposed to be searchable. |
||
| 57 | * |
||
| 58 | * @var FieldList |
||
| 59 | */ |
||
| 60 | protected $fields; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Array of {@link SearchFilter} subclasses. |
||
| 64 | * |
||
| 65 | * @var SearchFilter[] |
||
| 66 | */ |
||
| 67 | protected $filters; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Key/value pairs of search fields to search terms |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $searchParams = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The logical connective used to join WHERE clauses. Defaults to AND. |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | public $connective = 'AND'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * A key value pair of values that should be searched for. |
||
| 84 | * The keys should match the field names specified in {@link self::$fields}. |
||
| 85 | * Usually these values come from a submitted searchform |
||
| 86 | * in the form of a $_REQUEST object. |
||
| 87 | * CAUTION: All values should be treated as insecure client input. |
||
| 88 | * |
||
| 89 | * @param string $modelClass The base {@link DataObject} class that search properties related to. |
||
| 90 | * Also used to generate a set of result objects based on this class. |
||
| 91 | * @param FieldList $fields Optional. FormFields mapping to {@link DataObject::$db} properties |
||
| 92 | * which are to be searched. Derived from modelclass using |
||
| 93 | * {@link DataObject::scaffoldSearchFields()} if left blank. |
||
| 94 | * @param array $filters Optional. Derived from modelclass if left blank |
||
| 95 | */ |
||
| 96 | public function __construct($modelClass, $fields = null, $filters = null) |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns scaffolded search fields for UI. |
||
| 105 | * |
||
| 106 | * @return FieldList |
||
| 107 | */ |
||
| 108 | public function getSearchFields() |
||
| 111 | // $this->fields is causing weirdness, so we ignore for now, using the default scaffolding |
||
| 112 | //return singleton($this->modelClass)->scaffoldSearchFields(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @todo move to SQLSelect |
||
| 117 | * @todo fix hack |
||
| 118 | */ |
||
| 119 | protected function applyBaseTableFields() |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns a SQL object representing the search context for the given |
||
| 134 | * list of query parameters. |
||
| 135 | * |
||
| 136 | * @param array $searchParams Map of search criteria, mostly taken from $_REQUEST. |
||
| 137 | * If a filter is applied to a relationship in dot notation, |
||
| 138 | * the parameter name should have the dots replaced with double underscores, |
||
| 139 | * for example "Comments__Name" instead of the filter name "Comments.Name". |
||
| 140 | * @param array|bool|string $sort Database column to sort on. |
||
| 141 | * Falls back to {@link DataObject::$default_sort} if not provided. |
||
| 142 | * @param array|bool|string $limit |
||
| 143 | * @param DataList $existingQuery |
||
| 144 | * @return DataList |
||
| 145 | * @throws Exception |
||
| 146 | */ |
||
| 147 | public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) |
||
| 148 | { |
||
| 149 | /** DataList $query */ |
||
| 150 | $query = null; |
||
| 151 | if ($existingQuery) { |
||
| 152 | if (!($existingQuery instanceof DataList)) { |
||
|
|
|||
| 153 | throw new InvalidArgumentException("existingQuery must be DataList"); |
||
| 154 | } |
||
| 155 | if ($existingQuery->dataClass() != $this->modelClass) { |
||
| 156 | throw new InvalidArgumentException("existingQuery's dataClass is " . $existingQuery->dataClass() |
||
| 157 | . ", $this->modelClass expected."); |
||
| 158 | } |
||
| 159 | $query = $existingQuery; |
||
| 160 | } else { |
||
| 161 | $query = DataList::create($this->modelClass); |
||
| 162 | } |
||
| 163 | |||
| 164 | if (is_array($limit)) { |
||
| 165 | $query = $query->limit( |
||
| 166 | isset($limit['limit']) ? $limit['limit'] : null, |
||
| 167 | isset($limit['start']) ? $limit['start'] : null |
||
| 168 | ); |
||
| 169 | } else { |
||
| 170 | $query = $query->limit($limit); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** @var DataList $query */ |
||
| 174 | $query = $query->sort($sort); |
||
| 175 | $this->setSearchParams($searchParams); |
||
| 176 | |||
| 177 | foreach ($this->searchParams as $key => $value) { |
||
| 178 | $key = str_replace('__', '.', $key); |
||
| 179 | if ($filter = $this->getFilter($key)) { |
||
| 180 | $filter->setModel($this->modelClass); |
||
| 181 | $filter->setValue($value); |
||
| 182 | if (!$filter->isEmpty()) { |
||
| 183 | $modelObj = Injector::inst()->create($this->modelClass); |
||
| 184 | if(isset($modelObj->searchableFields()[$key]['match_any'])) { |
||
| 185 | $query = $query->alterDataQuery(function ($dataQuery) use ($modelObj, $key, $value) { |
||
| 186 | $searchFields = $modelObj->searchableFields()[$key]['match_any']; |
||
| 187 | $sqlSearchFields = []; |
||
| 188 | foreach($searchFields as $dottedRelation){ |
||
| 189 | $relation = substr($dottedRelation, 0, strpos($dottedRelation, '.')); |
||
| 190 | $relations = explode('.', $dottedRelation); |
||
| 191 | $fieldName = array_pop($relations); |
||
| 192 | |||
| 193 | // Apply join |
||
| 194 | $relationModelName = $dataQuery->applyRelation($relation); |
||
| 195 | |||
| 196 | // Get prefixed column |
||
| 197 | $relationPrefix = $dataQuery->applyRelationPrefix($relation); |
||
| 198 | |||
| 199 | // Find the db field the relation belongs to |
||
| 200 | $columnName = $modelObj->getSchema() |
||
| 201 | ->sqlColumnForField($relationModelName, $fieldName, $relationPrefix); |
||
| 202 | |||
| 203 | // Update filters to used the sqlColumnForField |
||
| 204 | $sqlSearchFields[$columnName] = $value; |
||
| 205 | } |
||
| 206 | $dataQuery = $dataQuery->whereAny($sqlSearchFields); |
||
| 207 | }); |
||
| 208 | } else { |
||
| 209 | $query = $query->alterDataQuery([$filter, 'apply']); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | if ($this->connective != "AND") { |
||
| 216 | throw new Exception("SearchContext connective '$this->connective' not supported after ORM-rewrite."); |
||
| 217 | } |
||
| 218 | |||
| 219 | return $query; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns a result set from the given search parameters. |
||
| 224 | * |
||
| 225 | * @todo rearrange start and limit params to reflect DataObject |
||
| 226 | * |
||
| 227 | * @param array $searchParams |
||
| 228 | * @param array|bool|string $sort |
||
| 229 | * @param array|bool|string $limit |
||
| 230 | * @return DataList |
||
| 231 | * @throws Exception |
||
| 232 | */ |
||
| 233 | public function getResults($searchParams, $sort = false, $limit = false) |
||
| 234 | { |
||
| 235 | $searchParams = array_filter((array)$searchParams, [$this, 'clearEmptySearchFields']); |
||
| 236 | |||
| 237 | // getQuery actually returns a DataList |
||
| 238 | return $this->getQuery($searchParams, $sort, $limit); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Callback map function to filter fields with empty values from |
||
| 243 | * being included in the search expression. |
||
| 244 | * |
||
| 245 | * @param mixed $value |
||
| 246 | * @return boolean |
||
| 247 | */ |
||
| 248 | public function clearEmptySearchFields($value) |
||
| 249 | { |
||
| 250 | return ($value != ''); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Accessor for the filter attached to a named field. |
||
| 255 | * |
||
| 256 | * @param string $name |
||
| 257 | * @return SearchFilter |
||
| 258 | */ |
||
| 259 | public function getFilter($name) |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the map of filters in the current search context. |
||
| 270 | * |
||
| 271 | * @return SearchFilter[] |
||
| 272 | */ |
||
| 273 | public function getFilters() |
||
| 274 | { |
||
| 275 | return $this->filters; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Overwrite the current search context filter map. |
||
| 280 | * |
||
| 281 | * @param array $filters |
||
| 282 | */ |
||
| 283 | public function setFilters($filters) |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds a instance of {@link SearchFilter}. |
||
| 290 | * |
||
| 291 | * @param SearchFilter $filter |
||
| 292 | */ |
||
| 293 | public function addFilter($filter) |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Removes a filter by name. |
||
| 300 | * |
||
| 301 | * @param string $name |
||
| 302 | */ |
||
| 303 | public function removeFilterByName($name) |
||
| 304 | { |
||
| 305 | unset($this->filters[$name]); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the list of searchable fields in the current search context. |
||
| 310 | * |
||
| 311 | * @return FieldList |
||
| 312 | */ |
||
| 313 | public function getFields() |
||
| 314 | { |
||
| 315 | return $this->fields; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Apply a list of searchable fields to the current search context. |
||
| 320 | * |
||
| 321 | * @param FieldList $fields |
||
| 322 | */ |
||
| 323 | public function setFields($fields) |
||
| 324 | { |
||
| 325 | $this->fields = $fields; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Adds a new {@link FormField} instance. |
||
| 330 | * |
||
| 331 | * @param FormField $field |
||
| 332 | */ |
||
| 333 | public function addField($field) |
||
| 334 | { |
||
| 335 | $this->fields->push($field); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Removes an existing formfield instance by its name. |
||
| 340 | * |
||
| 341 | * @param string $fieldName |
||
| 342 | */ |
||
| 343 | public function removeFieldByName($fieldName) |
||
| 344 | { |
||
| 345 | $this->fields->removeByName($fieldName); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set search param values |
||
| 350 | * |
||
| 351 | * @param array|HTTPRequest $searchParams |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function setSearchParams($searchParams) |
||
| 355 | { |
||
| 356 | // hack to work with $searchParams when it's an Object |
||
| 357 | if ($searchParams instanceof HTTPRequest) { |
||
| 358 | $this->searchParams = $searchParams->getVars(); |
||
| 359 | } else { |
||
| 360 | $this->searchParams = $searchParams; |
||
| 361 | } |
||
| 362 | return $this; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public function getSearchParams() |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Gets a list of what fields were searched and the values provided |
||
| 375 | * for each field. Returns an ArrayList of ArrayData, suitable for |
||
| 376 | * rendering on a template. |
||
| 377 | * |
||
| 378 | * @return ArrayList |
||
| 379 | */ |
||
| 380 | public function getSummary() |
||
| 418 | } |
||
| 419 | } |
||
| 420 |