Passed
Push — master ( 912bd1...0b1a33 )
by Marcel
06:53
created

StorageService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 6
dl 0
loc 13
rs 10
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 2021 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Db\StorageMapper;
15
use OCP\IRequest;
16
use Psr\Log\LoggerInterface;
17
18
class StorageService
19
{
20
    private $logger;
21
    private $StorageMapper;
22
    private $ThresholdService;
23
    private $DatasetService;
24
25
    public function __construct(
26
        string $AppName,
0 ignored issues
show
Unused Code introduced by
The parameter $AppName is not used and could be removed. ( Ignorable by Annotation )

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

26
        /** @scrutinizer ignore-unused */ string $AppName,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
        IRequest $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-unused */ IRequest $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
        LoggerInterface $logger,
29
        StorageMapper $StorageMapper,
30
        DatasetService $DatasetService,
31
        ThresholdService $ThresholdService
32
    )
33
    {
34
        $this->logger = $logger;
35
        $this->StorageMapper = $StorageMapper;
36
        $this->DatasetService = $DatasetService;
37
        $this->ThresholdService = $ThresholdService;
38
    }
39
40
    /**
41
     * Get the items for the selected category
42
     *
43
     * @NoAdminRequired
44
     * @param $datasetId
45
     * @param $options
46
     * @return array
47
     */
48
    public function read($datasetId, $options)
49
    {
50
        $availableDimensions = array();
51
        $header = array();
52
53
        $datasetMetadata = $this->DatasetService->read($datasetId);
54
55
        // output the dimensions available for filtering of this dataset
56
        // this needs to map the technical name to its display name in the report
57
        $availableDimensions['dimension1'] = $datasetMetadata['dimension1'];
58
        $availableDimensions['dimension2'] = $datasetMetadata['dimension2'];
59
60
        // return the header texts of the data being transferred according to the current drilldown state selected by user
61
        $options = json_decode($options, true);
62
        if (!isset($options['drilldown']['dimension1'])) $header[0] = $datasetMetadata['dimension1'];
63
        if (!isset($options['drilldown']['dimension2'])) $header[1] = $datasetMetadata['dimension2'];
64
        $header[6] = $datasetMetadata['value'];
65
        $header = array_values($header);
66
67
        $data = $this->StorageMapper->read($datasetMetadata['id'], $options);
68
        $data = array_values($data);
69
        foreach ($data as $key => $value) {
70
            $data[$key] = array_values($value);
71
        }
72
73
        return empty($data) ? [
74
            'dimensions' => $availableDimensions,
75
            'status' => 'nodata',
76
            'error' => 0
77
        ] : [
78
            'header' => $header,
79
            'dimensions' => $availableDimensions,
80
            'data' => $data,
81
            'error' => 0
82
        ];
83
    }
84
85
    /**
86
     * Get the items for the selected category
87
     *
88
     * @NoAdminRequired
89
     * @param int $datasetId
90
     * @param $dimension1
91
     * @param $dimension2
92
     * @param $value
93
     * @param string|null $user_id
94
     * @param $bulkInsert
95
     * @return array
96
     * @throws \Exception
97
     */
98
    public function update(int $datasetId, $dimension1, $dimension2, $value, string $user_id = null, $bulkInsert = null)
99
    {
100
        TODO:
101
        //dates in both columns
102
        $dimension2 = $this->convertGermanDateFormat($dimension2);
103
        //convert date into timestamp
104
        //$timestamp = $this->convertGermanDateFormat($timestamp);
105
        $value = $this->floatvalue($value);
106
        $validate = '';
107
        $insert = $update = $error = $action = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
108
109
        if ($value !== false) {
110
            try {
111
                $action = $this->StorageMapper->create($datasetId, $dimension1, $dimension2, $value, $user_id, null, $bulkInsert);
112
            } catch (\Exception $e) {
113
                $error = 1;
114
            }
115
            if ($action === 'insert') $insert = 1;
116
            elseif ($action === 'update') $update = 1;
117
        } else {
118
            $error = 1;
119
        }
120
121
        if ($error === 0) $validate = $this->ThresholdService->validate($datasetId, $dimension1, $dimension2, $value);
122
123
        return [
124
            'insert' => $insert,
125
            'update' => $update,
126
            'error' => $error,
127
            'validate' => $validate
128
        ];
129
    }
130
131
    /**
132
     * delete data
133
     *
134
     * @NoAdminRequired
135
     * @param int $datasetId
136
     * @param $dimension1
137
     * @param $dimension2
138
     * @return bool
139
     */
140
    public function delete(int $datasetId, $dimension1, $dimension2, string $user_id = null)
141
    {
142
        return $this->StorageMapper->delete($datasetId, $dimension1, $dimension2, $user_id);
143
    }
144
145
    /**
146
     * Simulate delete data
147
     *
148
     * @NoAdminRequired
149
     * @param int $datasetId
150
     * @param $dimension1
151
     * @param $dimension2
152
     * @return array
153
     */
154
    public function deleteSimulate(int $datasetId, $dimension1, $dimension2)
155
    {
156
        return $this->StorageMapper->deleteSimulate($datasetId, $dimension1, $dimension2);
157
    }
158
159
    private function floatvalue($val)
160
    {
161
        $val = str_replace(",", ".", $val);
162
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
163
        $val = preg_replace('/[^0-9-.]+/', '', $val);
164
        if (is_numeric($val)) {
165
            return number_format(floatval($val), 2, '.', '');
166
        } else {
167
            return false;
168
        }
169
    }
170
171
    private function convertGermanDateFormat($val)
172
    {
173
        $fullString = explode(' ', $val);
174
        $dateLength = strlen($fullString[0]);
175
        $dateParts = explode('.', $fullString[0]);
176
177
        if ($dateLength >= 6 && $dateLength <= 10 && count($dateParts) === 3) {
178
            // is most likely a german date format 20.02.2020
179
            $fullString[0] = $dateParts[2] . '-' . sprintf('%02d', $dateParts[1]) . '-' . sprintf('%02d', $dateParts[0]);
180
            $val = implode(' ', $fullString);
181
        }
182
        return $val;
183
    }
184
}