Passed
Push — master ( 3334e1...093f01 )
by Marcel
02:14
created

Provider::parse()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 58
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 45
nc 12
nop 3
dl 0
loc 58
rs 6.9666
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Data 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 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Activity;
13
14
use InvalidArgumentException;
15
use OCP\Activity\IEvent;
16
use OCP\Activity\IProvider;
17
use OCP\IL10N;
18
use OCP\IURLGenerator;
19
20
class Provider implements IProvider
21
{
22
23
    private $l10n;
24
    private $userId;
25
    private $urlGenerator;
26
27
    public function __construct(IURLGenerator $urlGenerator,
28
                                IL10N $l10n,
29
                                $userId)
30
    {
31
        $this->userId = $userId;
32
        $this->urlGenerator = $urlGenerator;
33
        $this->l10n = $l10n;
34
    }
35
36
    /**
37
     * @param string $language
38
     * @param IEvent $event
39
     * @param IEvent|null $previousEvent
40
     * @return IEvent
41
     * @throws InvalidArgumentException
42
     * @since 11.0.0
43
     */
44
    public function parse($language, IEvent $event, IEvent $previousEvent = null)
45
    {
46
        if ($event->getApp() !== 'analytics') {
47
            throw new InvalidArgumentException();
48
        }
49
50
        $parsedSubject = '';
51
        $ownActivity = ($event->getAuthor() === $this->userId);
52
53
        switch ($event->getSubject()) {
54
            case ActivityManager::SUBJECT_DATASET_ADD:
55
                $parsedSubject = $this->l10n->t('You created a new report');
56
                break;
57
            case ActivityManager::SUBJECT_DATASET_DELETE:
58
                $parsedSubject = $this->l10n->t('You deleted report {report}');
59
                break;
60
            case ActivityManager::SUBJECT_DATASET_SHARE:
61
                if ($ownActivity) {
62
                    $parsedSubject = $this->l10n->t('You shared report {report}');
63
                } else {
64
                    $parsedSubject = $event->getSubjectParameters()['author'] . $this->l10n->t(' shared report {report} with you');
65
                }
66
                break;
67
            case ActivityManager::SUBJECT_DATA_ADD:
68
                if ($ownActivity) {
69
                    $parsedSubject = $this->l10n->t('You have added new data to report {report}');
70
                } else {
71
                    $parsedSubject = $event->getSubjectParameters()['author'] . $this->l10n->t(' has added new data to report {report}');
72
                }
73
                break;
74
            case ActivityManager::SUBJECT_DATA_ADD_IMPORT:
75
                if ($ownActivity) {
76
                    $parsedSubject = $this->l10n->t('You have imported data in report {report}');
77
                } else {
78
                    $parsedSubject = $event->getSubjectParameters()['author'] . $this->l10n->t(' has importet data in report {report}');
79
                }
80
                break;
81
            case ActivityManager::SUBJECT_DATA_ADD_API:
82
                $parsedSubject = $this->l10n->t('New data was add via API to report {report}');
83
                break;
84
            case ActivityManager::SUBJECT_DATA_ADD_DATALOAD:
85
                $parsedSubject = $this->l10n->t('New data was add via dataload to report {report}');
86
                break;
87
        }
88
89
        $event->setRichSubject(
90
            $parsedSubject,
91
            ['report' => [
92
                'type' => 'highlight',
93
                'id' => $event->getObjectId(),
94
                'name' => basename($event->getObjectName()),
95
                'link' => $this->Url($event->getObjectId()),
96
            ]]
97
        );
98
99
        $event->setParsedSubject($parsedSubject)
100
            ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath($event->getApp(), 'app-dark.svg')));
101
        return $event;
102
    }
103
104
    public function Url($endpoint)
105
    {
106
        return $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . '#/r/' . $endpoint;
107
    }
108
}