Passed
Push — master ( 990f35...a2e3d3 )
by Marcel
03:12
created

DatasetMapper::getLastUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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\Db;
13
14
use OCP\IDBConnection;
15
use OCP\IL10N;
16
use Psr\Log\LoggerInterface;
17
18
class DatasetMapper
19
{
20
    private $userId;
21
    private $l10n;
22
    private $db;
23
    private $logger;
24
    const TABLE_NAME = 'analytics_dataset';
25
26
    public function __construct(
27
        $userId,
28
        IL10N $l10n,
29
        IDBConnection $db,
30
        LoggerInterface $logger
31
    )
32
    {
33
        $this->userId = $userId;
34
        $this->l10n = $l10n;
35
        $this->db = $db;
36
        $this->logger = $logger;
37
        self::TABLE_NAME;
38
    }
39
40
    /**
41
     * get datasets
42
     * @return array
43
     */
44
    public function index()
45
    {
46
        $sql = $this->db->getQueryBuilder();
47
        $sql->from(self::TABLE_NAME)
48
            ->select('id')
49
            ->addSelect('name')
50
            ->addSelect('type')
51
            ->addSelect('parent')
52
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
53
            ->orderBy('parent', 'ASC')
54
            ->addOrderBy('name', 'ASC');
55
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

55
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
56
        $result = $statement->fetchAll();
57
        $statement->closeCursor();
58
        return $result;
59
    }
60
61
    /**
62
     * create dataset
63
     * @return int
64
     */
65
    public function create()
66
    {
67
        $sql = $this->db->getQueryBuilder();
68
        $sql->insert(self::TABLE_NAME)
69
            ->values([
70
                'user_id' => $sql->createNamedParameter($this->userId),
71
                'name' => $sql->createNamedParameter($this->l10n->t('New')),
72
                'type' => $sql->createNamedParameter(2),
73
                'parent' => $sql->createNamedParameter(0),
74
                'dimension1' => $sql->createNamedParameter($this->l10n->t('Object')),
75
                'dimension2' => $sql->createNamedParameter($this->l10n->t('Date')),
76
                //'dimension3' => $sql->createNamedParameter($this->l10n->t('Value')),
77
                //'dimension4' => $sql->createNamedParameter($this->l10n->t('Value')),
78
                //'timestamp' => $sql->createNamedParameter($this->l10n->t('Date')),
79
                //'unit' => $sql->createNamedParameter($this->l10n->t('Value')),
80
                'value' => $sql->createNamedParameter($this->l10n->t('Value')),
81
                'chart' => $sql->createNamedParameter('column'),
82
                'visualization' => $sql->createNamedParameter('ct'),
83
            ]);
84
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

84
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
85
        return (int)$sql->getLastInsertId();
86
    }
87
88
    /**
89
     * get single dataset
90
     * @param int $id
91
     * @param string|null $user_id
92
     * @return array
93
     */
94
    public function read(int $id, string $user_id = null)
95
    {
96
        if ($user_id) $this->userId = $user_id;
97
98
        $sql = $this->db->getQueryBuilder();
99
        $sql->from(self::TABLE_NAME)
100
            ->select('*')
101
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)))
102
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
103
            ->orderBy('parent', 'ASC')
104
            ->addOrderBy('name', 'ASC');
105
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

105
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
106
        $result = $statement->fetch();
107
        $statement->closeCursor();
108
        return $result;
109
    }
110
111
    /**
112
     * update dataset
113
     * @param $id
114
     * @param $name
115
     * @param $subheader
116
     * @param $parent
117
     * @param $type
118
     * @param $link
119
     * @param $visualization
120
     * @param $chart
121
     * @param $chartoptions
122
     * @param $dataoptions
123
     * @param $dimension1
124
     * @param $dimension2
125
     * @param $value
126
     * @param $filteroptions
127
     * @return bool
128
     */
129
    public function update($id, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value, $filteroptions = null)
