Passed
Push — master ( a84da7...d6dfa5 )
by Marcel
02:49
created

StorageController::convertGermanDateFormat()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\Db\StorageMapper;
15
use OCP\AppFramework\Controller;
16
use OCP\ILogger;
17
use OCP\IRequest;
18
19
class StorageController extends Controller
20
{
21
    private $logger;
22
    private $StorageMapper;
23
    private $ThresholdController;
24
25
    public function __construct(
26
        string $AppName,
27
        IRequest $request,
28
        ILogger $logger,
29
        StorageMapper $StorageMapper,
30
        ThresholdController $ThresholdController
31
    )
32
    {
33
        parent::__construct($AppName, $request);
34
        $this->logger = $logger;
35
        $this->StorageMapper = $StorageMapper;
36
        $this->ThresholdController = $ThresholdController;
37
    }
38
39
    /**
40
     * Get the items for the selected category
41
     *
42
     * @NoAdminRequired
43
     * @param $datasetMetadata
44
     * @param $objectDrilldown
45
     * @param $dateDrilldown
46
     * @return array
47
     */
48
    public function read($datasetMetadata, $objectDrilldown, $dateDrilldown)
49
    {
50
        $header = array();
51
        if ($objectDrilldown === 'true') $header[0] = $datasetMetadata['dimension1'];
52
        if ($dateDrilldown === 'true') $header[1] = $datasetMetadata['dimension2'];
53
        $header[2] = $datasetMetadata['dimension3'];
54
55
        $data = $this->StorageMapper->getData($datasetMetadata['id'], $objectDrilldown, $dateDrilldown);
56
57
        return empty($data) ? [
58
            'status' => 'nodata'
59
        ] : [
60
            'header' => $header,
61
            'data' => $data,
62
        ];
63
    }
64
65
    /**
66
     * Get the items for the selected category
67
     *
68
     * @NoAdminRequired
69
     * @param int $datasetId
70
     * @param $dimension1
71
     * @param $dimension2
72
     * @param $dimension3
73
     * @param string|null $user_id
74
     * @return array
75
     * @throws \Exception
76
     */
77
    public function update(int $datasetId, $dimension1, $dimension2, $dimension3, string $user_id = null)
78
    {
79
        $dimension2 = $this->convertGermanDateFormat($dimension2);
80
        $dimension3 = $this->floatvalue($dimension3);
81
82
        $validate = $this->ThresholdController->validate($datasetId, $dimension1, $dimension2, $dimension3);
83
        $action = $this->StorageMapper->createData($datasetId, $dimension1, $dimension2, $dimension3, $user_id);
84
85
        $insert = $update = 0;
86
        if ($action === 'insert') $insert = 1;
87
        elseif ($action === 'update') $update = 1;
88
89
        return [
90
            'insert' => $insert,
91
            'update' => $update,
92
            'validate' => $validate
93
        ];
94
    }
95
96
    /**
97
     * delete data
98
     *
99
     * @NoAdminRequired
100
     * @param int $datasetId
101
     * @param $dimension1
102
     * @param $dimension2
103
     * @return bool
104
     */
105
    public function delete(int $datasetId, $dimension1, $dimension2)
106
    {
107
        return $this->StorageMapper->deleteData($datasetId, $dimension1, $dimension2);
108
    }
109
110
    /**
111
     * Simulate delete data
112
     *
113
     * @NoAdminRequired
114
     * @param int $datasetId
115
     * @param $dimension1
116
     * @param $dimension2
117
     * @param $dimension3
118
     * @return array
119
     */
120
    public function deleteSimulate(int $datasetId, $dimension1, $dimension2, $dimension3)
121
    {
122
        return $this->StorageMapper->deleteDataSimulate($datasetId, $dimension1, $dimension2, $dimension3);
123
    }
124
125
    private function floatvalue($val)
126
    {
127
        $val = str_replace(",", ".", $val);
128
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
129
        $val = preg_replace('/[^0-9-.]+/', '', $val);
130
        if (is_numeric($val)) {
131
            return number_format(floatval($val), 2, '.', '');
132
        } else {
133
            return false;
134
        }
135
    }
136
137
    private function convertGermanDateFormat($val)
138
    {
139
        $fullString = explode(' ', $val);
140
        $dateLength = strlen($fullString[0]);
141
        $dateParts = explode('.', $fullString[0]);
142
143
        if ($dateLength >= 6 && $dateLength <= 10 && count($dateParts) === 3) {
144
            // is most likely a german date format 20.02.2020
145
            $fullString[0] = $dateParts[2] . '-' . sprintf('%02d', $dateParts[1]) . '-' . sprintf('%02d', $dateParts[0]);
146
            $val = implode(' ', $fullString);
147
        }
148
        return $val;
149
    }
150
}