Passed
Push — master ( c70a6b...e1aeb2 )
by Marcel
05:29 queued 16s
created

StoryMapper::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 16
rs 9.7998

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 StoryMapper
20
{
21
    private $userId;
22
    private $l10n;
23
    private $db;
24
    private $logger;
25
    const TABLE_NAME = 'analytics_story';
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
            ->andWhere($sql->expr()->eq('page', $sql->createNamedParameter('0')))
52
            ->orderBy('name', 'ASC');
53
        $statement = $sql->executeQuery();
54
        $result = $statement->fetchAll();
55
        $statement->closeCursor();
56
57
        return $result;
58
    }
59
60
    /**
61
     * create report
62
     * @param $name
63
     * @param $subheader
64
     * @param $type
65
     * @param $page
66
     * @param $parent
67
     * @param $reports
68
     * @param $layout
69
     * @return int
70
     * @throws Exception
71
     */
72
    public function create($name, $subheader, $type, $page, $parent, $reports, $layout)
73
    {
74
        $sql = $this->db->getQueryBuilder();
75
        $sql->insert(self::TABLE_NAME)
76
            ->values([
77
                'user_id' => $sql->createNamedParameter($this->userId),
78
                'name' => $sql->createNamedParameter($name),
79
                'subheader' => $sql->createNamedParameter($subheader),
80
                'type' => $sql->createNamedParameter($type),
81
                'page' => $sql->createNamedParameter($page),
82
                'parent' => $sql->createNamedParameter($parent),
83
                'reports' => $sql->createNamedParameter($reports),
84
                'layout' => $sql->createNamedParameter($layout),
85
            ]);
86
        $sql->executeStatement();
87
        return (int)$sql->getLastInsertId();
88
    }
89
90
    /**
91
     * get single story including subpages for user
92
     * @param int $id
93
     * @return array
94
     * @throws Exception
95
     */
96
    public function readOwn(int $id)
97
    {
98
        $sql = $this->db->getQueryBuilder();
99
        $sql->from(self::TABLE_NAME)
100
            ->select('*')
101
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)))
102
            ->orWhere($sql->expr()->eq('parent', $sql->createNamedParameter($id)))
103
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
104
            ->orderBy('page', 'ASC');
105
        $statement = $sql->executeQuery();
106
        $result = $statement->fetch();
107
        $statement->closeCursor();
108
109
        return $result;
110
    }
111
112
    /**
113
     * update report
114
     * @param $id
115
     * @param $name
116
     * @param $subheader
117
     * @param $type
118
     * @param $page
119
     * @param $parent
120
     * @param $reports
121
     * @param $layout
122
     * @return bool
123
     * @throws Exception
124
     */
125
    public function update($id, $name, $subheader, $type, $page, $parent, $reports, $layout)
126
    {
127
        $name = $this->truncate($name, 64);
128
        $sql = $this->db->getQueryBuilder();
129
        $sql->update(self::TABLE_NAME)
130
            ->set('name', $sql->createNamedParameter($name))
131
            ->set('subheader', $sql->createNamedParameter($subheader))
132
            ->set('type', $sql->createNamedParameter($type))
133
            ->set('page', $sql->createNamedParameter($page))
134
            ->set('parent', $sql->createNamedParameter($parent))
135
            ->set('reports', $sql->createNamedParameter($reports))
136
            ->set('layout', $sql->createNamedParameter($layout))
137
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
138
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id)));
139
         $sql->executeStatement();
140
        return true;
141
    }
142
143
    /**
144
     * delete report
145
     * @param $id
146
     * @return bool
147
     * @throws Exception
148
     */
149
    public function delete($id)
150
    {
151
        $sql = $this->db->getQueryBuilder();
152
        $sql->delete(self::TABLE_NAME)
153
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
154
        $sql->executeStatement();
155
        return true;
156
    }
157
158
    /**
159
     * search reports by search string
160
     * @param $searchString
161
     * @return array
162
     * @throws Exception
163
     */
164
    public function search($searchString)
165
    {
166
        $sql = $this->db->getQueryBuilder();
167
        $sql->from(self::TABLE_NAME)
168
            ->select('id')
169
            ->addSelect('name')
170
            ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
171
            ->andWhere($sql->expr()->iLike('name', $sql->createNamedParameter('%' . $this->db->escapeLikeParameter($searchString) . '%')))
172
            ->orderBy('name', 'ASC');
173
        $statement = $sql->executeQuery();
174
        $result = $statement->fetchAll();
175
        $statement->closeCursor();
176
177
        return $result;
178
    }
179
180
181
    /**
182
     * truncates fiels do DB-field size
183
     *
184
     * @param $string
185
     * @param $length
186
     * @param $dots
187
     * @return string
188
     */
189
    private function truncate($string, $length, $dots = "...")
190
    {
191
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
192
    }
193
}