Passed
Push — master ( a291c5...6eae59 )
by Marcel
14:20 queued 12s
created

SearchProvider::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 2019-2022 Marcel Scherello
10
 */
11
12
declare(strict_types=1);
13
14
namespace OCA\Analytics\Search;
15
16
use OCA\Analytics\Service\ReportService;
17
use OCP\App\IAppManager;
18
use OCP\IL10N;
19
use OCP\IURLGenerator;
20
use OCP\IUser;
21
use OCP\Search\IProvider;
22
use OCP\Search\ISearchQuery;
23
use OCP\Search\SearchResult;
24
use OCP\Search\SearchResultEntry;
25
26
class SearchProvider implements IProvider
27
{
28
29
    /** @var IAppManager */
30
    private $appManager;
31
    /** @var IL10N */
32
    private $l10n;
33
    /** @var IURLGenerator */
34
    private $urlGenerator;
35
    private $ReportService;
36
37
    public function __construct(IAppManager $appManager,
38
                                IL10N $l10n,
39
                                IURLGenerator $urlGenerator,
40
                                ReportService $ReportService)
41
    {
42
        $this->appManager = $appManager;
43
        $this->l10n = $l10n;
44
        $this->urlGenerator = $urlGenerator;
45
        $this->ReportService = $ReportService;
46
    }
47
48
    public function getId(): string
49
    {
50
        return 'analytics';
51
    }
52
53
    public function search(IUser $user, ISearchQuery $query): SearchResult
54
    {
55
        if (!$this->appManager->isEnabledForUser('analytics', $user)) {
56
            return SearchResult::complete($this->getName(), []);
57
        }
58
59
        $datasets = $this->ReportService->search($query->getTerm());
60
        $result = [];
61
62
        foreach ($datasets as $dataset) {
63
            $result[] = new SearchResultEntry(
64
                $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'app-dark.svg')),
65
                $dataset['name'],
66
                '',
67
                $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('analytics.page.index') . '#/r/' . $dataset['id']),
68
                ''
69
            );
70
        }
71
72
        return SearchResult::complete(
73
            $this->l10n->t('Analytics'),
74
            $result
75
        );
76
    }
77
78
    public function getName(): string
79
    {
80
        return $this->l10n->t('Analytics');
81
    }
82
83
    public function getOrder(string $route, array $routeParameters): int
84
    {
85
        return 10;
86
    }
87
}