Tag::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 10
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\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
        $aPlaylists = array();
46
47
        foreach ($allTags as $tag) {
48
            $tagName = $tag->getName();
49
            $tagId = $tag->getId();
50
            $trackCount = $this->getTrackCount($tagId);
51
            if ($trackCount && $trackCount !== 0) {
52
                $aPlaylists[] = array('id' => $tagId, 'name' => $tagName, 'cnt' => $trackCount);
53
            }
54
        }
55
        return $aPlaylists;
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
        $result = 0;
70
        $fileChunks = array_chunk($allFiles, 999);
71
        foreach ($fileChunks as $fileChunk) {
72
            $sql = $this->db->getQueryBuilder();
73
            $sql->selectAlias($sql->func()->count('id'), 'count')
74
                ->from('audioplayer_tracks')
75
                ->where($sql->expr()->in('file_id', $sql->createNamedParameter($fileChunk, IQueryBuilder::PARAM_INT_ARRAY)))
76
                ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)));
77
            $statement = $sql->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

77
            $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
78
            $statementResult = $statement->fetch();
79
            $result = $result + $statementResult['count'];
80
            $statement->closeCursor();
81
        }
82
        return $result;
83
    }
84
85
    /**
86
     * Get the tracks for a selected category or album
87
     *
88
     * @param string $category
89
     * @param string $categoryId
90
     * @return array
91
     */
92
    public function getTracksDetails($tagId)
93
    {
94
        $allFiles = $this->tagManager->getObjectIdsForTags($tagId);
95
96
        $sql = $this->db->getQueryBuilder();
97
        $function = $sql->createFunction('
98
			CASE 
99
				WHEN ' . $sql->getColumnName('AB.cover') . ' IS NOT NULL THEN '. $sql->getColumnName('AB.id') . '
100
				ELSE NULL 
101
			END'
102
        );
103
104
        $sql->select('AT.id')
105
            ->selectAlias('AT.title', 'cl1')
106
            ->selectAlias('AA.name', 'cl2')
107
            ->selectAlias('AB.name', 'cl3')
108
            ->selectAlias('AT.length', 'len')
109
            ->selectAlias('AT.file_id', 'fid')
110
            ->selectAlias('AT.mimetype', 'mim')
111
            ->selectAlias($function, 'cid')
112
            ->selectAlias($sql->createNamedParameter(mb_strtolower('AB.name')), 'lower')
113
            ->from('audioplayer_tracks', 'AT')
114
            ->leftJoin('AT', 'audioplayer_artists', 'AA', $sql->expr()->eq('AT.artist_id', 'AA.id'))
115
            ->leftJoin('AT', 'audioplayer_albums', 'AB', $sql->expr()->eq('AT.album_id', 'AB.id'))
116
            ->orderBy($sql->createNamedParameter(mb_strtolower('AB.name')), 'ASC')
117
            ->addorderBy('AT.disc', 'ASC')
118
            ->addorderBy('AT.number', 'ASC')
119
            ->where($sql->expr()->in('AT.file_id', $sql->createNamedParameter($allFiles, IQueryBuilder::PARAM_INT_ARRAY)))
120
            ->andWhere($sql->expr()->eq('AT.user_id', $sql->createNamedParameter($this->userId)));
121
122
        $statement = $sql->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

122
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
123
        $result = $statement->fetchAll();
124
        $statement->closeCursor();
125
126
        return $result;
127
    }
128
129
    /**
130
     * Get the covers for the "Album Covers" view
131
     *
132
     * @NoAdminRequired
133
     * @param $category
134
     * @param $categoryId
135
     * @return array
136
     */
137
    public function getCategoryItemCovers($tagId)
138
    {
139
        $allFiles = $this->tagManager->getObjectIdsForTags($tagId);
140
141
        $sql = $this->db->getQueryBuilder();
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();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

164
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
165
        $result = $statement->fetchAll();
166
        $statement->closeCursor();
167
168
        return $result;
169
    }
170
}
171