Passed
Push — master ( 3b2113...e41df5 )
by Marcel
05:38 queued 12s
created

PanoramaService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 11
dl 0
loc 25
rs 9.9
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * 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-2022 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Controller\DatasourceController;
16
use OCA\Analytics\Db\PanoramaMapper;
17
use OCP\AppFramework\Http\DataDownloadResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataDownloadResponse 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\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception 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 OCP\Files\IRootFolder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\IRootFolder 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\ITagManager;
0 ignored issues
show
Bug introduced by
The type OCP\ITagManager 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...
21
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig 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...
22
use OCP\PreConditionNotMetException;
0 ignored issues
show
Bug introduced by
The type OCP\PreConditionNotMetException 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...
23
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...
24
use OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N 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...
25
26
class PanoramaService
27
{
28
    /** @var IConfig */
29
    protected $config;
30
    private $userId;
31
    private $logger;
32
    private $tagManager;
33
    private $ShareService;
34
    private $DatasetService;
35
    private $StorageMapper;
0 ignored issues
show
introduced by
The private property $StorageMapper is not used, and could be removed.
Loading history...
36
    private $PanoramaMapper;
37
    private $ThresholdMapper;
38
    private $DataloadMapper;
0 ignored issues
show
introduced by
The private property $DataloadMapper is not used, and could be removed.
Loading history...
39
    private $ActivityManager;
40
    private $rootFolder;
41
    private $VariableService;
42
    private $l10n;
43
44
    const REPORT_TYPE_GROUP = 0;
45
46
    public function __construct(
47
        $userId,
48
        IL10N $l10n,
49
        LoggerInterface $logger,
50
        ITagManager $tagManager,
51
        ShareService $ShareService,
52
        DatasetService $DatasetService,
53
        PanoramaMapper $PanoramaMapper,
54
        ActivityManager $ActivityManager,
55
        IRootFolder $rootFolder,
56
        IConfig $config,
57
        VariableService $VariableService
58
    )
59
    {
60
        $this->userId = $userId;
61
        $this->logger = $logger;
62
        $this->tagManager = $tagManager;
63
        $this->ShareService = $ShareService;
64
        $this->DatasetService = $DatasetService;
65
        $this->PanoramaMapper = $PanoramaMapper;
66
        $this->ActivityManager = $ActivityManager;
67
        $this->rootFolder = $rootFolder;
68
        $this->VariableService = $VariableService;
69
        $this->config = $config;
70
        $this->l10n = $l10n;
71
    }
72
73
    /**
74
     * get all reports
75
     *
76
     * @return array
77
     * @throws PreConditionNotMetException
78
     * @throws Exception
79
     */
80
    public function index(): array
81
    {
82
        $ownReports = $this->PanoramaMapper->index();
83
        //$sharedReports = $this->ShareService->getSharedReports();
84
        $sharedReports = null;
85
        $keysToKeep = array('id', 'name', 'dataset', 'favorite', 'parent', 'type', 'isShare', 'shareId');
86
87
        // get shared reports and remove duplicates
88
        foreach ($sharedReports as $sharedReport) {
0 ignored issues
show
Bug introduced by
The expression $sharedReports of type null is not traversable.
Loading history...
89
            if (!array_search($sharedReport['id'], array_column($ownReports, 'id'))) {
90
                // just keep the necessary fields
91
                $ownReports[] = array_intersect_key($sharedReport, array_flip($keysToKeep));;
92
            }
93
        }
94
95
        $favorites = $this->tagManager->load('analyticsPanorama')->getFavorites();
96
        foreach ($ownReports as &$ownReport) {
97
            $hasTag = 0;
98
            if (is_array($favorites) and in_array($ownReport['id'], $favorites)) {
99
                $hasTag = 1;
100
            }
101
            $ownReport['favorite'] = $hasTag;
102
            $ownReport = $this->VariableService->replaceTextVariables($ownReport);
103
        }
104
105
        return $ownReports;
106
    }
107
108
    /**
109
     * get own report details
110
     *
111
     * @param int $panoramaId
112
     * @return array
113
     * @throws Exception
114
     */
115
    public function read(int $panoramaId)
116
    {
117
        $ownReport = $this->PanoramaMapper->readOwn($panoramaId);
118
        return $ownReport;
119
    }
120
121
    /**
122
     * check if own report
123
     *
124
     * @param int $panoramaId
125
     * @return bool
126
     */
127
    public function isOwn(int $panoramaId)
128
    {
129
        $ownReport = $this->PanoramaMapper->readOwn($panoramaId);
130
        if (!empty($ownReport)) {
131
            return true;
132
        } else {
133
            return false;
134
        }
135
    }
136
137
    /**
138
     * create new blank report
139
     *
140
     * @param $name
141
     * @param int $type
142
     * @param int $parent
143
     * @param $pages
144
     * @return int
145
     * @throws Exception
146
     */
147
    public function create(int $type, int $parent): int
148
    {
149
        $reportId = $this->PanoramaMapper->create($this->l10n->t('New'), $type, $parent, '[]');
150
        //$this->ActivityManager->triggerEvent($reportId, ActivityManager::OBJECT_REPORT, ActivityManager::SUBJECT_REPORT_ADD);
151
        return $reportId;
152
    }
153
154
    /**
155
     * update report details
156
     *
157
     * @param int $id
158
     * @param $name
159
     * @param $subheader
160
     * @param int $type
161
     * @param int $parent
162
     * @param $pages
163
     * @return bool
164
     * @throws Exception
165
     */
166
    public function update(int $id, $name, int $type, int $parent, $pages)
167
    {
168
        return $this->PanoramaMapper->update($id, $name, $type, $parent, $pages);
169
    }
170
171
    /**
172
     * Delete Dataset and all depending objects
173
     *
174
     * @param int $reportId
175
     * @return string
176
     * @throws Exception
177
     */
178
    public function delete(int $reportId)
179
    {
180
        $this->PanoramaMapper->delete($reportId);
181
        return 'true';
182
    }
183
184
    /**
185
     * get dataset by user
186
     *
187
     * @param string $userId
188
     * @return array|bool
189
     * @throws Exception
190
     */
191
    public function deleteByUser(string $userId)
192
    {
193
        // ToDo
194
        $reports = $this->ReportMapper->indexByUser($userId);
0 ignored issues
show
Bug Best Practice introduced by
The property ReportMapper does not exist on OCA\Analytics\Service\PanoramaService. Did you maybe forget to declare it?
Loading history...
195
        foreach ($reports as $report) {
196
            $this->ShareService->deleteShareByReport($report['id']);
197
            $this->ThresholdMapper->deleteThresholdByReport($report['id']);
198
            $this->setFavorite($report['id'], 'false');
199
            $this->ReportMapper->delete($report['id']);
200
        }
201
        return true;
202
    }
203
204
    /**
205
     * get own reports which are marked as favorites
206
     *
207
     * @return array|bool
208
     * @throws Exception
209
     */
210
    public function getOwnFavoriteReports()
211
    {
212
        $ownReports = $this->PanoramaMapper->index();
213
        //$sharedReports = $this->ShareService->getSharedReports();
214
        $sharedReports = [];
215
        $favorites = $this->tagManager->load('analyticsPanorama')->getFavorites();
216
217
        // remove the favorite if the report is not existing anymore
218
        foreach ($favorites as $favorite) {
219
            if (!in_array($favorite, array_column($ownReports, 'id'))
220
                && !in_array($favorite, array_column($sharedReports, 'id'))) {
221
                unset($favorites[$favorite]);
222
                $this->tagManager->load('analyticsPanorama')->removeFromFavorites($favorite);
223
            }
224
        }
225
        return $favorites;
226
    }
227
228
    /**
229
     * set/remove the favorite flag for a report
230
     *
231
     * @param int $panoramaId
232
     * @param string $favorite
233
     * @return bool
234
     */
235
    public function setFavorite(int $panoramaId, string $favorite)
236
    {
237
        if ($favorite === 'true') {
238
            $return = $this->tagManager->load('analyticsPanorama')->addToFavorites($panoramaId);
239
        } else {
240
            $return = $this->tagManager->load('analyticsPanorama')->removeFromFavorites($panoramaId);
241
        }
242
        return $return;
243
    }
244
245
    /**
246
     * search for reports
247
     *
248
     * @param string $searchString
249
     * @return array
250
     */
251
    public function search(string $searchString)
252
    {
253
        return $this->PanoramaMapper->search($searchString);
254
    }
255
}
256