Passed
Branch master (7b1276)
by Marcel
06:24
created

ActivityManager::createEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 11
rs 9.9666
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 OCA\Analytics\Controller\DbController;
15
use OCP\Activity\IEvent;
16
use OCP\Activity\IManager;
17
use OCP\IL10N;
18
use OCP\ILogger;
19
20
class ActivityManager
21
{
22
    const OBJECT_DATASET = 'analytics_dataset';
23
    const OBJECT_DATA = 'analytics_data';
24
    const SUBJECT_DATASET_ADD = 'dataset_add';
25
    const SUBJECT_DATASET_DELETE = 'dataset_delete';
26
    const SUBJECT_DATASET_SHARE = 'dataset_share';
27
    const SUBJECT_DATA_ADD = 'data_add';
28
    const SUBJECT_DATA_ADD_API = 'data_add_api';
29
30
    private $manager;
31
    private $l10n;
32
    private $userId;
33
    private $DBController;
34
    private $logger;
35
36
    public function __construct(
37
        IManager $manager,
38
        IL10N $l10n,
39
        DbController $DBController,
40
        $userId,
41
        ILogger $logger
42
    )
43
    {
44
        $this->manager = $manager;
45
        $this->l10n = $l10n;
46
        $this->userId = $userId;
47
        $this->DBController = $DBController;
48
        $this->logger = $logger;
49
    }
50
51
    public function triggerEvent($datasetId, $eventType, $eventSubject)
52
    {
53
        try {
54
            $event = $this->createEvent($datasetId, $eventType, $eventSubject);
55
            if ($event !== null) {
56
                $this->sendToUsers($event);
57
            }
58
        } catch (\Exception $e) {
59
            // Ignore exception for undefined activities on update events
60
        }
61
    }
62
63
    private function createEvent($datasetId, $eventType, $eventSubject, $ownActivity = true, $author = null)
0 ignored issues
show
Unused Code introduced by
The parameter $ownActivity is not used and could be removed. ( Ignorable by Annotation )

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

63
    private function createEvent($datasetId, $eventType, $eventSubject, /** @scrutinizer ignore-unused */ $ownActivity = true, $author = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $author is not used and could be removed. ( Ignorable by Annotation )

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

63
    private function createEvent($datasetId, $eventType, $eventSubject, $ownActivity = true, /** @scrutinizer ignore-unused */ $author = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        $datasetName = $datasetId !== 0 ? $this->DBController->getOwnDataset($datasetId)['name'] : '';
66
        $event = $this->manager->generateEvent();
67
        $event->setApp('analytics')
68
            ->setType($eventType)
69
            ->setAuthor($this->userId)
70
            ->setObject('report', (int)$datasetId, $datasetName)
71
            ->setSubject($eventSubject, ['author' => $this->userId])
72
            ->setTimestamp(time());
73
        return $event;
74
    }
75
76
    /**
77
     * Publish activity to all users that are part of the dataset
78
     *
79
     * @param IEvent $event
80
     */
81
    private function sendToUsers(IEvent $event)
82
    {
83
        $users = $this->DBController->getSharedReceiver($event->getObjectId());
84
        //$this->logger->error($users);
85
        foreach ($users as $user) {
86
            $event->setAffectedUser($user['uid_owner']);
87
            $this->manager->publish($event);
88
        }
89
        $event->setAffectedUser('admin');
90
        $this->manager->publish($event);
91
    }
92
}