ActivityManager::createEvent()   B
last analyzed

Complexity

Conditions 8
Paths 14

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 14
nop 4
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
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\Activity;
10
11
use OCA\Analytics\Db\ReportMapper;
12
use OCA\Analytics\Db\DatasetMapper;
13
use OCA\Analytics\Db\PanoramaMapper;
14
use OCA\Analytics\Db\ShareMapper;
15
use OCA\Analytics\Service\ShareService;
16
use OCP\Activity\IEvent;
0 ignored issues
show
Bug introduced by
The type OCP\Activity\IEvent 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\Activity\IManager;
0 ignored issues
show
Bug introduced by
The type OCP\Activity\IManager 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 Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
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...
19
20
class ActivityManager
21
{
22
    const OBJECT_REPORT = 'analytics_report';
23
	const OBJECT_PANORAMA = 'analytics_panorama';
24
	const OBJECT_DATASET = 'analytics_dataset';
25
	const OBJECT_DATA = 'analytics_data';
26
27
    const SUBJECT_REPORT_ADD = 'report_add';
28
    const SUBJECT_REPORT_DELETE = 'report_delete';
29
    const SUBJECT_REPORT_SHARE = 'report_share';
30
31
	const SUBJECT_PANORAMA_ADD = 'panorama_add';
32
	const SUBJECT_PANORAMA_DELETE = 'panorama_delete';
33
	const SUBJECT_PANORAMA_SHARE = 'panorama_share';
34
35
	const SUBJECT_DATASET_ADD = 'dataset_add';
36
	const SUBJECT_DATASET_DELETE = 'dataset_delete';
37
	const SUBJECT_DATASET_SHARE = 'dataset_share';
38
39
    const SUBJECT_DATA_ADD = 'data_add';
40
    const SUBJECT_DATA_ADD_API = 'data_add_api';
41
    const SUBJECT_DATA_ADD_IMPORT = 'data_add_import';
42
    const SUBJECT_DATA_ADD_DATALOAD = 'data_add_dataload';
43
44
    private $manager;
45
    private $userId;
46
    private $ShareMapper;
47
    private $logger;
48
    private $ReportMapper;
49
    private $DatasetMapper;
50
	private $PanoramaMapper;
51
52
    public function __construct(
53
        IManager $manager,
54
        ShareMapper $ShareMapper,
55
        $userId,
56
        ReportMapper $ReportMapper,
57
        DatasetMapper $DatasetMapper,
58
        LoggerInterface $logger,
59
		PanoramaMapper $PanoramaMapper
60
    )
61
    {
62
        $this->manager = $manager;
63
        $this->userId = $userId;
64
        $this->ShareMapper = $ShareMapper;
65
        $this->ReportMapper = $ReportMapper;
66
        $this->DatasetMapper = $DatasetMapper;
67
		$this->PanoramaMapper = $PanoramaMapper;
68
        $this->logger = $logger;
69
    }
70
71
    /**
72
     * @param $datasetId
73
     * @param $eventType
74
     * @param $eventSubject
75
     * @param string|null $user_id
76
     */
77
    public function triggerEvent($datasetId, $eventType, $eventSubject, string $user_id = null)
78
    {
79
        try {
80
            $event = $this->createEvent($datasetId, $eventType, $eventSubject, $user_id);
81
            if ($event !== null) {
82
				// TODO
83
                $this->sendToUsers($event);
84
            }
85
        } catch (\Exception $e) {
86
            // Ignore exception for undefined activities on update events
87
        }
88
    }
89
90
    /**
91
     * @param $reportId
92
     * @param $eventType
93
     * @param $eventSubject
94
     * @param string|null $user_id
95
     * @return IEvent
96
     * @throws \OCP\DB\Exception
97
     */
98
    private function createEvent($objectId, $eventType, $eventSubject, string $user_id = null)
99
    {
100
        if ($eventType === ActivityManager::OBJECT_REPORT) {
101
            $name = $objectId !== 0 ? $this->ReportMapper->readOwn($objectId)['name'] : '';
102
        } elseif ($eventType === ActivityManager::OBJECT_DATASET) {
103
            $name = $objectId !== 0 ? $this->DatasetMapper->read($objectId)['name'] : '';
104
            $objectId = 0;
105
		} elseif ($eventType === ActivityManager::OBJECT_PANORAMA) {
106
			$name = $objectId !== 0 ? $this->PanoramaMapper->read($objectId)['name'] : '';
107
        }
108
109
        if ($user_id) $this->userId = $user_id;
110
        $event = $this->manager->generateEvent();
111
        $event->setApp('analytics')
112
            ->setType($eventType)
113
            ->setAuthor($this->userId)
114
            ->setObject('report', (int)$objectId, $name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.
Loading history...
115
            ->setSubject($eventSubject, ['author' => $this->userId])
116
            ->setTimestamp(time());
117
        return $event;
118
    }
119
120
    /**
121
     * Publish activity to all users that are part of the dataset
122
     *
123
     * @param IEvent $event
124
     */
125
    private function sendToUsers(IEvent $event)
126
    {
127
		switch ($event->getType()) {
128
			case ActivityManager::OBJECT_REPORT:
129
				$item_type = ShareService::SHARE_ITEM_TYPE_REPORT;
130
				break;
131
			case ActivityManager::OBJECT_DATASET:
132
				$item_type = ShareService::SHARE_ITEM_TYPE_DATASET;
133
				break;
134
			case ActivityManager::OBJECT_PANORAMA:
135
				$item_type = ShareService::SHARE_ITEM_TYPE_PANORAMA;
136
				break;
137
		}
138
139
        $users = $this->ShareMapper->getSharedReceiver($item_type, $event->getObjectId());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item_type does not seem to be defined for all execution paths leading up to this point.
Loading history...
140
		//$this->logger->info('share receiver: ' . $item_type . $event->getObjectId());
141
        foreach ($users as $user) {
142
            $event->setAffectedUser($user['uid_owner']);
143
            $this->manager->publish($event);
144
        }
145
        $event->setAffectedUser($this->userId);
146
        $this->manager->publish($event);
147
    }
148
}