SidebarController::getAudioInfo()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 4
eloc 21
c 3
b 1
f 0
nc 8
nop 1
dl 0
loc 29
rs 9.584
1
<?php
2
/**
3
 * Audio Player
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 2016-2021 Marcel Scherello
10
 */
11
12
namespace OCA\audioplayer\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\AppFramework\Http\JSONResponse;
16
use OCP\Files\IRootFolder;
17
use OCP\IRequest;
18
use OCP\IL10N;
19
use OCP\IDBConnection;
20
use OCP\ITagManager;
21
22
/**
23
 * Controller class for Sidebar.
24
 */
25
class SidebarController extends Controller
26
{
27
28
    private $userId;
29
    private $db;
30
    private $l10n;
31
    private $tagger;
32
    private $tagManager;
33
    private $DBController;
34
    private $rootFolder;
35
36
    public function __construct(
37
        $appName,
38
        IRequest $request,
39
        $userId,
40
        IL10N $l10n,
41
        ITagManager $tagManager,
42
        IDBConnection $db,
43
        DbController $DBController,
44
        IRootFolder $rootFolder
45
    )
46
    {
47
        parent::__construct($appName, $request);
48
        $this->appName = $appName;
49
        $this->l10n = $l10n;
50
        $this->userId = $userId;
51
        $this->tagManager = $tagManager;
52
        $this->tagger = null;
53
        $this->db = $db;
54
        $this->DBController = $DBController;
55
        $this->rootFolder = $rootFolder;
56
    }
57
58
    /**
59
     * @NoAdminRequired
60
     * @param $trackid
61
     * @return JSONResponse
62
     */
63
    public function getAudioInfo($trackid)
64
    {
65
66
        $row = $this->DBController->getTrackInfo($trackid);
67
        $artist = $this->DBController->loadArtistsToAlbum($row['album_id'], $row['Album Artist']);
68
        $row['Album Artist'] = $artist;
69
70
        if ($row['Year'] === '0') $row['Year'] = $this->l10n->t('Unknown');
71
        if ($row['Bitrate'] !== '') $row['Bitrate'] = $row['Bitrate'] . ' kbps';
72
73
        array_splice($row, 15, 3);
74
75
        $fileId = $this->DBController->getFileId($trackid);
76
        $nodes = $this->rootFolder->getUserFolder($this->userId)->getById($fileId);
77
        $node = $nodes[0];
78
        $path = $this->rootFolder->getUserFolder($this->userId)->getRelativePath($node->getPath());
79
        $path = \join('/', \array_map('rawurlencode', \explode('/', $path)));
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $path = \join('/', \array_map('rawurlencode', \explode('/', /** @scrutinizer ignore-type */ $path)));
Loading history...
80
        $row['Path'] = $path;
81
82
        if ($row['Title']) {
83
            $result = [
84
                'status' => 'success',
85
                'data' => $row];
86
        } else {
87
            $result = [
88
                'status' => 'error',
89
                'data' => 'nodata'];
90
        }
91
        return new JSONResponse($result);
92
    }
93
94
    /**
95
     * @NoAdminRequired
96
     * @param $trackid
97
     * @return JSONResponse
98
     */
99
    public function getPlaylists($trackid)
100
    {
101
        $playlists = $this->DBController->getPlaylistsForTrack($this->userId, $trackid);
102
        if (!empty($playlists)) {
103
            $result = [
104
                'status' => 'success',
105
                'data' => $playlists];
106
        } else {
107
            $result = [
108
                'status' => 'error',
109
                'data' => 'nodata'];
110
        }
111
        return new JSONResponse($result);
112
    }
113
114
}
115