Passed
Push — master ( 1d1c0d...40ae07 )
by Marcel
02:46
created

StorageMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 11
rs 10
c 0
b 0
f 0
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\Db;
13
14
use OCP\IDbConnection;
15
use OCP\IL10N;
16
use OCP\ILogger;
17
18
class StorageMapper
19
{
20
    private $userId;
21
    private $l10n;
22
    private $db;
23
    private $logger;
24
25
    public function __construct(
26
        $userId,
27
        IL10N $l10n,
28
        IDbConnection $db,
29
        ILogger $logger
30
    )
31
    {
32
        $this->userId = $userId;
33
        $this->l10n = $l10n;
34
        $this->db = $db;
35
        $this->logger = $logger;
36
    }
37
38
    /**
39
     * Get file id for single track
40
     * @param int $dataset
41
     * @param  $objectDrilldown
42
     * @param  $dateDrilldown
43
     * @return array
44
     */
45
    public function getData(int $dataset, $objectDrilldown = null, $dateDrilldown = null)
46
    {
47
        $SQL = 'SELECT';
48
        if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,';
49
        if ($dateDrilldown === 'true') $SQL .= ' `dimension2`,';
50
        $SQL .= ' SUM(`dimension3`) AS `dimension3`';
51
        $SQL .= ' FROM `*PREFIX*analytics_facts`
52
                WHERE `dataset` = ?
53
                GROUP BY `dataset`';
54
        if ($objectDrilldown === 'true') $SQL .= ', `dimension1`';
55
        if ($dateDrilldown === 'true') $SQL .= ', `dimension2`';
56
        $SQL .= ' ORDER BY';
57
        if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,';
58
        $SQL .= ' `dimension2` ASC';
59
60
        //$this->logger->error($SQL);
61
62
        $stmt = $this->db->prepare($SQL);
63
        $stmt->execute(array($dataset));
64
        return $stmt->fetchAll();
65
    }
66
67
    /**
68
     * delete data
69
     */
70
    public function deleteData(int $datasetId, $dimension1, $dimension2)
71
    {
72
        $dimension1 = str_replace('*', '%', $dimension1);
73
        $dimension2 = str_replace('*', '%', $dimension2);
74
        $SQL = 'DELETE FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` like ? AND `dimension2` like ?';
75
        $stmt = $this->db->prepare($SQL);
76
        $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2));
77
        return true;
78
    }
79
80
    /**
81
     * Simulate delete data
82
     * @param int $datasetId
83
     * @param $dimension1
84
     * @param $dimension2
85
     * @param $dimension3
86
     * @return array
87
     */
88
    public function deleteDataSimulate(int $datasetId, $dimension1, $dimension2, $dimension3)
0 ignored issues
show
Unused Code introduced by
The parameter $dimension3 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

88
    public function deleteDataSimulate(int $datasetId, $dimension1, $dimension2, /** @scrutinizer ignore-unused */ $dimension3)

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...
89
    {
90
        $dimension1 = str_replace('*', '%', $dimension1);
91
        $dimension2 = str_replace('*', '%', $dimension2);
92
        $SQL = 'select count(*) FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` like ? AND `dimension2` like ?';
93
        $stmt = $this->db->prepare($SQL);
94
        $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2));
95
        return $stmt->fetch();
96
    }
97
98
    /**
99
     * delete all data of a dataset
100
     */
101
    public function deleteDataByDataset(int $datasetId)
102
    {
103
        $SQL = 'DELETE FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ?';
104
        $stmt = $this->db->prepare($SQL);
105
        $stmt->execute(array($this->userId, $datasetId));
106
        return true;
107
    }
108
109
    /**
110
     * create data
111
     * @param int $datasetId
112
     * @param $dimension1
113
     * @param $dimension2
114
     * @param $dimension3
115
     * @param string|null $user_id
116
     * @return string
117
     */
118
    public function createData(int $datasetId, $dimension1, $dimension2, $dimension3, string $user_id = null)
119
    {
120
        $dimension1 = str_replace('*', '', $dimension1);
121
        $dimension2 = str_replace('*', '', $dimension2);
122
        if ($user_id) $this->userId = $user_id;
123
        $SQL = 'SELECT `id` FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
124
        $stmt = $this->db->prepare($SQL);
125
        $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2));
126
        $row = $stmt->fetch();
127
        if ($row) {
128
            $SQL = 'UPDATE `*PREFIX*analytics_facts` SET `dimension3` = ? WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
129
            $stmt = $this->db->prepare($SQL);
130
            $stmt->execute(array($dimension3, $this->userId, $datasetId, $dimension1, $dimension2));
131
            //$stmt->fetch();
132
            return 'update';
133
        } else {
134
            $SQL = 'INSERT INTO `*PREFIX*analytics_facts` (`user_id`,`dataset`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?)';
135
            $stmt = $this->db->prepare($SQL);
136
            $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2, $dimension3));
137
            return 'insert';
138
        }
139
    }
140
}