DbController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10

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
 * Audio Player Editor
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 Marcel Scherello
10
 */
11
12
namespace OCA\audioplayer_editor\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\IRequest;
16
use OCP\IL10N;
17
use OCP\IDbConnection;
18
use OCP\Share\IManager;
19
use OCP\ILogger;
20
use OCP\ITagManager;
21
22
/**
23
 * Controller class for main page.
24
 */
25
class DbController extends Controller
26
{
27
28
    private $userId;
29
    private $l10n;
30
    private $db;
31
    private $shareManager;
32
    private $tagManager;
33
    private $logger;
34
35
    public function __construct(
36
        $appName,
37
        IRequest $request,
38
        $userId,
39
        IL10N $l10n,
40
        IDbConnection $db,
41
        ITagManager $tagManager,
42
        IManager $shareManager,
43
        ILogger $logger
44
    )
45
    {
46
        parent::__construct($appName, $request);
47
        $this->userId = $userId;
48
        $this->l10n = $l10n;
49
        $this->db = $db;
50
        $this->shareManager = $shareManager;
51
        $this->tagManager = $tagManager;
52
        $this->logger = $logger;
53
    }
54
55
    /**
56
     * Add album to db if not exist
57
     * @param int $userId
58
     * @param string $sAlbum
59
     * @param string $sYear
60
     * @param int $iArtistId
61
     * @param int $parentId
62
     * @return array
63
     * @throws \Doctrine\DBAL\DBALException
64
     */
65
    public function writeAlbumToDB($userId, $sAlbum, $sYear, $iArtistId, $parentId)
66
    {
67
        $sAlbum = $this->truncate($sAlbum, '256');
68
        $sYear = $this->normalizeInteger($sYear);
69
        $AlbumCount = 0;
70
71
        $stmt = $this->db->prepare('SELECT `id`, `artist_id` FROM `*PREFIX*audioplayer_albums` WHERE `user_id` = ? AND `name` = ? AND `folder_id` = ?');
72
        $stmt->execute(array($userId, $sAlbum, $parentId));
73
        $row = $stmt->fetch();
74
        if ($row) {
75
            if ((int)$row['artist_id'] !== (int)$iArtistId) {
76
                $various_id = $this->writeArtistToDB($userId, $this->l10n->t('Various Artists'));
77
                $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `artist_id`= ? WHERE `id` = ? AND `user_id` = ?');
78
                $stmt->execute(array($various_id, $row['id'], $userId));
79
            }
80
            $insertid = $row['id'];
81
        } else {
82
            $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_albums` (`user_id`,`name`,`folder_id`) VALUES(?,?,?)');
83
            $stmt->execute(array($userId, $sAlbum, $parentId));
84
            $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_albums');
85
            if ($iArtistId) {
86
                $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `year`= ?, `artist_id`= ? WHERE `id` = ? AND `user_id` = ?');
87
                $stmt->execute(array((int)$sYear, $iArtistId, $insertid, $userId));
88
            } else {
89
                $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `year`= ? WHERE `id` = ? AND `user_id` = ?');
90
                $stmt->execute(array((int)$sYear, $insertid, $userId));
91
            }
92
            $AlbumCount = 1;
93
        }
94
95
        $return = [
96
            'id' => $insertid,
97
            'state' => true,
98
            'albumcount' => $AlbumCount,
99
        ];
100
        return $return;
101
    }
102
103
    /**
104
     * truncates fiels do DB-field size
105
     *
106
     * @param $string
107
     * @param $length
108
     * @param $dots
109
     * @return string
110
     */
111
    private function truncate($string, $length, $dots = "...")
112
    {
113
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
114
    }
115
116
    /**
117
     * validate unsigned int values
118
     *
119
     * @param string $value
120
     * @return int value
121
     */
122
    private function normalizeInteger($value)
123
    {
124
        // convert format '1/10' to '1' and '-1' to null
125
        $tmp = explode('/', $value);
126
        $tmp = explode('-', $tmp[0]);
127
        $value = $tmp[0];
128
        if (is_numeric($value) && ((int)$value) > 0) {
129
            $value = (int)$value;
130
        } else {
131
            $value = 0;
132
        }
133
        return $value;
134
    }
135
136
    /**
137
     * Add artist to db if not exist
138
     * @param int $userId
139
     * @param string $sArtist
140
     * @return int
141
     */
142
    public function writeArtistToDB($userId, $sArtist)
143
    {
144
        $sArtist = $this->truncate($sArtist, '256');
145
146
        $stmt = $this->db->prepare('SELECT `id` FROM `*PREFIX*audioplayer_artists` WHERE `user_id` = ? AND `name` = ?');
147
        $stmt->execute(array($userId, $sArtist));
148
        $row = $stmt->fetch();
149
        if ($row) {
150
            return $row['id'];
151
        } else {
152
            $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_artists` (`user_id`,`name`) VALUES(?,?)');
153
            $stmt->execute(array($userId, $sArtist));
154
            $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_artists');
155
            return $insertid;
156
        }
157
    }
158
159
    /**
160
     * Add genre to db if not exist
161
     * @param int $userId
162
     * @param string $sGenre
163
     * @return int
164
     */
165
    public function writeGenreToDB($userId, $sGenre)
166
    {
167
        $sGenre = $this->truncate($sGenre, '256');
168
169
        $stmt = $this->db->prepare('SELECT `id` FROM `*PREFIX*audioplayer_genre` WHERE `user_id` = ? AND `name` = ?');
170
        $stmt->execute(array($userId, $sGenre));
171
        $row = $stmt->fetch();
172
        if ($row) {
173
            return $row['id'];
174
        } else {
175
            $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_genre` (`user_id`,`name`) VALUES(?,?)');
176
            $stmt->execute(array($userId, $sGenre));
177
            $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_genre');
178
            return $insertid;
179
        }
180
    }
181
182
    /**
183
     * Get file id for single track
184
     * @param int $trackId
185
     * @return int
186
     */
187
    public function getFileId($trackId)
188
    {
189
        $SQL = "SELECT `file_id` FROM `*PREFIX*audioplayer_tracks` WHERE  `user_id` = ? AND `id` = ?";
190
        $stmt = $this->db->prepare($SQL);
191
        $stmt->execute(array($this->userId, $trackId));
192
        $row = $stmt->fetch();
193
        return $row['file_id'];
194
    }
195
196
    /**
197
     * Add track to db if not exist
198
     * @param int $userId
199
     * @param int $track_id
200
     * @param string $editKey
201
     * @param string $editValue
202
     * @return bool
203
     */
204
    public function updateTrack($userId, $track_id, $editKey, $editValue)
205
    {
206
        $SQL = 'UPDATE `*PREFIX*audioplayer_tracks` SET `' . $editKey . '` = ? WHERE `user_id` = ? AND `id` = ?';
207
        $stmt = $this->db->prepare($SQL);
208
        $stmt->execute(array($editValue,
209
            $userId,
210
            $track_id
211
        ));
212
        return true;
213
    }
214
215
}
216