EditorController::saveMetaData()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 2
b 0
f 0
nc 8
nop 3
dl 0
loc 22
rs 8.8333
1
<?php
2
/**
3
 * Audio Player Tag 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
use OCP\AppFramework\Controller;
14
use OCP\IRequest;
15
use OCP\IL10N;
16
use OCP\L10N\IFactory;
17
use OCP\IDbConnection;
18
use OCP\Files\IRootFolder;
19
use \OC\Files\View; //remove when editAudioFiles is updated and toTmpFile alternative
0 ignored issues
show
Bug introduced by
The type OC\Files\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\ILogger;
21
22
/**
23
 * Controller class for main page.
24
 */
25
class EditorController extends Controller {
26
	
27
	private $userId;
28
	private $l10n;
29
	private $db;
30
	private $languageFactory;
31
	private $rootFolder;
32
    private $logger;
33
    private $DBController;
34
		public function __construct(
35
			$appName, 
36
			IRequest $request, 
37
			$userId, 
38
			IL10N $l10n, 
39
			IDbConnection $db, 
40
			IFactory $languageFactory,
41
			IRootFolder $rootFolder,
42
            DbController $DBController,
43
            ILogger $logger
44
        ) {
45
		parent::__construct($appName, $request);
46
		$this->appName = $appName;
47
		$this->userId = $userId;
48
		$this->l10n = $l10n;
49
		$this->db = $db;
50
		$this->languageFactory = $languageFactory;
51
		$this->rootFolder = $rootFolder;
52
		$this->logger = $logger;
53
		$this->DBController = $DBController;
54
	}
55
56
    /**
57
     * Save meta data in database
58
     * @param int $trackid
59
     * @param string $editKey
60
     * @param string $editValue
61
     * @return bool
62
     */
63
    public function saveMetaData($trackid, $editKey, $editValue)
64
    {
65
66
        $directEditArray = ['Title' => 'title', 'Genre' => '', 'Subtitle' => 'subtitle', 'Year' => 'year', 'Album' => '', 'Artist' => '', 'Disc' => 'disc', 'Track' => 'number'];
67
        $updateKey = $directEditArray[$editKey];
68
69
        if ($editKey === 'Genre' AND $editValue !== '') {
70
            $editValue = $this->DBController->writeGenreToDB($this->userId, $editValue);
71
            $updateKey = 'genre_id';
72
        }
73
        if ($editKey === 'Artist' AND $editValue !== '') {
74
            $editValue = $this->DBController->writeArtistToDB($this->userId, $editValue);
75
            $updateKey = 'artist_id';
76
        }
77
78
        if ($updateKey !== '' AND $editValue !== ''){
79
            $this->DBController->updateTrack($this->userId, $trackid, $updateKey, $editValue);
80
            //$this->DBController->cleanupDb($this->userId);
81
            //return $this->updateFileMetaData($trackid, $updateKey, $editValue);
82
            return true;
83
        }
84
        return false;
85
    }
86
87
    /**
88
     * Save meta data to file. not working yet
89
     * @param int $trackid
90
     * @param string $editKey
91
     * @param string $editValue
92
     * @return array
93
     */
94
    private function updateFileMetaData($trackid, $editKey, $editValue)
0 ignored issues
show
Unused Code introduced by
The method updateFileMetaData() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
95
    {
96
        $fileId = $this->DBController->getFileId($trackid);
97
        if (!class_exists('getid3_exception')) {
98
            require_once __DIR__ . '/../../3rdparty/getid3/getid3.php';
99
        }
100
        require_once __DIR__ . '/../../3rdparty/getid3/write.php';
101
102
        $userView = new View('/' . $this->userId . '/files');
103
        $path = $userView->getPath($fileId);
104
        $this->logger->debug('Section 2: ' . $fileId . ' Path : ' . $path, array('app' => 'audioplayer'));
105
106
        if (\OC\Files\Filesystem::isUpdatable($path)) {
0 ignored issues
show
Bug introduced by
The type OC\Files\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
107
108
            $tagWriter = new \getid3_writetags;
0 ignored issues
show
Bug introduced by
The type getid3_writetags was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
109
            $localFile = $userView->getLocalFile($path);
110
            $this->logger->debug('Updating following file: ' . $localFile, array('app' => 'audioplayer'));
111
            $tagWriter->filename = $localFile;
112
            $tagWriter->tagformats = array('id3v2.3');
113
            $tagWriter->overwrite_tags = false;
114
            $tagWriter->tag_encoding = 'UTF-8';
115
116
            $resultData = [
117
                $editKey => [$editValue]
118
            ];
119
            $resultData = $tagWriter->tag_data = $resultData;
120
            $tagWriter->WriteTags();
121
122
            if (count($tagWriter->errors)) {
123
                $this->logger->debug('errors: ' . print_r($tagWriter->errors), array('app' => 'audioplayer'));
0 ignored issues
show
Bug introduced by
Are you sure print_r($tagWriter->errors) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

123
                $this->logger->debug('errors: ' . /** @scrutinizer ignore-type */ print_r($tagWriter->errors), array('app' => 'audioplayer'));
Loading history...
124
                $result = [
125
                    'status' => 'errors',
126
                    'msg' => print_r($tagWriter->errors),
127
                ];
128
            } elseif (count($tagWriter->warnings)) {
129
                $this->logger->debug('errors: ' . print_r($tagWriter->warnings), array('app' => 'audioplayer'));
0 ignored issues
show
Bug introduced by
Are you sure print_r($tagWriter->warnings) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

129
                $this->logger->debug('errors: ' . /** @scrutinizer ignore-type */ print_r($tagWriter->warnings), array('app' => 'audioplayer'));
Loading history...
130
                $result = [
131
                    'status' => 'warnings',
132
                    'msg' => (string)$tagWriter->warnings,
133
                ];
134
            } else {
135
                $result = [
136
                    'status' => 'success',
137
                    'data' => $resultData,
138
                ];
139
            }
140
        } else {
141
            $result = [
142
                'status' => 'error_write',
143
                'msg' => 'not writeable',
144
            ];
145
        }
146
147
        //$response = new JSONResponse();
148
        //$response -> setData($result);
149
        return $result;
150
151
    }
152
}
153