130
    {
131
        $name = $this->truncate($name, 64);
132
        $sql = $this->db->getQueryBuilder();
133
        $sql->update(self::TABLE_NAME)
134
            ->set('name', $sql->createNamedParameter($name))
135
            ->set('subheader', $sql->createNamedParameter($subheader))
136
            ->set('type', $sql->createNamedParameter($type))
137
            ->set('link', $sql->createNamedParameter($link))
138
            ->set('visualization', $sql->createNamedParameter($visualization))
139
            ->set('chart', $sql->createNamedParameter($chart))
140
            ->set('chartoptions', $sql->createNamedParameter($chartoptions))
141
            ->set('dataoptions', $sql->createNamedParameter($dataoptions))
142
            ->set('parent', $sql->createNamedParameter($parent))
143
            ->set('dimension1', $sql->createNamedParameter($dimension1))
144
            ->set('dimension2', $sql->createNamedParameter($dimension2))
145
            ->set('value', $sql->createNamedParameter($value))
146
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
147
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
148
        if ($filteroptions !== null) $sql->set('filteroptions', $sql->createNamedParameter($filteroptions));
149
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

149
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
150
        return true;
151
    }
152
153
    /**
154
     * update dataset options
155
     * @param $id
156
     * @param $chartoptions
157
     * @param $dataoptions
158
     * @param $filteroptions
159
     * @return bool
160
     */
161
    public function updateOptions($id, $chartoptions, $dataoptions, $filteroptions)
162
    {
163
        $sql = $this->db->getQueryBuilder();
164
        $sql->update(self::TABLE_NAME)
165
            ->set('chartoptions', $sql->createNamedParameter($chartoptions))
166
            ->set('dataoptions', $sql->createNamedParameter($dataoptions))
167
            ->set('filteroptions', $sql->createNamedParameter($filteroptions))
168
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
169
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
170
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

170
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
171
        return true;
172
    }
173
174
    /**
175
     * read dataset options
176
     * @param $id
177
     * @return array
178
     */
179
    public function readOptions($id)
180
    {
181
        $sql = $this->db->getQueryBuilder();
182
        $sql->from(self::TABLE_NAME)
183
            ->select('name')
184
            ->addSelect('visualization')
185
            ->addSelect('chart')
186
            ->addSelect('user_id')
187
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
188
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

188
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
189
        $result = $statement->fetch();
190
        $statement->closeCursor();
191
        return $result;
192
    }
193
194
    /**
195
     * delete dataset
196
     * @param $id
197
     * @return bool
198
     */
199
    public function delete($id)
200
    {
201
        $sql = $this->db->getQueryBuilder();
202
        $sql->delete(self::TABLE_NAME)
203
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
204
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
205
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

205
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
206
        return true;
207
    }
208
209
    /**
210
     * search datasets by searchstring
211
     * @param $searchString
212
     * @return array
213
     */
214
    public function search($searchString)
215
    {
216
        $sql = $this->db->getQueryBuilder();
217
        $sql->from(self::TABLE_NAME)
218
            ->select('id')
219
            ->addSelect('name')
220
            ->addSelect('type')
221
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
222
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
223
            ->orderBy('name', 'ASC');
224
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

224
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
225
        $result = $statement->fetchAll();
226
        $statement->closeCursor();
227
        return $result;
228
    }
229
230
    /**
231
     * get the newest timestamp of the data of a dataset
232
     * @param $datasetId
233
     * @return int
234
     */
235
    public function getLastUpdate($datasetId)
236
    {
237
        $sql = $this->db->getQueryBuilder();
238
        $sql->from('analytics_facts')
239
            ->select($sql->func()->max('timestamp'))
240
            ->where($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId)));
241
        $result = (int)$sql->execute()->fetchOne();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

241
        $result = (int)/** @scrutinizer ignore-deprecated */ $sql->execute()->fetchOne();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
242
        return $result;
243
    }
244
245
    /**
246
     * get the report owner
247
     * @param $datasetId
248
     * @return int
249
     */
250
    public function getOwner($datasetId)
251
    {
252
        $sql = $this->db->getQueryBuilder();
253
        $sql->from(self::TABLE_NAME)
254
            ->select('user_id')
255
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($datasetId)));
256
        $result = (string)$sql->execute()->fetchOne();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

256
        $result = (string)/** @scrutinizer ignore-deprecated */ $sql->execute()->fetchOne();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
257
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type string which is incompatible with the documented return type integer.
Loading history...
258
    }
259
260
    /**
261
     * truncates fiels do DB-field size
262
     *
263
     * @param $string
264
     * @param $length
265
     * @param $dots
266
     * @return string
267
     */
268
    private function truncate($string, $length, $dots = "...")
269
    {
270
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
271
    }
272
}