nextcloud-app-radio/lib/Controller/SettingsController.php
2020-11-24 15:16:53 +01:00

154 lines
3.4 KiB
PHP

<?php
/**
* Radio App
*
* @author Jonas Heinrich
* @copyright 2020 Jonas Heinrich <onny@project-insanity.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Radio\Controller;
use Exception;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Util;
class SettingsController extends ApiController {
/** @var IConfig */
private $config;
/** @var string */
private $userId;
/**
* @var IL10N
*/
private $l;
/**
* @param string $appName
* @param IRequest $request
* @param string $userId
* @param IConfig $config
* @param IL10N $l
*/
public function __construct(
$appName, $request, $userId, IConfig $config, IL10N $l
) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userId = $userId;
$this->l = $l;
}
private function getSetting(string $key, string $name, $default): JSONResponse {
try {
$userValue = $this->config->getUserValue(
$this->userId,
$this->appName,
$key,
$default
);
} catch (Exception $e) {
Util::writeLog('radio', $e->getMessage(), Util::ERROR);
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([$name => $userValue], Http::STATUS_OK);
}
private function setSetting($key, $value): JSONResponse {
try {
$this->config->setUserValue(
$this->userId,
$this->appName,
$key,
$value
);
} catch (Exception $e) {
return new JSONResponse(['status' => 'error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse(['status' => 'success'], Http::STATUS_OK);
}
/**
* set menu state
*
* @param string $menuState
* @return JSONResponse
*
* @NoAdminRequired
*/
public function setMenuState($menuState = ""): JSONResponse {
if ($menuState == 'SEARCH') {
return new JSONResponse(['status' => 'success'], Http::STATUS_OK);
};
$legalArguments = ['TOP', 'RECENT', 'NEW', 'FAVORITES', 'CATEGORIES'];
if (!in_array($menuState, $legalArguments)) {
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
}
return $this->setSetting(
'menuState',
$menuState
);
}
/**
* get menu state
*
* @return JSONResponse
*
* @NoAdminRequired
*/
public function getMenuState(): JSONResponse {
return $this->getSetting('menuState', 'menuState', 'TOP');
}
/**
* set player volume
*
* @param string $playerVolume
* @return JSONResponse
*
* @NoAdminRequired
*/
public function setVolumeState($volumeState = "0.5"): JSONResponse {
return $this->setSetting(
'volumeState',
$volumeState
);
}
/**
* get player volume
*
* @return JSONResponse
*
* @NoAdminRequired
*/
public function getVolumeState(): JSONResponse {
return $this->getSetting('volumeState', 'volumeState', 0.5);
}
}