Passed
Push — master ( 7c42c4...9333e3 )
by Marcel
04:41 queued 14s
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
        return $ownReports;
95
    }
96
97
    /**
98
     * get own report details
99
     *
100
     * @param int $panoramaId
101
     * @return array
102
     * @throws Exception
103
     */
104
    public function read(int $panoramaId)
105
    {
106
        $ownReport = $this->PanoramaMapper->readOwn($panoramaId);
107
        return $ownReport;
108
    }
109
110
    /**
111
     * check if own report
112
     *
113
     * @param int $panoramaId
114
     * @return bool
115
     */
116
    public function isOwn(int $panoramaId)
117
    {
118
        $ownReport = $this->PanoramaMapper->readOwn($panoramaId);
119
        if (!empty($ownReport)) {
120
            return true;
121
        } else {
122
            return false;
123
        }
124
    }
125
126
    /**
127
     * create new blank report
128
     *
129
     * @param $name
130
     * @param int $type
131
     * @param int $parent
132
     * @param $pages
133
     * @return int
134
     * @throws Exception
135
     */
136
    public function create(int $type, int $parent): int
137
    {
138
        $reportId = $this->PanoramaMapper->create($this->l10n->t('New'), $type, $parent, '[]');
139
        //$this->ActivityManager->triggerEvent($reportId, ActivityManager::OBJECT_REPORT, ActivityManager::SUBJECT_REPORT_ADD);
140
        return $reportId;
141
    }
142
143
    /**
144
     * update report details
145
     *
146
     * @param int $id
147
     * @param $name
148
     * @param $subheader
149
     * @param int $type
150
     * @param int $parent
151
     * @param $pages
152
     * @return bool
153
     * @throws Exception
154
     */
155
    public function update(int $id, $name, int $type, int $parent, $pages)
156
    {
157
        return $this->PanoramaMapper->update($id, $name, $type, $parent, $pages);
158
    }
159
160
    /**
161
     * Delete Dataset and all depending objects
162
     *
163
     * @param int $reportId
164
     * @return string
165
     * @throws Exception
166
     */
167
    public function delete(int $reportId)
168
    {
169
        $this->PanoramaMapper->delete($reportId);
170
        return 'true';
171
    }
172
173
    /**
174
     * get dataset by user
175
     *
176
     * @param string $userId
177
     * @return array|bool
178
     * @throws Exception
179
     */
180
    public function deleteByUser(string $userId)
181
    {
182
        $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...
183
        foreach ($reports as $report) {
184
            $this->ShareService->deleteShareByReport($report['id']);
185
            $this->ThresholdMapper->deleteThresholdByReport($report['id']);
186
            $this->setFavorite($report['id'], 'false');
0 ignored issues
show
Bug introduced by
The method setFavorite() does not exist on OCA\Analytics\Service\PanoramaService. ( Ignorable by Annotation )

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

186
            $this->/** @scrutinizer ignore-call */ 
187
                   setFavorite($report['id'], 'false');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
            $this->ReportMapper->delete($report['id']);
188
        }
189
        return true;
190
    }
191
    /**
192
     * search for reports
193
     *
194
     * @param string $searchString
195
     * @return array
196
     */
197
    public function search(string $searchString)
198
    {
199
        return $this->PanoramaMapper->search($searchString);
200
    }
201
}
202