Passed
Push — master ( 7f6713...5eaeae )
by Marcel
02:17
created

DataLoadController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10

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
 * 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\Controller;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\DataResponse;
17
use OCP\AppFramework\Http\NotFoundResponse;
18
use OCP\Files\NotFoundException;
19
use OCP\ILogger;
20
use OCP\IRequest;
21
22
class DataLoadController extends Controller
23
{
24
    private $logger;
25
    private $StorageController;
26
    private $DataSourceController;
27
    private $userId;
28
    private $ActivityManager;
29
    private $DatasetController;
30
31
    public function __construct(
32
        string $AppName,
33
        IRequest $request,
34
        $userId,
35
        ILogger $logger,
36
        ActivityManager $ActivityManager,
37
        DataSourceController $DataSourceController,
38
        DatasetController $DatasetController,
39
        StorageController $StorageController
40
    )
41
    {
42
        parent::__construct($AppName, $request);
43
        $this->userId = $userId;
44
        $this->logger = $logger;
45
        $this->StorageController = $StorageController;
46
        $this->ActivityManager = $ActivityManager;
47
        $this->DataSourceController = $DataSourceController;
48
        $this->DatasetController = $DatasetController;
49
    }
50
51
    /**
52
     * update data from input form
53
     *
54
     * @NoAdminRequired
55
     * @param int $datasetId
56
     * @param $dimension1
57
     * @param $dimension2
58
     * @param $dimension3
59
     * @return DataResponse|NotFoundResponse
60
     */
61
    public function update(int $datasetId, $dimension1, $dimension2, $dimension3)
62
    {
63
        //$this->NotificationManager->triggerNotification(NotificationManager::OBJECT_DATASET, $datasetId, NotificationManager::SUBJECT_THRESHOLD, ['subject' => 'Pflanze', 'rule' => 'Hum too low']);
64
        //disabled for the moment
65
        $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId);
66
        if (!empty($datasetMetadata)) {
67
            $insert = $update = 0;
68
            $dimension3 = str_replace(',', '.', $dimension3);
69
            $action = $this->StorageController->update($datasetId, $dimension1, $dimension2, $dimension3);
70
            $insert = $insert + $action['insert'];
71
            $update = $update + $action['update'];
72
73
            $result = [
74
                'insert' => $insert,
75
                'update' => $update
76
            ];
77
78
            $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD);
79
            return new DataResponse($result);
80
        } else {
81
            return new NotFoundResponse();
82
        }
83
    }
84
85
    /**
86
     * update data from input form
87
     *
88
     * @NoAdminRequired
89
     * @param int $datasetId
90
     * @param $dimension1
91
     * @param $dimension2
92
     * @return DataResponse|NotFoundResponse
93
     */
94
    public function delete(int $datasetId, $dimension1, $dimension2)
95
    {
96
        $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId);
97
        if (!empty($datasetMetadata)) {
98
            $result = $this->StorageController->delete($datasetId, $dimension1, $dimension2);
99
            return new DataResponse($result);
0 ignored issues
show
Bug introduced by
$result of type true is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

99
            return new DataResponse(/** @scrutinizer ignore-type */ $result);
Loading history...
100
        } else {
101
            return new NotFoundResponse();
102
        }
103
    }
104
105
    /**
106
     * Import clipboard data
107
     *
108
     * @NoAdminRequired
109
     * @param int $datasetId
110
     * @param $import
111
     * @return DataResponse|NotFoundResponse
112
     */
113
    public function importClipboard($datasetId, $import)
114
    {
115
        $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId);
116
        if (!empty($datasetMetadata)) {
117
            $insert = $update = 0;
118
            $delimiter = $this->detectDelimiter($import);
119
            $rows = str_getcsv($import, "\n");
120
121
            foreach ($rows as &$row) {
122
                $row = str_getcsv($row, $delimiter);
123
                $row[2] = $this->floatvalue($row[2]);
124
                $action = $this->StorageController->update($datasetId, $row[0], $row[1], $row[2]);
125
                $insert = $insert + $action['insert'];
126
                $update = $update + $action['update'];
127
            }
128
129
            $result = [
130
                'insert' => $insert,
131
                'update' => $update,
132
                'delimiter' => $delimiter
133
            ];
134
135
            $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT);
136
            return new DataResponse($result);
137
        } else {
138
            return new NotFoundResponse();
139
        }
140
    }
141
142
    private function detectDelimiter($data)
143
    {
144
        $delimiters = ["\t", ";", "|", ","];
145
        $data_2 = null;
146
        $delimiter = $delimiters[0];
147
        foreach ($delimiters as $d) {
148
            $firstRow = str_getcsv($data, "\n")[0];
149
            $data_1 = str_getcsv($firstRow, $d);
150
            if (sizeof($data_1) > sizeof($data_2)) {
151
                $delimiter = $d;
152
                $data_2 = $data_1;
153
            }
154
        }
155
        return $delimiter;
156
    }
157
158
    private function floatvalue($val)
159
    {
160
        $val = str_replace(",", ".", $val);
161
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
162
        $val = preg_replace('/[^0-9-.]+/', '', $val);
163
        return floatval($val);
164
    }
165
166
    /**
167
     * Import data into dataset from an internal or external file
168
     *
169
     * @NoAdminRequired
170
     * @param int $datasetId
171
     * @param $path
172
     * @return DataResponse|NotFoundResponse
173
     * @throws NotFoundException
174
     */
175
    public function importFile(int $datasetId, $path)
176
    {
177
        //$this->logger->error('DataLoadController 100:'.$datasetId. $path);
178
        $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId);
179
        if (!empty($datasetMetadata)) {
180
            $insert = $update = 0;
181
            $datasetMetadata['type'] = DataSourceController::DATASET_TYPE_INTERNAL_FILE;
182
            $result = $this->DataSourceController->read($datasetMetadata, $path);
183
184
            foreach ($result['data'] as &$row) {
185
                $action = $this->StorageController->update($datasetId, $row['dimension1'], $row['dimension2'], $row['dimension3']);
186
                $insert = $insert + $action['insert'];
187
                $update = $update + $action['update'];
188
            }
189
190
            $result = [
191
                'insert' => $insert,
192
                'update' => $update
193
            ];
194
195
            $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT);
196
            return new DataResponse($result);
197
        } else {
198
            return new NotFoundResponse();
199
        }
200
    }
201
}