Passed
Push — master ( 0b1a33...6372a5 )
by Marcel
06:49
created

DatasetMapper::getOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
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\DB\Exception;
15
use OCP\IDBConnection;
16
use Psr\Log\LoggerInterface;
17
18
class DatasetMapper
19
{
20
    private $userId;
21
    private $db;
22
    private $logger;
23
    const TABLE_NAME = 'analytics_dataset';
24
25
    public function __construct(
26
        $userId,
27
        IDBConnection $db,
28
        LoggerInterface $logger
29
    )
30
    {
31
        $this->userId = $userId;
32
        $this->db = $db;
33
        $this->logger = $logger;
34
    }
35
36
    /**
37
     * get datasets
38
     * @return array
39
     * @throws Exception
40
     */
41
    public function index(): array
42
    {
43
        $sql = $this->db->getQueryBuilder();
44
        $sql->from(self::TABLE_NAME)
45
            ->select('id')
46
            ->addSelect('name')
47
            ->addSelect('dimension1')
48
            ->addSelect('dimension2')
49
            ->addSelect('value')
50
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
51
            ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter('2')))
52
            ->addOrderBy('name', 'ASC');
53
        $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

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

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

102
        $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...
103
        $result = $statement->fetch();
104
        $statement->closeCursor();
105
        return $result;
106
    }
107
108
    /**
109
     * update dataset
110
     * @param $id
111
     * @param $name
112
     * @param $dimension1
113
     * @param $dimension2
114
     * @param $value
115
     * @return bool
116
     * @throws Exception
117
     */
118
    public function update($id, $name, $dimension1, $dimension2, $value): bool
119
    {
120
        $name = $this->truncate($name, 64);
121
        $sql = $this->db->getQueryBuilder();
122
        $sql->update(self::TABLE_NAME)
123
            ->set('name', $sql->createNamedParameter($name))
124
            ->set('dimension1', $sql->createNamedParameter($dimension1))
125
            ->set('dimension2', $sql->createNamedParameter($dimension2))
126
            ->set('value', $sql->createNamedParameter($value))
127
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
128
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
129
        $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

129
        /** @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...
130
        return true;
131
    }
132
133
    /**
134
     * delete dataset
135
     * @param $id
136
     * @return bool
137
     * @throws Exception
138
     */
139
    public function delete($id): bool
140
    {
141
        $sql = $this->db->getQueryBuilder();
142
        $sql->delete(self::TABLE_NAME)
143
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
144
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
145
        $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

145
        /** @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...
146
        return true;
147
    }
148
149
    /**
150
     * get the newest timestamp of the data of a dataset
151
     * @param $datasetId
152
     * @return int
153
     * @throws Exception
154
     */
155
    public function getLastUpdate($datasetId): int
156
    {
157
        $sql = $this->db->getQueryBuilder();
158
        $sql->from('analytics_facts')
159
            ->select($sql->func()->max('timestamp'))
160
            ->where($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId)));
161
        return (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

161
        return (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...
162
    }
163
164
    /**
165
     * get the report owner
166
     * @param $datasetId
167
     * @return string
168
     * @throws Exception
169
     */
170
    public function getOwner($datasetId): string
171
    {
172
        $sql = $this->db->getQueryBuilder();
173
        $sql->from(self::TABLE_NAME)
174
            ->select('user_id')
175
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($datasetId)));
176
        return (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

176
        return (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...
177
    }
178
179
    /**
180
     * truncates fields do DB-field size
181
     *
182
     * @param $string
183
     * @param int $length
184
     * @param string $dots
185
     * @return string
186
     */
187
    private function truncate($string, int $length, string $dots = "..."): string
188
    {
189
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
190
    }
191
}