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

ReportMapper::readOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9
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 ReportMapper
19
{
20
    private $userId;
21
    private $l10n;
22
    private $db;
23
    private $logger;
24
    const TABLE_NAME = 'analytics_report';
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
    }
38
39
    /**
40
     * get reports
41
     * @return array
42
     */
43
    public function index()
44
    {
45
        $sql = $this->db->getQueryBuilder();
46
        $sql->from(self::TABLE_NAME)
47
            ->select('id')
48
            ->addSelect('name')
49
            ->addSelect('type')
50
            ->addSelect('parent')
51
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
52
            ->orderBy('parent', 'ASC')
53
            ->addOrderBy('name', 'ASC');
54
        $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

54
        $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...
55
        $result = $statement->fetchAll();
56
        $statement->closeCursor();
57
        return $result;
58
    }
59
60
    /**
61
     * create report
62
     * @return int
63
     */
64
    public function create()
65
    {
66
        $sql = $this->db->getQueryBuilder();
67
        $sql->insert(self::TABLE_NAME)
68
            ->values([
69
                'user_id' => $sql->createNamedParameter($this->userId),
70
                'name' => $sql->createNamedParameter($this->l10n->t('New')),
71
                'type' => $sql->createNamedParameter(2),
72
                'parent' => $sql->createNamedParameter(0),
73
                'dimension1' => $sql->createNamedParameter($this->l10n->t('Object')),
74
                'dimension2' => $sql->createNamedParameter($this->l10n->t('Date')),
75
                //'dimension3' => $sql->createNamedParameter($this->l10n->t('Value')),
76
                //'dimension4' => $sql->createNamedParameter($this->l10n->t('Value')),
77
                //'timestamp' => $sql->createNamedParameter($this->l10n->t('Date')),
78
                //'unit' => $sql->createNamedParameter($this->l10n->t('Value')),
79
                'value' => $sql->createNamedParameter($this->l10n->t('Value')),
80
                'chart' => $sql->createNamedParameter('column'),
81
                'visualization' => $sql->createNamedParameter('ct'),
82
                'dataset' => $sql->createNamedParameter('0'),
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 report
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 report
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, $dataset, $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
            ->set('dataset', $sql->createNamedParameter($dataset))
147
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
148
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
149
        if ($filteroptions !== null) $sql->set('filteroptions', $sql->createNamedParameter($filteroptions));
150
        $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

150
        /** @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...
151
        return true;
152
    }
153
154
    /**
155
     * update report options
156
     * @param $id
157
     * @param $chartoptions
158
     * @param $dataoptions
159
     * @param $filteroptions
160
     * @return bool
161
     */
162
    public function updateOptions($id, $chartoptions, $dataoptions, $filteroptions)
163
    {
164
        $sql = $this->db->getQueryBuilder();
165
        $sql->update(self::TABLE_NAME)
166
            ->set('chartoptions', $sql->createNamedParameter($chartoptions))
167
            ->set('dataoptions', $sql->createNamedParameter($dataoptions))
168
            ->set('filteroptions', $sql->createNamedParameter($filteroptions))
169
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
170
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
171
        $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

171
        /** @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...
172
        return true;
173
    }
174
175
    /**
176
     * update report refresh interval
177
     * @param $id
178
     * @param $chartoptions
179
     * @param $dataoptions
180
     * @param $filteroptions
181
     * @return bool
182
     */
183
    public function updateRefresh($id, $refresh)
184
    {
185
        $sql = $this->db->getQueryBuilder();
186
        $sql->update(self::TABLE_NAME)
187
            ->set('refresh', $sql->createNamedParameter($refresh))
188
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
189
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
190
        $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

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

208
        $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...
209
        $result = $statement->fetch();
210
        $statement->closeCursor();
211
        return $result;
212
    }
213
214
    /**
215
     * delete report
216
     * @param $id
217
     * @return bool
218
     */
219
    public function delete($id)
220
    {
221
        $sql = $this->db->getQueryBuilder();
222
        $sql->delete(self::TABLE_NAME)
223
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
224
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
225
        $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

225
        /** @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...
226
        return true;
227
    }
228
229
    /**
230
     * search reports by searchstring
231
     * @param $searchString
232
     * @return array
233
     */
234
    public function search($searchString)
235
    {
236
        $sql = $this->db->getQueryBuilder();
237
        $sql->from(self::TABLE_NAME)
238
            ->select('id')
239
            ->addSelect('name')
240
            ->addSelect('type')
241
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
242
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
243
            ->orderBy('name', 'ASC');
244
        $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

244
        $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...
245
        $result = $statement->fetchAll();
246
        $statement->closeCursor();
247
        return $result;
248
    }
249
250
    /**
251
     * get the report owner
252
     * @param $reportId
253
     * @return int
254
     */
255
    public function getOwner($reportId)
256
    {
257
        $sql = $this->db->getQueryBuilder();
258
        $sql->from(self::TABLE_NAME)
259
            ->select('user_id')
260
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($reportId)));
261
        $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

261
        $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...
262
        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...
263
    }
264
265
    /**
266
     * truncates fiels do DB-field size
267
     *
268
     * @param $string
269
     * @param $length
270
     * @param $dots
271
     * @return string
272
     */
273
    private function truncate($string, $length, $dots = "...")
274
    {
275
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
276
    }
277
}