Passed
Push — master ( a8ef1b...a5405c )
by Marcel
02:41
created

Notifier::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Data Analytics
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the LICENSE.md file.
8
 *
9
 * @author Marcel Scherello <[email protected]>
10
 * @copyright 2019 Marcel Scherello
11
 */
12
13
namespace OCA\Analytics\Notification;
14
15
use InvalidArgumentException;
16
use OCP\IURLGenerator;
17
use OCP\IUserManager;
18
use OCP\L10N\IFactory;
19
use OCP\Notification\AlreadyProcessedException;
0 ignored issues
show
Bug introduced by
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...
20
use OCP\Notification\IManager as INotificationManager;
21
use OCP\Notification\INotification;
22
use OCP\Notification\INotifier;
23
24
class Notifier implements INotifier
25
{
26
27
    /** @var IFactory */
28
    protected $l10nFactory;
29
30
    /** @var INotificationManager */
31
    protected $notificationManager;
32
33
    /** @var IUserManager */
34
    protected $userManager;
35
36
    /** @var IURLGenerator */
37
    protected $urlGenerator;
38
39
    public function __construct(IFactory $l10nFactory,
40
                                INotificationManager $notificationManager,
41
                                IUserManager $userManager,
42
                                IURLGenerator $urlGenerator)
43
    {
44
        $this->l10nFactory = $l10nFactory;
45
        $this->notificationManager = $notificationManager;
46
        $this->userManager = $userManager;
47
        $this->urlGenerator = $urlGenerator;
48
    }
49
50
    /**
51
     * Identifier of the notifier, only use [a-z0-9_]
52
     *
53
     * @return string
54
     * @since 17.0.0
55
     */
56
    public function getID(): string
57
    {
58
        return 'analytics';
59
    }
60
61
    /**
62
     * Human readable name describing the notifier
63
     *
64
     * @return string
65
     * @since 17.0.0
66
     */
67
    public function getName(): string
68
    {
69
        return $this->l10nFactory->get('analytics')->t('Announcements');
70
    }
71
72
    /**
73
     * @param INotification $notification
74
     * @param string $languageCode The code of the language that should be used to prepare the notification
75
     * @return INotification
76
     * @throws InvalidArgumentException When the notification was not prepared by a notifier
77
     */
78
    public function prepare(INotification $notification, $languageCode)
79
    {
80
        if ($notification->getApp() !== 'analytics') {
81
            // Not my app => throw
82
            throw new InvalidArgumentException('Unknown app');
83
        }
84
85
        // Read the language from the notification
86
        $l = $this->l10nFactory->get('analytics', $languageCode);
87
88
        $i = $notification->getSubject();
89
        if ($i !== 'alert') {
90
            // Unknown subject => Unknown notification => throw
91
            throw new InvalidArgumentException('Unknown subject');
92
        }
93
94
        $link = $this->urlGenerator->linkToRouteAbsolute('analytics.page.main', [
95
            'analytics' => $notification->getObjectId(),
96
        ]);
97
98
        $notification->setParsedMessage('test message')
99
            ->setRichSubject(
100
                $l->t('{user} announced '),
101
                [
102
                    'user' => [
103
                        'type' => 'user',
104
                        'id' => 1,
105
                        'name' => 'testuser',
106
                    ]
107
                ]
108
            )
109
            ->setLink($link)
110
            ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'app-dark.svg')));
111
112
        $notification->setParsedSubject($notification);
0 ignored issues
show
Bug introduced by
$notification of type OCP\Notification\INotification is incompatible with the type string expected by parameter $subject of OCP\Notification\INotification::setParsedSubject(). ( Ignorable by Annotation )

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

112
        $notification->setParsedSubject(/** @scrutinizer ignore-type */ $notification);
Loading history...
113
        return $notification;
114
    }
115
}