Passed
Push — master ( 1330f4...591d77 )
by Marcel
05:54 queued 15s
created

PanoramaMapper::indexByUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
rs 10
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
     * update report
122
     * @param $id
123
     * @param $name
124
     * @param $type
125
     * @param $parent
126
     * @param $pages
127
     * @return bool
128
     * @throws Exception
129
     */
130
    public function update($id, $name, $type, $parent, $pages)
131
    {
132
        $name = $this->truncate($name, 64);
133
        $sql = $this->db->getQueryBuilder();
134
        $sql->update(self::TABLE_NAME)
135
            ->set('name', $sql->createNamedParameter($name))
136
            ->set('type', $sql->createNamedParameter($type))
137
            ->set('parent', $sql->createNamedParameter($parent))
138
            ->set('pages', $sql->createNamedParameter($pages))
139
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
140
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
141
         $sql->executeStatement();
142
        return true;
143
    }
144
145
    /**
146
     * delete report
147
     * @param $id
148
     * @return bool
149
     * @throws Exception
150
     */
151
    public function delete($id)
152
    {
153
        $sql = $this->db->getQueryBuilder();
154
        $sql->delete(self::TABLE_NAME)
155
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
156
        $sql->executeStatement();
157
        return true;
158
    }
159
160
    /**
161
     * search reports by search string
162
     * @param $searchString
163
     * @return array
164
     * @throws Exception
165
     */
166
    public function search($searchString)
167
    {
168
        $sql = $this->db->getQueryBuilder();
169
        $sql->from(self::TABLE_NAME)
170
            ->select('id')
171
            ->addSelect('name')
172
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
173
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
174
            ->orderBy('name', 'ASC');
175
        $statement = $sql->executeQuery();
176
        $result = $statement->fetchAll();
177
        $statement->closeCursor();
178
179
        return $result;
180
    }
181
182
183
    /**
184
     * truncates fiels do DB-field size
185
     *
186
     * @param $string
187
     * @param $length
188
     * @param $dots
189
     * @return string
190
     */
191
    private function truncate($string, $length, $dots = "...")
192
    {
193
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
194
    }
195
}