Issues (496)

lib/Notification/Notifier.php (7 issues)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Analytics
5
 *
6
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\Analytics\Notification;
11
12
use InvalidArgumentException;
13
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...
14
use OCP\IUserManager;
0 ignored issues
show
The type OCP\IUserManager 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 OCP\L10N\IFactory;
0 ignored issues
show
The type OCP\L10N\IFactory 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\Notification\AlreadyProcessedException;
0 ignored issues
show
The type OCP\Notification\AlreadyProcessedException 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\Notification\INotification;
0 ignored issues
show
The type OCP\Notification\INotification 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\Notification\INotifier;
0 ignored issues
show
The type OCP\Notification\INotifier 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 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...
20
21
class Notifier implements INotifier
22
{
23
24
    /** @var IFactory */
25
    protected $l10nFactory;
26
27
    /** @var IUserManager */
28
    protected $userManager;
29
30
    /** @var IURLGenerator */
31
    protected $urlGenerator;
32
33
    private $logger;
34
35
    public function __construct(IFactory $l10nFactory,
36
                                IUserManager $userManager,
37
                                LoggerInterface $logger,
38
                                IURLGenerator $urlGenerator)
39
    {
40
        $this->l10nFactory = $l10nFactory;
41
        $this->userManager = $userManager;
42
        $this->logger = $logger;
43
        $this->urlGenerator = $urlGenerator;
44
    }
45
46
    /**
47
     * Identifier of the notifier, only use [a-z0-9_]
48
     *
49
     * @return string
50
     * @since 17.0.0
51
     */
52
    public function getID(): string
53
    {
54
        return 'analytics';
55
    }
56
57
    /**
58
     * Human readable name describing the notifier
59
     *
60
     * @return string
61
     * @since 17.0.0
62
     */
63
    public function getName(): string
64
    {
65
        return $this->l10nFactory->get('analytics')->t('Announcements');
66
    }
67
68
    /**
69
     * @param INotification $notification
70
     * @param string $languageCode The code of the language that should be used to prepare the notification
71
     * @return INotification
72
     * @throws InvalidArgumentException When the notification was not prepared by a notifier
73
     * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
74
     * @since 9.0.0
75
     */
76
    public function prepare(INotification $notification, string $languageCode): INotification
77
    {
78
        if ($notification->getApp() !== 'analytics') {
79
            // Not my app => throw
80
            throw new InvalidArgumentException('Unknown app');
81
        }
82
83
        // Read the language from the notification
84
        $l = $this->l10nFactory->get('analytics', $languageCode);
85
        $parsedSubject = '';
86
87
        $parameters = $notification->getSubjectParameters();
88
89
        switch ($notification->getObjectType()) {
90
            case NotificationManager::SUBJECT_THRESHOLD:
91
                $parsedSubject = $l->t("Report '{report}': {subject} reached the threshold of {rule} {value}");
92
                $link = $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . '#/r/' . $notification->getObjectId();
93
94
                $notification->setRichSubject(
95
                    $parsedSubject,
96
                    [
97
                        'report' => [
98
                            'type' => 'highlight',
99
                            'id' => $notification->getObjectId(),
100
                            'name' => $parameters['report'],
101
                            'link' => $link,
102
                        ],
103
                        'subject' => [
104
                            'type' => 'highlight',
105
                            'id' => $notification->getObjectId(),
106
                            'name' => $parameters['subject'],
107
                        ],
108
                        'rule' => [
109
                            'type' => 'highlight',
110
                            'id' => $notification->getObjectId(),
111
                            'name' => $parameters['rule'],
112
                        ],
113
                        'value' => [
114
                            'type' => 'highlight',
115
                            'id' => $notification->getObjectId(),
116
                            'name' => $parameters['value'],
117
                        ],
118
                    ]
119
                );
120
121
                break;
122
            case NotificationManager::DATALOAD_ERROR:
123
                $parsedSubject = $l->t("Error during data load \"{dataloadName}\" for data set \"{datasetName}\"" );
124
                $link = $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . 'a/#/r/' . $notification->getObjectId();
125
126
                $notification->setRichSubject(
127
                    $parsedSubject,
128
                    [
129
                        'dataloadName' => [
130
                            'type' => 'highlight',
131
                            'id' => $notification->getObjectId(),
132
                            'name' => $parameters['dataloadName'],
133
                        ],
134
                        'datasetName' => [
135
                            'type' => 'highlight',
136
                            'id' => $notification->getObjectId(),
137
                            'name' => $parameters['datasetName'],
138
                            'link' => $link,
139
                        ]
140
                    ]
141
                );
142
143
                break;
144
            default: // legacy due to switch to subject field filled with an id for notification removal
145
                //$parsedSubject = $l->t("Report '{report}': {subject} reached the threshold of {rule} {value}");
146
                //$link = $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . '#/r/' . $notification->getObjectId();
147
        }
148
149
150
        $notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'app-dark.svg')));
151
        $this->setParsedSubjectFromRichSubject($notification);
152
        return $notification;
153
    }
154
155
    // This is a little helper function which automatically sets the simple parsed subject
156
    // based on the rich subject you set.
157
    protected function setParsedSubjectFromRichSubject(INotification $notification)
158
    {
159
        $placeholders = $replacements = [];
160
        foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) {
161
            $placeholders[] = '{' . $placeholder . '}';
162
            if ($parameter['type'] === 'file') {
163
                $replacements[] = $parameter['path'];
164
            } else {
165
                $replacements[] = $parameter['name'];
166
            }
167
        }
168
169
        $notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject()));
170
    }
171
172
}
173