Passed
Push — master ( 4987fe...db5ac9 )
by John
13:30 queued 12s
created

FilesSearchProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 2 1
A __construct() 0 6 1
A getName() 0 2 1
A search() 0 17 3
A formatSubline() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\Files\Search;
27
28
use OC\Search\Provider\File;
29
use OC\Search\Result\File as FileResult;
30
use OCP\IL10N;
31
use OCP\IURLGenerator;
32
use OCP\IUser;
33
use OCP\Search\IProvider;
34
use OCP\Search\ISearchQuery;
35
use OCP\Search\SearchResult;
36
37
class FilesSearchProvider implements IProvider {
38
39
	/** @var File */
40
	private $fileSearch;
41
42
	/** @var IL10N */
43
	private $l10n;
44
45
	/** @var IURLGenerator */
46
	private $urlGenerator;
47
48
	public function __construct(File $fileSearch,
49
								IL10N $l10n,
50
								IURLGenerator $urlGenerator) {
51
		$this->l10n = $l10n;
52
		$this->fileSearch = $fileSearch;
53
		$this->urlGenerator = $urlGenerator;
54
	}
55
56
	public function getId(): string {
57
		return 'files';
58
	}
59
60
	public function getName(): string {
61
		return $this->l10n->t('Files');
62
	}
63
64
	public function search(IUser $user, ISearchQuery $query): SearchResult {
65
		return SearchResult::complete(
66
			$this->l10n->t('Files'),
67
			array_map(function (FileResult $result) {
68
				// Generate thumbnail url
69
				$thumbnailUrl = $result->type === 'folder'
0 ignored issues
show
Deprecated Code introduced by
The property OC\Search\Result\File::$type has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
				$thumbnailUrl = /** @scrutinizer ignore-deprecated */ $result->type === 'folder'

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
70
					? ''
71
					: $this->urlGenerator->linkToRoute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->id]);
0 ignored issues
show
Deprecated Code introduced by
The property OCP\Search\Result::$id has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
					: $this->urlGenerator->linkToRoute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => /** @scrutinizer ignore-deprecated */ $result->id]);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
72
73
				return new FilesSearchResultEntry(
74
					$thumbnailUrl,
75
					$result->name,
0 ignored issues
show
Deprecated Code introduced by
The property OCP\Search\Result::$name has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
					/** @scrutinizer ignore-deprecated */ $result->name,

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
76
					$this->formatSubline($result),
77
					$result->link,
0 ignored issues
show
Deprecated Code introduced by
The property OCP\Search\Result::$link has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
					/** @scrutinizer ignore-deprecated */ $result->link,

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
78
					$result->type === 'folder' ? 'icon-folder' : 'icon-filetype-file'
0 ignored issues
show
Deprecated Code introduced by
The property OC\Search\Result\File::$type has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
					/** @scrutinizer ignore-deprecated */ $result->type === 'folder' ? 'icon-folder' : 'icon-filetype-file'

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
79
				);
80
			}, $this->fileSearch->search($query->getTerm()))
0 ignored issues
show
Bug introduced by
$this->fileSearch->search($query->getTerm()) of type OCP\Search\Result is incompatible with the type array expected by parameter $arr1 of array_map(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
			}, /** @scrutinizer ignore-type */ $this->fileSearch->search($query->getTerm()))
Loading history...
81
		);
82
	}
83
84
	/**
85
	 * Format subline for files
86
	 *
87
	 * @param FileResult $result
88
	 * @return string
89
	 */
90
	private function formatSubline($result): string {
91
		// Do not show the location if the file is in root
92
		if ($result->path === '/' . $result->name) {
0 ignored issues
show
Deprecated Code introduced by
The property OC\Search\Result\File::$path has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
		if (/** @scrutinizer ignore-deprecated */ $result->path === '/' . $result->name) {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The property OCP\Search\Result::$name has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
		if ($result->path === '/' . /** @scrutinizer ignore-deprecated */ $result->name) {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
93
			return '';
94
		}
95
96
		$path = ltrim(dirname($result->path), '/');
0 ignored issues
show
Deprecated Code introduced by
The property OC\Search\Result\File::$path has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
		$path = ltrim(dirname(/** @scrutinizer ignore-deprecated */ $result->path), '/');

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
97
		return $this->l10n->t('in %s', [$path]);
98
	}
99
}
100