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

DatasetMapper::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.8666
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('dimension1')
51
            ->addSelect('dimension2')
52
            ->addSelect('value')
53
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
54
            ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter('2')))
55
            ->addOrderBy('name', 'ASC');
56
        $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

56
        $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...
57
        $result = $statement->fetchAll();
58
        $statement->closeCursor();
59
        return $result;
60
    }
61
62
    /**
63
     * create dataset
64
     * @param $name
65
     * @param $dimension1
66
     * @param $dimension2
67
     * @param $value
68
     * @return int
69
     * @throws \OCP\DB\Exception
70
     */
71
    public function create($name, $dimension1, $dimension2, $value)
72
    {
73
        $sql = $this->db->getQueryBuilder();
74
        $sql->insert(self::TABLE_NAME)
75
            ->values([
76
                'user_id' => $sql->createNamedParameter($this->userId),
77
                'name' => $sql->createNamedParameter($name),
78
                'dimension1' => $sql->createNamedParameter($dimension1),
79
                'dimension2' => $sql->createNamedParameter($dimension2),
80
                'value' => $sql->createNamedParameter($value),
81
                'type' => $sql->createNamedParameter('2'),
82
            ]);
83
        $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

83
        /** @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...
84
        return (int)$sql->getLastInsertId();
85
    }
86
87
    /**
88
     * get single dataset
89
     * @param int $id
90
     * @param string|null $user_id
91
     * @return array
92
     */
93
    public function read(int $id, string $user_id = null)
94
    {
95
        if ($user_id) $this->userId = $user_id;
96
97
        $sql = $this->db->getQueryBuilder();
98
        $sql->from(self::TABLE_NAME)
99
            ->select('*')
100
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)))
101
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
102
            ->orderBy('parent', 'ASC')
103
            ->addOrderBy('name', 'ASC');
104
        $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

104
        $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...
105
        $result = $statement->fetch();
106
        $statement->closeCursor();
107
        return $result;
108
    }
109
110
    /**
111
     * update dataset
112
     * @param $id
113
     * @param $name
114
     * @param $subheader
115
     * @param $parent
116
     * @param $type
117
     * @param $link
118
     * @param $visualization
119
     * @param $chart
120
     * @param $chartoptions
121
     * @param $dataoptions
122
     * @param $dimension1
123
     * @param $dimension2
124
     * @param $value
125
     * @param $filteroptions
126
     * @return bool
127
     */
128
    public function update($id, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value, $filteroptions = null)
129
    {
130
        $name = $this->truncate($name, 64);
131
        $sql = $this->db->getQueryBuilder();
132
        $sql->update(self::TABLE_NAME)
133
            ->set('name', $sql->createNamedParameter($name))
134
            ->set('subheader', $sql->createNamedParameter($subheader))
135
            ->set('type', $sql->createNamedParameter($type))
136
            ->set('link', $sql->createNamedParameter($link))
137
            ->set('visualization', $sql->createNamedParameter($visualization))
138
            ->set('chart', $sql->createNamedParameter($chart))
139
            ->set('chartoptions', $sql->createNamedParameter($chartoptions))
140
            ->set('dataoptions', $sql->createNamedParameter($dataoptions))
141
            ->set('parent', $sql->createNamedParameter($parent))
142
            ->set('dimension1', $sql->createNamedParameter($dimension1))
143
            ->set('dimension2', $sql->createNamedParameter($dimension2))
144
            ->set('value', $sql->createNamedParameter($value))
145
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
146
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
147
        if ($filteroptions !== null) $sql->set('filteroptions', $sql->createNamedParameter($filteroptions));
148
        $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

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

169
        /** @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...
170
        return true;
171
    }
172
173
    /**
174
     * read dataset options
175
     * @param $id
176
     * @return array
177
     */
178
    public function readOptions($id)
179
    {
180
        $sql = $this->db->getQueryBuilder();
181
        $sql->from(self::TABLE_NAME)
182
            ->select('name')
183
            ->addSelect('visualization')
184
            ->addSelect('chart')
185
            ->addSelect('user_id')
186
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
187
        $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

187
        $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...
188
        $result = $statement->fetch();
189
        $statement->closeCursor();
190
        return $result;
191
    }
192
193
    /**
194
     * delete dataset
195
     * @param $id
196
     * @return bool
197
     */
198
    public function delete($id)
199
    {
200
        $sql = $this->db->getQueryBuilder();
201
        $sql->delete(self::TABLE_NAME)
202
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
203
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
204
        $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

204
        /** @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...
205
        return true;
206
    }
207
208
    /**
209
     * get the newest timestamp of the data of a dataset
210
     * @param $datasetId
211
     * @return int
212
     */
213
    public function getLastUpdate($datasetId)
214
    {
215
        $sql = $this->db->getQueryBuilder();
216
        $sql->from('analytics_facts')
217
            ->select($sql->func()->max('timestamp'))
218
            ->where($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId)));
219
        $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

219
        $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...
220
        return $result;
221
    }
222
223
    /**
224
     * get the report owner
225
     * @param $datasetId
226
     * @return int
227
     */
228
    public function getOwner($datasetId)
229
    {
230
        $sql = $this->db->getQueryBuilder();
231
        $sql->from(self::TABLE_NAME)
232
            ->select('user_id')
233
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($datasetId)));
234
        $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

234
        $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...
235
        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...
236
    }
237
238
    /**
239
     * truncates fiels do DB-field size
240
     *
241
     * @param $string
242
     * @param $length
243
     * @param $dots
244
     * @return string
245
     */
246
    private function truncate($string, $length, $dots = "...")
247
    {
248
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
249
    }
250
}