Issues (496)

lib/Reference/ReferenceProvider.php (9 issues)

Labels
Severity
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Reference;
10
11
use OCA\Analytics\Service\ReportService;
12
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
0 ignored issues
show
The type OCP\Collaboration\Refere...erableReferenceProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use OCP\Collaboration\Reference\ISearchableReferenceProvider;
0 ignored issues
show
The type OCP\Collaboration\Refere...chableReferenceProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use OCP\Collaboration\Reference\Reference;
0 ignored issues
show
The type OCP\Collaboration\Reference\Reference was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OC\Collaboration\Reference\ReferenceManager;
0 ignored issues
show
The type OC\Collaboration\Reference\ReferenceManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\Collaboration\Reference\IReference;
0 ignored issues
show
The type OCP\Collaboration\Reference\IReference was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use OCP\IConfig;
0 ignored issues
show
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\IL10N;
0 ignored issues
show
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\IURLGenerator;
0 ignored issues
show
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Psr\Log\LoggerInterface;
0 ignored issues
show
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class ReferenceProvider extends ADiscoverableReferenceProvider implements ISearchableReferenceProvider
23
{
24
    private const RICH_OBJECT_TYPE = 'analytics'; // 'open-graph'
25
26
    private ?string $userId;
27
    private IConfig $config;
28
    private ReferenceManager $referenceManager;
29
    private IL10N $l10n;
30
    private IURLGenerator $urlGenerator;
31
    private LoggerInterface $logger;
32
    private ReportService $ReportService;
33
34
    public function __construct(IConfig          $config,
35
                                LoggerInterface  $logger,
36
                                IL10N            $l10n,
37
                                IURLGenerator    $urlGenerator,
38
                                ReferenceManager $referenceManager,
39
                                ReportService    $ReportService,
40
                                ?string          $userId)
41
    {
42
        $this->userId = $userId;
43
        $this->logger = $logger;
44
        $this->config = $config;
45
        $this->referenceManager = $referenceManager;
46
        $this->l10n = $l10n;
47
        $this->urlGenerator = $urlGenerator;
48
        $this->ReportService = $ReportService;
49
    }
50
51
    public function getId(): string
52
    {
53
        return 'analytics';
54
    }
55
56
    public function getTitle(): string
57
    {
58
        return $this->l10n->t('Analytics Report');
59
    }
60
61
    public function getOrder(): int
62
    {
63
        return 10;
64
    }
65
66
    public function getIconUrl(): string
67
    {
68
        return $this->urlGenerator->getAbsoluteURL(
69
            $this->urlGenerator->imagePath('analytics', 'app-dark.svg')
70
        );
71
    }
72
73
    public function getSupportedSearchProviderIds(): array
74
    {
75
        return ['analytics'];
76
    }
77
78
    public function matchReference(string $referenceText): bool
79
    {
80
        $adminLinkPreviewEnabled = $this->config->getAppValue('analytics', 'link_preview_enabled', '1') === '1';
81
        if (!$adminLinkPreviewEnabled) {
82
            //return false;
83
        }
84
        return preg_match('~/apps/analytics/#/r/~', $referenceText) === 1;
85
    }
86
87
    public function resolveReference(string $referenceText): ?IReference
88
    {
89
        if ($this->matchReference($referenceText)) {
90
            preg_match("/\d+$/", $referenceText, $matches); // get the last integer
91
            $reportMetadata = $this->ReportService->read((int)$matches[0]);
92
            if (!empty($reportMetadata)) {
93
                $imageUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'app-dark.svg'));
94
                $name = $reportMetadata['name'];
95
                $subheader = $reportMetadata['subheader'];
96
            } else {
97
                $imageUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'noReport.svg'));
98
                $name = $this->l10n->t('Report not found');
99
                $subheader = $this->l10n->t('This report was removed or is not shared with you');
100
            }
101
            $reference = new Reference($referenceText);
102
            $reference->setTitle($name);
103
            $reference->setDescription($subheader);
104
            $reference->setImageUrl($imageUrl);
105
            $reference->setRichObject(
106
                self::RICH_OBJECT_TYPE,
107
                [
108
                    'name' => $name,
109
                    'subheader' => $subheader,
110
                    'url' => $referenceText,
111
                    'image' => $imageUrl
112
                ]
113
            );
114
            return $reference;
115
        }
116
        return null;
117
    }
118
119
    public function getCachePrefix(string $referenceId): string
120
    {
121
        return $this->userId ?? '';
122
    }
123
124
    public function getCacheKey(string $referenceId): ?string
125
    {
126
        return $referenceId;
127
    }
128
129
    public function invalidateUserCache(string $userId): void
130
    {
131
        $this->referenceManager->invalidateCache($userId);
132
    }
133
}