| Total Complexity | 89 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FileSearchBackend 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 FileSearchBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class FileSearchBackend implements ISearchBackend { |
||
| 54 | /** @var CachingTree */ |
||
| 55 | private $tree; |
||
| 56 | |||
| 57 | /** @var IUser */ |
||
| 58 | private $user; |
||
| 59 | |||
| 60 | /** @var IRootFolder */ |
||
| 61 | private $rootFolder; |
||
| 62 | |||
| 63 | /** @var IManager */ |
||
| 64 | private $shareManager; |
||
| 65 | |||
| 66 | /** @var View */ |
||
| 67 | private $view; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * FileSearchBackend constructor. |
||
| 71 | * |
||
| 72 | * @param CachingTree $tree |
||
| 73 | * @param IUser $user |
||
| 74 | * @param IRootFolder $rootFolder |
||
| 75 | * @param IManager $shareManager |
||
| 76 | * @param View $view |
||
| 77 | * @internal param IRootFolder $rootFolder |
||
| 78 | */ |
||
| 79 | public function __construct(CachingTree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) { |
||
| 80 | $this->tree = $tree; |
||
| 81 | $this->user = $user; |
||
| 82 | $this->rootFolder = $rootFolder; |
||
| 83 | $this->shareManager = $shareManager; |
||
| 84 | $this->view = $view; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Search endpoint will be remote.php/dav |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getArbiterPath() { |
||
| 94 | } |
||
| 95 | |||
| 96 | public function isValidScope($href, $depth, $path) { |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getPropertyDefinitionsForScope($href, $path) { |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param Query $search |
||
| 139 | * @return SearchResult[] |
||
| 140 | */ |
||
| 141 | public function search(Query $search) { |
||
| 142 | if (count($search->from) !== 1) { |
||
| 143 | throw new \InvalidArgumentException('Searching more than one folder is not supported'); |
||
| 144 | } |
||
| 145 | $query = $this->transformQuery($search); |
||
| 146 | $scope = $search->from[0]; |
||
| 147 | if ($scope->path === null) { |
||
| 148 | throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead'); |
||
| 149 | } |
||
| 150 | $node = $this->tree->getNodeForPath($scope->path); |
||
| 151 | if (!$node instanceof Directory) { |
||
| 152 | throw new \InvalidArgumentException('Search is only supported on directories'); |
||
| 153 | } |
||
| 154 | |||
| 155 | $fileInfo = $node->getFileInfo(); |
||
| 156 | $folder = $this->rootFolder->get($fileInfo->getPath()); |
||
| 157 | /** @var Folder $folder $results */ |
||
| 158 | $results = $folder->search($query); |
||
| 159 | |||
| 160 | /** @var SearchResult[] $nodes */ |
||
| 161 | $nodes = array_map(function (Node $node) { |
||
| 162 | if ($node instanceof Folder) { |
||
| 163 | $davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager); |
||
| 164 | } else { |
||
| 165 | $davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager); |
||
| 166 | } |
||
| 167 | $path = $this->getHrefForNode($node); |
||
| 168 | $this->tree->cacheNode($davNode, $path); |
||
| 169 | return new SearchResult($davNode, $path); |
||
| 170 | }, $results); |
||
| 171 | |||
| 172 | if (!$query->limitToHome()) { |
||
| 173 | // Sort again, since the result from multiple storages is appended and not sorted |
||
| 174 | usort($nodes, function (SearchResult $a, SearchResult $b) use ($search) { |
||
| 175 | return $this->sort($a, $b, $search->orderBy); |
||
| 176 | }); |
||
| 177 | } |
||
| 178 | |||
| 179 | // If a limit is provided use only return that number of files |
||
| 180 | if ($search->limit->maxResults !== 0) { |
||
| 181 | $nodes = \array_slice($nodes, 0, $search->limit->maxResults); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $nodes; |
||
| 185 | } |
||
| 186 | |||
| 187 | private function sort(SearchResult $a, SearchResult $b, array $orders) { |
||
| 188 | /** @var Order $order */ |
||
| 189 | foreach ($orders as $order) { |
||
| 190 | $v1 = $this->getSearchResultProperty($a, $order->property); |
||
| 191 | $v2 = $this->getSearchResultProperty($b, $order->property); |
||
| 192 | |||
| 193 | |||
| 194 | if ($v1 === null && $v2 === null) { |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | if ($v1 === null) { |
||
| 198 | return $order->order === Order::ASC ? 1 : -1; |
||
| 199 | } |
||
| 200 | if ($v2 === null) { |
||
| 201 | return $order->order === Order::ASC ? -1 : 1; |
||
| 202 | } |
||
| 203 | |||
| 204 | $s = $this->compareProperties($v1, $v2, $order); |
||
| 205 | if ($s === 0) { |
||
| 206 | continue; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($order->order === Order::DESC) { |
||
| 210 | $s = -$s; |
||
| 211 | } |
||
| 212 | return $s; |
||
| 213 | } |
||
| 214 | |||
| 215 | return 0; |
||
| 216 | } |
||
| 217 | |||
| 218 | private function compareProperties($a, $b, Order $order) { |
||
| 219 | switch ($order->property->dataType) { |
||
| 220 | case SearchPropertyDefinition::DATATYPE_STRING: |
||
| 221 | return strcmp($a, $b); |
||
| 222 | case SearchPropertyDefinition::DATATYPE_BOOLEAN: |
||
| 223 | if ($a === $b) { |
||
| 224 | return 0; |
||
| 225 | } |
||
| 226 | if ($a === false) { |
||
| 227 | return -1; |
||
| 228 | } |
||
| 229 | return 1; |
||
| 230 | default: |
||
| 231 | if ($a === $b) { |
||
| 232 | return 0; |
||
| 233 | } |
||
| 234 | if ($a < $b) { |
||
| 235 | return -1; |
||
| 236 | } |
||
| 237 | return 1; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | private function getSearchResultProperty(SearchResult $result, SearchPropertyDefinition $property) { |
||
| 242 | /** @var \OCA\DAV\Connector\Sabre\Node $node */ |
||
| 243 | $node = $result->node; |
||
| 244 | |||
| 245 | switch ($property->name) { |
||
| 246 | case '{DAV:}displayname': |
||
| 247 | return $node->getName(); |
||
| 248 | case '{DAV:}getlastmodified': |
||
| 249 | return $node->getLastModified(); |
||
| 250 | case FilesPlugin::SIZE_PROPERTYNAME: |
||
| 251 | return $node->getSize(); |
||
| 252 | case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME: |
||
| 253 | return $node->getInternalFileId(); |
||
| 254 | default: |
||
| 255 | return null; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param Node $node |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | private function getHrefForNode(Node $node) { |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param Query $query |
||
| 270 | * @return ISearchQuery |
||
| 271 | */ |
||
| 272 | private function transformQuery(Query $query): ISearchQuery { |
||
| 273 | // TODO offset |
||
| 274 | $limit = $query->limit; |
||
| 275 | $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy); |
||
| 276 | |||
| 277 | $limitHome = false; |
||
| 278 | $ownerProp = $this->extractWhereValue($query->where, FilesPlugin::OWNER_ID_PROPERTYNAME, Operator::OPERATION_EQUAL); |
||
| 279 | if ($ownerProp !== null) { |
||
| 280 | if ($ownerProp === $this->user->getUID()) { |
||
| 281 | $limitHome = true; |
||
| 282 | } else { |
||
| 283 | throw new \InvalidArgumentException("Invalid search value for '{http://owncloud.org/ns}owner-id', only the current user id is allowed"); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | return new SearchQuery( |
||
| 288 | $this->transformSearchOperation($query->where), |
||
| 289 | (int)$limit->maxResults, |
||
| 290 | 0, |
||
| 291 | $orders, |
||
| 292 | $this->user, |
||
| 293 | $limitHome |
||
| 294 | ); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param Order $order |
||
| 299 | * @return ISearchOrder |
||
| 300 | */ |
||
| 301 | private function mapSearchOrder(Order $order) { |
||
| 302 | return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property)); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param Operator $operator |
||
| 307 | * @return ISearchOperator |
||
| 308 | */ |
||
| 309 | private function transformSearchOperation(Operator $operator) { |
||
| 310 | list(, $trimmedType) = explode('}', $operator->type); |
||
| 311 | switch ($operator->type) { |
||
| 312 | case Operator::OPERATION_AND: |
||
| 313 | case Operator::OPERATION_OR: |
||
| 314 | case Operator::OPERATION_NOT: |
||
| 315 | $arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments); |
||
| 316 | return new SearchBinaryOperator($trimmedType, $arguments); |
||
| 317 | case Operator::OPERATION_EQUAL: |
||
| 318 | case Operator::OPERATION_GREATER_OR_EQUAL_THAN: |
||
| 319 | case Operator::OPERATION_GREATER_THAN: |
||
| 320 | case Operator::OPERATION_LESS_OR_EQUAL_THAN: |
||
| 321 | case Operator::OPERATION_LESS_THAN: |
||
| 322 | case Operator::OPERATION_IS_LIKE: |
||
| 323 | if (count($operator->arguments) !== 2) { |
||
| 324 | throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation'); |
||
| 325 | } |
||
| 326 | if (!($operator->arguments[0] instanceof SearchPropertyDefinition)) { |
||
| 327 | throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property'); |
||
| 328 | } |
||
| 329 | if (!($operator->arguments[1] instanceof Literal)) { |
||
| 330 | throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal'); |
||
| 331 | } |
||
| 332 | return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value)); |
||
| 333 | case Operator::OPERATION_IS_COLLECTION: |
||
| 334 | return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE); |
||
| 335 | default: |
||
| 336 | throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')'); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param SearchPropertyDefinition $property |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | private function mapPropertyNameToColumn(SearchPropertyDefinition $property) { |
||
| 345 | switch ($property->name) { |
||
| 346 | case '{DAV:}displayname': |
||
| 347 | return 'name'; |
||
| 348 | case '{DAV:}getcontenttype': |
||
| 349 | return 'mimetype'; |
||
| 350 | case '{DAV:}getlastmodified': |
||
| 351 | return 'mtime'; |
||
| 352 | case FilesPlugin::SIZE_PROPERTYNAME: |
||
| 353 | return 'size'; |
||
| 354 | case TagsPlugin::FAVORITE_PROPERTYNAME: |
||
| 355 | return 'favorite'; |
||
| 356 | case TagsPlugin::TAGS_PROPERTYNAME: |
||
| 357 | return 'tagname'; |
||
| 358 | case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME: |
||
| 359 | return 'fileid'; |
||
| 360 | default: |
||
| 361 | throw new \InvalidArgumentException('Unsupported property for search or order: ' . $property->name); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | private function castValue(SearchPropertyDefinition $property, $value) { |
||
| 366 | switch ($property->dataType) { |
||
| 367 | case SearchPropertyDefinition::DATATYPE_BOOLEAN: |
||
| 368 | return $value === 'yes'; |
||
| 369 | case SearchPropertyDefinition::DATATYPE_DECIMAL: |
||
| 370 | case SearchPropertyDefinition::DATATYPE_INTEGER: |
||
| 371 | case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER: |
||
| 372 | return 0 + $value; |
||
| 373 | case SearchPropertyDefinition::DATATYPE_DATETIME: |
||
| 374 | if (is_numeric($value)) { |
||
| 375 | return max(0, 0 + $value); |
||
| 376 | } |
||
| 377 | $date = \DateTime::createFromFormat(\DateTime::ATOM, $value); |
||
| 378 | return ($date instanceof \DateTime && $date->getTimestamp() !== false) ? $date->getTimestamp() : 0; |
||
| 379 | default: |
||
| 380 | return $value; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get a specific property from the were clause |
||
| 386 | */ |
||
| 387 | private function extractWhereValue(Operator &$operator, string $propertyName, string $comparison, bool $acceptableLocation = true): ?string { |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 |