Passed
Push — master ( 49e88e...27428c )
by Pauli
02:49 queued 10s
created

RadioApiController::exportAllToFile()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 7
nop 3
dl 0
loc 11
rs 9.9332
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020, 2021
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use \OCP\AppFramework\Controller;
16
use \OCP\AppFramework\Http;
17
use \OCP\AppFramework\Http\JSONResponse;
18
19
use \OCP\Files\Folder;
20
use \OCP\IRequest;
21
22
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
23
use \OCA\Music\AppFramework\Core\Logger;
24
use \OCA\Music\BusinessLayer\RadioStationBusinessLayer;
25
use \OCA\Music\Http\ErrorResponse;
26
use \OCA\Music\Utility\PlaylistFileService;
27
use \OCA\Music\Utility\Util;
28
29
class RadioApiController extends Controller {
30
	private $businessLayer;
31
	private $playlistFileService;
32
	private $userId;
33
	private $userFolder;
34
	private $logger;
35
36
	public function __construct(string $appname,
37
								IRequest $request,
38
								RadioStationBusinessLayer $businessLayer,
39
								PlaylistFileService $playlistFileService,
40
								?string $userId,
41
								?Folder $userFolder,
42
								Logger $logger) {
43
		parent::__construct($appname, $request);
44
		$this->businessLayer = $businessLayer;
45
		$this->playlistFileService = $playlistFileService;
46
		$this->userId = $userId;
47
		$this->userFolder = $userFolder;
48
		$this->logger = $logger;
49
	}
50
51
	/**
52
	 * lists all radio stations
53
	 *
54
	 * @NoAdminRequired
55
	 * @NoCSRFRequired
56
	 */
57
	public function getAll() {
58
		$stations = $this->businessLayer->findAll($this->userId);
59
		return Util::arrayMapMethod($stations, 'toApi');
60
	}
61
62
	/**
63
	 * creates a station
64
	 *
65
	 * @NoAdminRequired
66
	 * @NoCSRFRequired
67
	 */
68
	public function create($name, $streamUrl, $homeUrl) {
69
		if ($streamUrl === null) {
70
			return new ErrorResponse(Http::STATUS_BAD_REQUEST, "Mandatory argument 'streamUrl' not given");
71
		} else {
72
			$station = $this->businessLayer->create($this->userId, $name, $streamUrl, $homeUrl);
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\BusinessLayer\...BusinessLayer::create() 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

72
			$station = $this->businessLayer->create(/** @scrutinizer ignore-type */ $this->userId, $name, $streamUrl, $homeUrl);
Loading history...
73
			return $station->toApi();
74
		}
75
	}
76
77
	/**
78
	 * deletes a station
79
	 *
80
	 * @NoAdminRequired
81
	 * @NoCSRFRequired
82
	 */
83
	public function delete(int $id) {
84
		try {
85
			$this->businessLayer->delete($id, $this->userId);
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\AppFramework\B...BusinessLayer::delete() 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

85
			$this->businessLayer->delete($id, /** @scrutinizer ignore-type */ $this->userId);
Loading history...
86
			return [];
87
		} catch (BusinessLayerException $ex) {
88
			return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage());
89
		}
90
	}
91
92
	/**
93
	 * get a single radio station
94
	 *
95
	 * @NoAdminRequired
96
	 * @NoCSRFRequired
97
	 */
98
	public function get(int $id) {
99
		try {
100
			$station = $this->businessLayer->find($id, $this->userId);
101
			return $station->toAPI();
102
		} catch (BusinessLayerException $ex) {
103
			return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage());
104
		}
105
	}
106
107
	/**
108
	 * update a station
109
	 *
110
	 * @NoAdminRequired
111
	 * @NoCSRFRequired
112
	 */
113
	public function update(int $id, string $name = null, string $streamUrl = null, string $homeUrl = null) {
114
		if ($name === null && $streamUrl === null && $homeUrl === null) {
115
			return new ErrorResponse(Http::STATUS_BAD_REQUEST, "at least one of the args ['name', 'streamUrl', 'homrUrl'] must be given");
116
		}
117
118
		try {
119
			$station = $this->businessLayer->find($id, $this->userId);
120
			if ($name !== null) {
121
				$station->setName($name);
122
			}
123
			if ($streamUrl !== null) {
124
				$station->setStreamUrl($streamUrl);
125
			}
126
			if ($homeUrl !== null) {
127
				$station->setHomeUrl($homeUrl);
128
			}
129
			$this->businessLayer->update($station);
130
131
			return new JSONResponse($station->toApi());
132
		} catch (BusinessLayerException $ex) {
133
			return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage());
134
		}
