Passed
Push — master ( b5cc7d...f2f8fd )
by Marcel
02:32
created

Tag::getCategoryItems()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 14
rs 9.9666
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\Categories;
13
14
use OCA\audioplayer\Service\TagManager;
15
use OCP\DB\QueryBuilder\IQueryBuilder;
16
use OCP\IDBConnection;
17
use Psr\Log\LoggerInterface;
18
19
class Tag
20
{
21
    private $tagManager;
22
    private $userId;
23
    private $logger;
24
    private $db;
25
26
    public function __construct(
27
        $userId,
28
        IDBConnection $db,
29
        LoggerInterface $logger,
30
        TagManager $tagManager
31
    )
32
    {
33
        $this->db = $db;
34
        $this->logger = $logger;
35
        $this->tagManager = $tagManager;
36
        $this->userId = $userId;
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function getCategoryItems()
43
    {
44
        $allTags = $this->tagManager->getAllTags();
45
46
        foreach ($allTags as $tag) {
47
            $tagName = $tag->getName();
48
            $tagId = $tag->getId();
49
            $trackCount = $this->getTrackCount($tagId);
50
            if ($trackCount && $trackCount !== 0) {
51
                $row = array('id' => $tagId, 'name' => $tagName, 'cnt' => $trackCount);
52
            }
53
            $aPlaylists[] = $row;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row does not seem to be defined for all execution paths leading up to this point.
Loading history...
54
        }
55
        return $aPlaylists;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $aPlaylists seems to be defined by a foreach iteration on line 46. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
56
    }
57
58
    /**
59
     * Get the number of tracks for a Tag
60
     *
61
     * @param string $category
62
     * @param integer $categoryId
63
     * @return integer
64
     */
65
    private function getTrackCount($tagId)
66
    {
67
        $allFiles = $this->tagManager->getObjectIdsForTags($tagId);
68
69
        $sql = $this->db->getQueryBuilder();
70
        $sql->select($sql->func()->count('id'))
71
            ->from('audioplayer_tracks')
72
            ->where($sql->expr()->in('file_id', $sql->createNamedParameter($allFiles, IQueryBuilder::PARAM_INT_ARRAY)))
73
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)));
74
75
        $statement = $sql->execute();
76
        $result = $statement->fetch();
77
        $statement->closeCursor();
78
79
        $this->logger->error(json_encode($result['count']));
80
        return $result['count'];
81
    }
82
83
    /**
84
     * Get the tracks for a selected category or album
85
     *
86
     * @param string $category
87
     * @param string $categoryId
88
     * @return array
89
     */
90
    public function getTracksDetails($tagId)
91
    {
92
        $allFiles = $this->tagManager->getObjectIdsForTags($tagId);
93
94
        $sql = $this->db->getQueryBuilder();
95
96
        $function = $sql->createFunction('
97
			CASE 
98
				WHEN ' . $sql->getColumnName('AB.cover') . ' IS NOT NULL THEN '. $sql->getColumnName('AB.id') . '
99
				ELSE NULL 
100
			END'
101
        );
102
103
        $sql->select('AT.id')
104
            ->selectAlias('AT.title', 'cl1')
105
            ->selectAlias('AA.name', 'cl2')
106
            ->selectAlias('AB.name', 'cl3')
107
            ->selectAlias('AT.length', 'len')
108
            ->selectAlias('AT.file_id', 'fid')
109
            ->selectAlias('AT.mimetype', 'mim')
110
            ->selectAlias($function, 'cid')
111
            ->selectAlias($sql->createNamedParameter(mb_strtolower('AB.name')), 'lower')
112
            ->from('audioplayer_tracks', 'AT')
113
            ->leftJoin('AT', 'audioplayer_artists', 'AA', $sql->expr()->eq('AT.artist_id', 'AA.id'))
114
            ->leftJoin('AT', 'audioplayer_albums', 'AB', $sql->expr()->eq('AT.album_id', 'AB.id'))
115
            ->orderBy($sql->createNamedParameter(mb_strtolower('AB.name')), 'ASC')
116
            ->addorderBy('AT.disc', 'ASC')
117
            ->addorderBy('AT.number', 'ASC')
118
            ->where($sql->expr()->in('AT.file_id', $sql->createNamedParameter($allFiles, IQueryBuilder::PARAM_INT_ARRAY)))
119
            ->andWhere($sql->expr()->eq('AT.user_id', $sql->createNamedParameter($this->userId)));
120
121
        $statement = $sql->execute();
122
        $result = $statement->fetchAll();
123
        $statement->closeCursor();
124
125
        return $result;
126
    }
127
128
    /**
129
     * Get the covers for the "Album Covers" view
130
     *
131
     * @NoAdminRequired
132
     * @param $category
133
     * @param $categoryId
134
     * @return array
135
     */
136
    public function getCategoryItemCovers($tagId)
137
    {
138
        $allFiles = $this->tagManager->getObjectIdsForTags($tagId);
139
140
        $sql = $this->db->getQueryBuilder();
141
142
        $function = $sql->createFunction('
143
			CASE 
144
				WHEN ' . $sql->getColumnName('AB.cover') . ' IS NOT NULL THEN '. $sql->getColumnName('AB.id') . '
145
				ELSE NULL 
146
			END'
147
        );
148
149
        $sql->select('AB.id')
150
            ->addSelect('AB.name')
151
            ->selectAlias($sql->func()->lower(('AB.name')), 'lower')
152
            ->selectAlias('AA.id', 'art')
153
            ->selectAlias($function, 'cid')
154
            ->from('audioplayer_tracks', 'AT')
155
            ->leftJoin('AT', 'audioplayer_albums', 'AB', $sql->expr()->eq('AT.album_id', 'AB.id'))
156
            ->leftJoin('AB', 'audioplayer_artists', 'AA', $sql->expr()->eq('AB.artist_id', 'AA.id'))
157
            ->where($sql->expr()->in('AT.file_id', $sql->createNamedParameter($allFiles, IQueryBuilder::PARAM_INT_ARRAY)))
158
            ->andWhere($sql->expr()->eq('AT.user_id', $sql->createNamedParameter($this->userId)))
159
            ->orderBy($sql->createNamedParameter(mb_strtolower('AB.name')), 'ASC')
160
            ->groupBy('AB.id')
161
            ->addGroupBy('AA.id')
162
            ->addGroupBy('AB.name');
163
164
        $statement = $sql->execute();
165
        $result = $statement->fetchAll();
166
        $statement->closeCursor();
167
168
        return $result;
169
    }
170
}