Passed
Push — master ( c70a6b...e1aeb2 )
by Marcel
05:29 queued 16s
created

StoryService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

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\StoryMapper;
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 StoryService
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 $StoryMapper;
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
        StoryMapper $StoryMapper,
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->StoryMapper = $StoryMapper;
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
     */
79
    public function index(): array
80
    {
81
        $ownReports = $this->StoryMapper->index();
82
        //$sharedReports = $this->ShareService->getSharedReports();
83
        $sharedReports = null;
84
        $keysToKeep = array('id', 'name', 'dataset', 'favorite', 'parent', 'type', 'isShare', 'shareId');
85
86
        // get shared reports and remove duplicates
87
        foreach ($sharedReports as $sharedReport) {
0 ignored issues
show
Bug introduced by
The expression $sharedReports of type null is not traversable.
Loading history...
88
            if (!array_search($sharedReport['id'], array_column($ownReports, 'id'))) {
89
                // just keep the necessary fields
90
                $ownReports[] = array_intersect_key($sharedReport, array_flip($keysToKeep));;
91
            }
92
        }
93
        return $ownReports;
94
    }
95
96
    /**
97
     * get own report details
98
     *
99
     * @param int $storyId
100
     * @return array
101
     * @throws Exception
102
     */
103
    public function read(int $storyId)
104
    {
105
        $ownReport = $this->StoryMapper->readOwn($storyId);
106
        return $ownReport;
107
    }
108
109
    /**
110
     * check if own report
111
     *
112
     * @param int $storyId
113
     * @return bool
114
     */
115
    public function isOwn(int $storyId)
116
    {
117
        $ownReport = $this->StoryMapper->readOwn($storyId);
118
        if (!empty($ownReport)) {
119
            return true;
120
        } else {
121
            return false;
122
        }
123
    }
124
125
    /**
126
     * create new blank report
127
     *
128
     * @param $name
129
     * @param $subheader
130
     * @param int $type
131
     * @param int $page
132
     * @param int $parent
133
     * @param $reports
134
     * @param $layout
135
     * @return int
136
     * @throws Exception
137
     */
138
    public function create($name, $subheader, int $type, int $page, int $parent, $reports, $layout): int
139
    {
140
        $reportId = $this->StoryMapper->create($name, $subheader, $type, $page, $parent, $reports, $layout);
141
        //$this->ActivityManager->triggerEvent($reportId, ActivityManager::OBJECT_REPORT, ActivityManager::SUBJECT_REPORT_ADD);
142
        return $reportId;
143
    }
144
145
    /**
146
     * update report details
147
     *
148
     * @param int $id
149
     * @param $name
150
     * @param $subheader
151
     * @param int $type
152
     * @param int $page
153
     * @param int $parent
154
     * @param $reports
155
     * @param $layout
156
     * @return bool
157
     * @throws Exception
158
     */
159
    public function update(int $id, $name, $subheader, int $type, int $page, int $parent, $reports, $layout)
160
    {
161
        return $this->StoryMapper->update($id, $name, $subheader, $type, $page, $parent, $reports, $layout);
162
    }
163
164
    /**
165
     * Delete Dataset and all depending objects
166
     *
167
     * @param int $reportId
168
     * @return string
169
     * @throws Exception
170
     */
171
    public function delete(int $reportId)
172
    {
173
        $this->StoryMapper->delete($reportId);
174
        return 'true';
175
    }
176
177
    /**
178
     * get dataset by user
179
     *
180
     * @param string $userId
181
     * @return array|bool
182
     * @throws Exception
183
     */
184
    public function deleteByUser(string $userId)
185
    {
186
        $reports = $this->ReportMapper->indexByUser($userId);
0 ignored issues
show
Bug Best Practice introduced by
The property ReportMapper does not exist on OCA\Analytics\Service\StoryService. Did you maybe forget to declare it?
Loading history...
187
        foreach ($reports as $report) {
188
            $this->ShareService->deleteShareByReport($report['id']);
189
            $this->ThresholdMapper->deleteThresholdByReport($report['id']);
190
            $this->setFavorite($report['id'], 'false');
0 ignored issues
show
Bug introduced by
The method setFavorite() does not exist on OCA\Analytics\Service\StoryService. ( Ignorable by Annotation )

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

190
            $this->/** @scrutinizer ignore-call */ 
191
                   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...
191
            $this->ReportMapper->delete($report['id']);
192
        }
193
        return true;
194
    }
195
    /**
196
     * search for reports
197
     *
198
     * @param string $searchString
199
     * @return array
200
     */
201
    public function search(string $searchString)
202
    {
203
        return $this->StoryMapper->search($searchString);
204
    }
205
}
206