Passed
Push — master ( 1eb084...1c074e )
by Morris
13:12 queued 11s
created

SearchOrder::sortFileInfoNoDirection()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 7
nop 2
dl 0
loc 16
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Files\Search;
25
26
use OCP\Files\FileInfo;
27
use OCP\Files\Search\ISearchOrder;
28
29
class SearchOrder implements ISearchOrder {
30
	/** @var  string */
31
	private $direction;
32
	/** @var  string */
33
	private $field;
34
35
	/**
36
	 * SearchOrder constructor.
37
	 *
38
	 * @param string $direction
39
	 * @param string $field
40
	 */
41
	public function __construct($direction, $field) {
42
		$this->direction = $direction;
43
		$this->field = $field;
44
	}
45
46
	/**
47
	 * @return string
48
	 */
49
	public function getDirection() {
50
		return $this->direction;
51
	}
52
53
	/**
54
	 * @return string
55
	 */
56
	public function getField() {
57
		return $this->field;
58
	}
59
60
	public function sortFileInfo(FileInfo $a, FileInfo $b): int {
61
		$cmp = $this->sortFileInfoNoDirection($a, $b);
62
		return $cmp * ($this->direction === ISearchOrder::DIRECTION_ASCENDING ? 1 : -1);
63
	}
64
65
	private function sortFileInfoNoDirection(FileInfo $a, FileInfo $b): int {
66
		switch ($this->field) {
67
			case 'name':
68
				return $a->getName() <=> $b->getName();
69
			case 'mimetype':
70
				return $a->getMimetype() <=> $b->getMimetype();
71
			case 'mtime':
72
				return $a->getMtime() <=> $b->getMtime();
73
			case 'size':
74
				return $a->getSize() <=> $b->getSize();
75
			case 'fileid':
76
				return $a->getId() <=> $b->getId();
77
			case 'permissions':
78
				return $a->getPermissions() <=> $b->getPermissions();
79
			default:
80
				return 0;
81
		}
82
	}
83
}
84