Passed
Push — master ( 2ab278...f93ee0 )
by Marcel
03:22
created

Provider::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 17
rs 9.9
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2020 Marcel Scherello
10
 */
11
12
declare(strict_types=1);
13
14
namespace OCA\Analytics\Search;
15
16
use OCA\Analytics\Controller\DatasetController;
17
use OCP\IL10N;
18
use OCP\IURLGenerator;
19
use OCP\IUser;
20
use OCP\Search\IProvider;
21
use OCP\Search\ISearchQuery;
22
use OCP\Search\SearchResult;
23
use OCP\Search\SearchResultEntry;
24
25
class Provider implements IProvider
26
{
27
28
    /** @var IL10N */
29
    private $l10n;
30
31
    /** @var IURLGenerator */
32
    private $urlGenerator;
33
34
    private $datasetController;
35
36
    public function __construct(IL10N $l10n,
37
                                IURLGenerator $urlGenerator,
38
                                DatasetController $datasetController)
39
    {
40
        $this->l10n = $l10n;
41
        $this->urlGenerator = $urlGenerator;
42
        $this->datasetController = $datasetController;
43
    }
44
45
    public function getId(): string
46
    {
47
        return 'analytics';
48
    }
49
50
    public function search(IUser $user, ISearchQuery $query): SearchResult
51
    {
52
        $datasets = $this->datasetController->search($query->getTerm());
53
54
        foreach ($datasets as $dataset) {
55
            $result[] = new SearchResultEntry(
56
                '',
57
                $dataset['name'],
58
                '',
59
                $this->urlGenerator->linkToRoute('analytics.page.index') . '#/r/' . $dataset['id'],
60
                $this->urlGenerator->imagePath('analytics', 'app-dark.svg')
61
            );
62
        }
63
64
        return SearchResult::complete(
65
            $this->l10n->t('Analytics'),
66
            $result
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 54. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
67
        );
68
    }
69
70
    public function getName(): string
71
    {
72
        return $this->l10n->t('Analytics');
73
    }
74
75
    public function getOrder(string $route, array $routeParameters): int
76
    {
77
        return 10;
78
    }
79
}