Passed
Push — master ( 7cb5a0...7ee6da )
by Marcel
03:06
created

DatasetMapper::updateOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 9.9666
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
     * update dataset options
176
     * @param $id
177
     * @param $chartoptions
178
     * @param $dataoptions
179
     * @param $filteroptions
180
     * @return bool
181
     */
182
    public function updateRefresh($id, $refresh)
183
    {
184
        $sql = $this->db->getQueryBuilder();
185
        $sql->update(self::TABLE_NAME)
186
            ->set('refresh', $sql->createNamedParameter($refresh))
187
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
188
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
189
        $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

189
        /** @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...
190
        return true;
191
    }
192
193
    /**
194
     * read dataset options
195
     * @param $id
196
     * @return array
197
     */
198
    public function readOptions($id)
199
    {
200
        $sql = $this->db->getQueryBuilder();
201
        $sql->from(self::TABLE_NAME)
202
            ->select('name')
203
            ->addSelect('visualization')
204
            ->addSelect('chart')
205
            ->addSelect('user_id')
206
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
207
        $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

207
        $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...
208
        $result = $statement->fetch();
209
        $statement->closeCursor();
210
        return $result;
211
    }
212
213
    /**
214
     * delete dataset
215
     * @param $id
216
     * @return bool
217
     */
218
    public function delete($id)
219
    {
220
        $sql = $this->db->getQueryBuilder();
221
        $sql->delete(self::TABLE_NAME)
222
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
223
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
224
        $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
        /** @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
        return true;
226
    }
227
228
    /**
229
     * search datasets by searchstring
230
     * @param $searchString
231
     * @return array
232
     */
233
    public function search($searchString)
234
    {
235
        $sql = $this->db->getQueryBuilder();
236
        $sql->from(self::TABLE_NAME)
237
            ->select('id')
238
            ->addSelect('name')
239
            ->addSelect('type')
240
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
241
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
242
            ->orderBy('name', 'ASC');
243
        $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

243
        $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...
244
        $result = $statement->fetchAll();
245
        $statement->closeCursor();
246
        return $result;
247
    }
248
249
    /**
250
     * get the newest timestamp of the data of a dataset
251
     * @param $datasetId
252
     * @return int
253
     */
254
    public function getLastUpdate($datasetId)
255
    {
256
        $sql = $this->db->getQueryBuilder();
257
        $sql->from('analytics_facts')
258
            ->select($sql->func()->max('timestamp'))
259
            ->where($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId)));
260
        $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

260
        $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...
261
        return $result;
262
    }
263
264
    /**
265
     * get the report owner
266
     * @param $datasetId
267
     * @return int
268
     */
269
    public function getOwner($datasetId)
270
    {
271
        $sql = $this->db->getQueryBuilder();
272
        $sql->from(self::TABLE_NAME)
273
            ->select('user_id')
274
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($datasetId)));
275
        $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

275
        $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...
276
        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...
277
    }
278
279
    /**
280
     * truncates fiels do DB-field size
281
     *
282
     * @param $string
283
     * @param $length
284
     * @param $dots
285
     * @return string
286
     */
287
    private function truncate($string, $length, $dots = "...")
288
    {
289
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
290
    }
291
}