SidebarController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 48
c 6
b 2
f 0
dl 0
loc 87
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A getPlaylists() 0 13 2
A getAudioInfo() 0 29 4
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-2020 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)));
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