Passed
Push — master ( 7c42c4...9333e3 )
by Marcel
04:41 queued 14s
created

PanoramaMapper::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 4
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
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 2019-2022 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Db;
13
14
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...
15
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...
16
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...
17
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...
18
19
class PanoramaMapper
20
{
21
    private $userId;
22
    private $l10n;
23
    private $db;
24
    private $logger;
25
    const TABLE_NAME = 'analytics_panorama';
26
27
    public function __construct(
28
        $userId,
29
        IL10N $l10n,
30
        IDBConnection $db,
31
        LoggerInterface $logger
32
    )
33
    {
34
        $this->userId = $userId;
35
        $this->l10n = $l10n;
36
        $this->db = $db;
37
        $this->logger = $logger;
38
    }
39
40
    /**
41
     * get reports
42
     * @return array
43
     * @throws Exception
44
     */
45
    public function index()
46
    {
47
        $sql = $this->db->getQueryBuilder();
48
        $sql->from(self::TABLE_NAME)
49
            ->select('*')
50
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
51
            ->orderBy('name', 'ASC');
52
        $statement = $sql->executeQuery();
53
        $result = $statement->fetchAll();
54
        $statement->closeCursor();
55
56
        return $result;
57
    }
58
59
    /**
60
     * create report
61
     * @param $name
62
     * @param $type
63
     * @param $parent
64
     * @param $pages
65
     * @return int
66
     * @throws Exception
67
     */
68
    public function create($name, $type, $parent, $pages)
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
                'type' => $sql->createNamedParameter($type),
76
                'parent' => $sql->createNamedParameter($parent),
77
                'pages' => $sql->createNamedParameter($pages),
78
            ]);
79
        $sql->executeStatement();
80
        return (int)$sql->getLastInsertId();
81
    }
82
83
    /**
84
     * get single panorama including subpages for user
85
     * @param int $id
86
     * @return array
87
     * @throws Exception
88
     */
89
    public function readOwn(int $id)
90
    {
91
        $sql = $this->db->getQueryBuilder();
92
        $sql->from(self::TABLE_NAME)
93
            ->select('*')
94
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)))
95
            ->orWhere($sql->expr()->eq('parent', $sql->createNamedParameter($id)))
96
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
97
            ->orderBy('name', 'ASC');
98
        $statement = $sql->executeQuery();
99
        $result = $statement->fetch();
100
        $statement->closeCursor();
101
102
        return $result;
103
    }
104
105
    /**
106
     * update report
107
     * @param $id
108
     * @param $name
109
     * @param $type
110
     * @param $parent
111
     * @param $pages
112
     * @return bool
113
     * @throws Exception
114
     */
115
    public function update($id, $name, $type, $parent, $pages)
116
    {
117
        $name = $this->truncate($name, 64);
118
        $sql = $this->db->getQueryBuilder();
119
        $sql->update(self::TABLE_NAME)
120
            ->set('name', $sql->createNamedParameter($name))
121
            ->set('type', $sql->createNamedParameter($type))
122
            ->set('parent', $sql->createNamedParameter($parent))
123
            ->set('pages', $sql->createNamedParameter($pages))
124
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
125
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
126
         $sql->executeStatement();
127
        return true;
128
    }
129
130
    /**
131
     * delete report
132
     * @param $id
133
     * @return bool
134
     * @throws Exception
135
     */
136
    public function delete($id)
137
    {
138
        $sql = $this->db->getQueryBuilder();
139
        $sql->delete(self::TABLE_NAME)
140
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
141
        $sql->executeStatement();
142
        return true;
143
    }
144
145
    /**
146
     * search reports by search string
147
     * @param $searchString
148
     * @return array
149
     * @throws Exception
150
     */
151
    public function search($searchString)
152
    {
153
        $sql = $this->db->getQueryBuilder();
154
        $sql->from(self::TABLE_NAME)
155
            ->select('id')
156
            ->addSelect('name')
157
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
158
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
159
            ->orderBy('name', 'ASC');
160
        $statement = $sql->executeQuery();
161
        $result = $statement->fetchAll();
162
        $statement->closeCursor();
163
164
        return $result;
165
    }
166
167
168
    /**
169
     * truncates fiels do DB-field size
170
     *
171
     * @param $string
172
     * @param $length
173
     * @param $dots
174
     * @return string
175
     */
176
    private function truncate($string, $length, $dots = "...")
177
    {
178
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
179
    }
180
}