135
	}
136
137
	/**
138
	 * export all radio stations to a file
139
	 *
140
	 * @param string $name target file name without the file extension
141
	 * @param string $path parent folder path
142
	 * @param string $oncollision action to take on file name collision,
143
	 *								supported values:
144
	 *								- 'overwrite' The existing file will be overwritten
145
	 *								- 'keepboth' The new file is named with a suffix to make it unique
146
	 *								- 'abort' (default) The operation will fail
147
	 *
148
	 * @NoAdminRequired
149
	 * @NoCSRFRequired
150
	 */
151
	public function exportAllToFile(string $name, string $path, string $oncollision) {
152
		try {
153
			$exportedFilePath = $this->playlistFileService->exportRadioStationsToFile(
154
					$this->userId, $this->userFolder, $path, $name . '.m3u8', $oncollision);
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\Utility\Playli...rtRadioStationsToFile() 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

154
					/** @scrutinizer ignore-type */ $this->userId, $this->userFolder, $path, $name . '.m3u8', $oncollision);
Loading history...
Bug introduced by
It seems like $this->userFolder can also be of type null; however, parameter $userFolder of OCA\Music\Utility\Playli...rtRadioStationsToFile() does only seem to accept OCP\Files\Folder, 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

154
					$this->userId, /** @scrutinizer ignore-type */ $this->userFolder, $path, $name . '.m3u8', $oncollision);
Loading history...
155
			return new JSONResponse(['wrote_to_file' => $exportedFilePath]);
156
		} catch (\OCP\Files\NotFoundException $ex) {
157
			return new ErrorResponse(Http::STATUS_NOT_FOUND, 'folder not found');
158
		} catch (\RuntimeException $ex) {
159
			return new ErrorResponse(Http::STATUS_CONFLICT, $ex->getMessage());
160
		} catch (\OCP\Files\NotPermittedException $ex) {
161
			return new ErrorResponse(Http::STATUS_FORBIDDEN, 'user is not allowed to write to the target file');
162
		}
163
	}
164
165
	/**
166
	 * import radio stations from a file
167
	 * @param string $filePath path of the file to import
168
	 *
169
	 * @NoAdminRequired
170
	 * @NoCSRFRequired
171
	 */
172
	public function importFromFile(string $filePath) {
173
		try {
174
			$result = $this->playlistFileService->importRadioStationsFromFile($this->userId, $this->userFolder, $filePath);
0 ignored issues
show
Bug introduced by
It seems like $this->userFolder can also be of type null; however, parameter $userFolder of OCA\Music\Utility\Playli...RadioStationsFromFile() does only seem to accept OCP\Files\Folder, 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

174
			$result = $this->playlistFileService->importRadioStationsFromFile($this->userId, /** @scrutinizer ignore-type */ $this->userFolder, $filePath);
Loading history...
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\Utility\Playli...RadioStationsFromFile() 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

174
			$result = $this->playlistFileService->importRadioStationsFromFile(/** @scrutinizer ignore-type */ $this->userId, $this->userFolder, $filePath);
Loading history...
175
			$result['stations'] = Util::arrayMapMethod($result['stations'], 'toApi');
176
			return $result;
177
		} catch (\OCP\Files\NotFoundException $ex) {
178
			return new ErrorResponse(Http::STATUS_NOT_FOUND, 'playlist file not found');
179
		} catch (\UnexpectedValueException $ex) {
180
			return new ErrorResponse(Http::STATUS_UNSUPPORTED_MEDIA_TYPE, $ex->getMessage());
181
		}
182
	}
183
184
	/**
185
	 * reset all the radio stations of the user
186
	 *
187
	 * @NoAdminRequired
188
	 * @NoCSRFRequired
189
	 */
190
	public function resetAll() {
191
		$this->businessLayer->deleteAll($this->userId);
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\AppFramework\B...inessLayer::deleteAll() 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

191
		$this->businessLayer->deleteAll(/** @scrutinizer ignore-type */ $this->userId);
Loading history...
192
		return new JSONResponse(['success' => true]);
193
	}
194
}
195