Passed
Push — master ( 591d77...ff0d74 )
by Marcel
04:58
created

PanoramaMapper::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Db;
10
11
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class PanoramaMapper
17
{
18
    private $userId;
19
    private $l10n;
20
    private $db;
21
    private $logger;
22
    const TABLE_NAME = 'analytics_panorama';
23
24
    public function __construct(
25
        $userId,
26
        IL10N $l10n,
27
        IDBConnection $db,
28
        LoggerInterface $logger
29
    )
30
    {
31
        $this->userId = $userId;
32
        $this->l10n = $l10n;
33
        $this->db = $db;
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * get reports
39
     * @return array
40
     * @throws Exception
41
     */
42
    public function index()
43
    {
44
        $sql = $this->db->getQueryBuilder();
45
        $sql->from(self::TABLE_NAME)
46
            ->select('*')
47
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
48
            ->orderBy('name', 'ASC');
49
        $statement = $sql->executeQuery();
50
        $result = $statement->fetchAll();
51
        $statement->closeCursor();
52
53
        return $result;
54
    }
55
56
	/**
57
	 * get reports for user
58
	 * @param $userId
59
	 * @return array
60
	 * @throws Exception
61
	 */
62
	public function indexByUser($userId)
63
	{
64
		$sql = $this->db->getQueryBuilder();
65
		$sql->from(self::TABLE_NAME)
66
			->select('id')
67
			->where($sql->expr()->eq('user_id', $sql->createNamedParameter($userId)));
68
		$statement = $sql->executeQuery();
69
		$result = $statement->fetchAll();
70
		$statement->closeCursor();
71
		return $result;
72
	}
73
74
	/**
75
     * create report
76
     * @param $name
77
     * @param $type
78
     * @param $parent
79
     * @param $pages
80
     * @return int
81
     * @throws Exception
82
     */
83
    public function create($name, $type, $parent, $pages)
84
    {
85
        $sql = $this->db->getQueryBuilder();
86
        $sql->insert(self::TABLE_NAME)
87
            ->values([
88
                'user_id' => $sql->createNamedParameter($this->userId),
89
                'name' => $sql->createNamedParameter($name),
90
                'type' => $sql->createNamedParameter($type),
91
                'parent' => $sql->createNamedParameter($parent),
92
                'pages' => $sql->createNamedParameter($pages),
93
            ]);
94
        $sql->executeStatement();
95
        return (int)$sql->getLastInsertId();
96
    }
97
98
    /**
99
     * get single panorama including subpages for user
100
     * @param int $id
101
     * @return array
102
     * @throws Exception
103
     */
104
    public function readOwn(int $id)
105
    {
106
        $sql = $this->db->getQueryBuilder();
107
        $sql->from(self::TABLE_NAME)
108
            ->select('*')
109
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)))
110
            ->orWhere($sql->expr()->eq('parent', $sql->createNamedParameter($id)))
111
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
112
            ->orderBy('name', 'ASC');
113
        $statement = $sql->executeQuery();
114
        $result = $statement->fetch();
115
        $statement->closeCursor();
116
117
        return $result;
118
    }
119
120
	/**
121
	 * get single report
122
	 * @param int $id
123
	 * @return array
124
	 * @throws Exception
125
	 */
126
	public function read(int $id)
127
	{
128
		$sql = $this->db->getQueryBuilder();
129
		$sql->from(self::TABLE_NAME)
130
			->select('*')
131
			->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
132
		$statement = $sql->executeQuery();
133
		$result = $statement->fetch();
134
		$statement->closeCursor();
135
136
		if ($result !== false) {
137
			$result['type'] = (int) $result['type'];
138
			$result['parent'] = (int) $result['parent'];
139
		}
140
141
		return $result;
142
	}
143
144
	/**
145
     * update report
146
     * @param $id
147
     * @param $name
148
     * @param $type
149
     * @param $parent
150
     * @param $pages
151
     * @return bool
152
     * @throws Exception
153
     */
154
    public function update($id, $name, $type, $parent, $pages)
155
    {
156
        $name = $this->truncate($name, 64);
157
        $sql = $this->db->getQueryBuilder();
158
        $sql->update(self::TABLE_NAME)
159
            ->set('name', $sql->createNamedParameter($name))
160
            ->set('type', $sql->createNamedParameter($type))
161
            ->set('parent', $sql->createNamedParameter($parent))
162
            ->set('pages', $sql->createNamedParameter($pages))
163
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
164
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
165
         $sql->executeStatement();
166
        return true;
167
    }
168
169
    /**
170
     * delete report
171
     * @param $id
172
     * @return bool
173
     * @throws Exception
174
     */
175
    public function delete($id)
176
    {
177
        $sql = $this->db->getQueryBuilder();
178
        $sql->delete(self::TABLE_NAME)
179
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
180
        $sql->executeStatement();
181
        return true;
182
    }
183
184
    /**
185
     * search reports by search string
186
     * @param $searchString
187
     * @return array
188
     * @throws Exception
189
     */
190
    public function search($searchString)
191
    {
192
        $sql = $this->db->getQueryBuilder();
193
        $sql->from(self::TABLE_NAME)
194
            ->select('id')
195
            ->addSelect('name')
196
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
197
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
198
            ->orderBy('name', 'ASC');
199
        $statement = $sql->executeQuery();
200
        $result = $statement->fetchAll();
201
        $statement->closeCursor();
202
203
        return $result;
204
    }
205
206
207
    /**
208
     * truncates fiels do DB-field size
209
     *
210
     * @param $string
211
     * @param $length
212
     * @param $dots
213
     * @return string
214
     */
215
    private function truncate($string, $length, $dots = "...")
216
    {
217
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
218
    }
219
}