polish PHP controllers

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
Georg Ehrke 2018-11-27 10:48:56 +01:00
parent 352b36b522
commit 1ec99c0d53
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
5 changed files with 37 additions and 232 deletions

View File

@ -38,7 +38,6 @@ return [
['name' => 'contact#searchAttendee', 'url' => '/v1/autocompletion/attendee', 'verb' => 'POST'],
['name' => 'contact#searchLocation', 'url' => '/v1/autocompletion/location', 'verb' => 'POST'],
//Settings
['name' => 'settings#getConfig', 'url' => '/v1/config', 'verb' => 'GET'],
['name' => 'settings#setConfig', 'url' => '/v1/config', 'verb' => 'POST'],
// Tools
['name' => 'email#sendEmailPublicLink', 'url' => '/v1/public/sendmail', 'verb' => 'POST'],

View File

@ -3,7 +3,7 @@
* Calendar App
*
* @author Georg Ehrke
* @copyright 2016 Georg Ehrke <oc.list@georgehrke.com>
* @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@ -40,7 +40,7 @@ class ContactController extends Controller {
* @param IRequest $request an instance of the request
* @param IManager $contacts
*/
public function __construct($appName, IRequest $request, IManager $contacts) {
public function __construct(string $appName, IRequest $request, IManager $contacts) {
parent::__construct($appName, $request);
$this->contacts = $contacts;
}
@ -52,7 +52,7 @@ class ContactController extends Controller {
*
* @NoAdminRequired
*/
public function searchLocation($location) {
public function searchLocation(string $location):JSONResponse {
$result = $this->contacts->search($location, ['FN', 'ADR']);
$contacts = [];
@ -62,7 +62,7 @@ class ContactController extends Controller {
}
$name = $this->getNameFromContact($r);
if (is_string($r['ADR'])) {
if (\is_string($r['ADR'])) {
$r['ADR'] = [$r['ADR']];
}
@ -85,7 +85,7 @@ class ContactController extends Controller {
*
* @NoAdminRequired
*/
public function searchAttendee($search) {
public function searchAttendee(string $search):JSONResponse {
$result = $this->contacts->search($search, ['FN', 'EMAIL']);
$contacts = [];
@ -95,7 +95,7 @@ class ContactController extends Controller {
}
$name = $this->getNameFromContact($r);
if (is_string($r['EMAIL'])) {
if (\is_string($r['EMAIL'])) {
$r['EMAIL'] = [$r['EMAIL']];
}
@ -115,7 +115,7 @@ class ContactController extends Controller {
* @param array $r
* @return string
*/
private function getNameFromContact(array $r) {
private function getNameFromContact(array $r):string {
$name = '';
if (isset($r['FN'])) {
$name = $r['FN'];

View File

@ -84,7 +84,7 @@ class EmailController extends Controller {
* @return JSONResponse
* @NoAdminRequired
*/
public function sendEmailPublicLink($recipient, $url, $calendarName) {
public function sendEmailPublicLink(string $recipient, string $url, string $calendarName):JSONResponse {
$user = $this->userSession->getUser();
$displayName = $user->getDisplayName();
@ -145,7 +145,7 @@ class EmailController extends Controller {
* @param string $textBody
* @return int
*/
private function sendEmail($recipient, $subject, $body, $textBody) {
private function sendEmail(string $recipient, string $subject, string $body, string $textBody):int {
if (!$this->mailer->validateMailAddress($recipient)) {
return Http::STATUS_BAD_REQUEST;
}
@ -160,7 +160,12 @@ class EmailController extends Controller {
$message->setTo([$recipient => $this->l10n->t('Recipient')]);
$message->setPlainBody($textBody);
$message->setHtmlBody($body);
$this->mailer->send($message);
try {
$this->mailer->send($message);
} catch(\Exception $ex) {
return Http::STATUS_INTERNAL_SERVER_ERROR;
}
return Http::STATUS_OK;
}

View File

@ -35,11 +35,6 @@ class SettingsController extends Controller {
*/
private $config;
/**
* @var IUserSession
*/
private $userSession;
/**
* @var string
*/
@ -55,32 +50,10 @@ class SettingsController extends Controller {
IConfig $config) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userSession = $userSession;
$this->userId = $userSession->getUser()->getUID();
}
/**
* get a configuration item
*
* @NoAdminRequired
*
* @param string $key
* @return JSONResponse
*/
public function getConfig($key) {
switch ($key) {
case 'view':
return $this->getView();
case 'skipPopover':
return $this->getSkipPopover();
case 'showWeekNr':
return $this->getShowWeekNr();
case 'firstRun':
return $this->getFirstRun();
case 'timezone':
return $this->getTimezone();
default:
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
$user = $userSession->getUser();
if ($user) {
$this->userId = $user->getUID();
}
}
@ -89,11 +62,11 @@ class SettingsController extends Controller {
*
* @NoAdminRequired
*
* @param string $key
* @param mixed $value
* @param string $key The config key to set
* @param mixed $value The value to set for given config key
* @return JSONResponse
*/
public function setConfig($key, $value) {
public function setConfig(string $key, string $value):JSONResponse {
switch ($key) {
case 'view':
return $this->setView($value);
@ -116,11 +89,11 @@ class SettingsController extends Controller {
/**
* set a new view
*
* @param string $view
* @param string $view Selected view by user
* @return JSONResponse
*/
private function setView($view) {
if (!$this->isViewAllowed($view)) {
private function setView(string $view):JSONResponse {
if (!\in_array($view, ['agendaDay', 'agendaWeek', 'month'])) {
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
}
@ -138,53 +111,14 @@ class SettingsController extends Controller {
return new JSONResponse();
}
/**
* get a config value
*
* @return JSONResponse
*/
private function getView() {
try {
$view = $this->config->getUserValue(
$this->userId,
$this->appName,
'currentView',
'month'
);
} catch(\Exception $e) {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([
'value' => $view,
]);
}
/**
* check if view is allowed
*
* @param $view
* @return bool
*/
private function isViewAllowed($view) {
$allowedViews = [
'agendaDay',
'agendaWeek',
'month',
];
return in_array($view, $allowedViews);
}
/**
* set if popover shall be skipped
*
* @param $value
* @param $value User-selected option whether or not to show simple event editor
* @return JSONResponse
*/
private function setSkipPopover($value) {
if (!$this->isSkipPopoverValueAllowed($value)) {
private function setSkipPopover(string $value):JSONResponse {
if (!\in_array($value, ['yes', 'no'])) {
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
}
@ -202,51 +136,14 @@ class SettingsController extends Controller {
return new JSONResponse();
}
/**
* get if popover shall be skipped
*
* @return JSONResponse
*/
private function getSkipPopover() {
try {
$view = $this->config->getUserValue(
$this->userId,
$this->appName,
'skipPopover',
'no'
);
} catch(\Exception $e) {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([
'value' => $view,
]);
}
/**
* check if value for skipPopover is allowed
*
* @param $value
* @return bool
*/
private function isSkipPopoverValueAllowed($value) {
$allowedValues = [
'yes',
'no'
];
return in_array($value, $allowedValues);
}
/**
* set config value for showing week numbers
*
* @param $value
* @param $value User-selected option whether or not to show weekends
* @return JSONResponse
*/
private function showWeekends($value) {
if (!$this->isShowWeekendsValueAllowed($value)) {
private function showWeekends(string $value):JSONResponse {
if (!\in_array($value, ['yes', 'no'])) {
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
}
@ -264,29 +161,14 @@ class SettingsController extends Controller {
return new JSONResponse();
}
/**
* check if value for showWeekNr is allowed
*
* @param $value
* @return bool
*/
private function isShowWeekendsValueAllowed($value) {
$allowedValues = [
'yes',
'no'
];
return in_array($value, $allowedValues);
}
/**
* set config value for showing week numbers
*
* @param $value
* @param $value User-selected option whether or not to show week numbers
* @return JSONResponse
*/
private function setShowWeekNr($value) {
if (!$this->isShowWeekNrValueAllowed($value)) {
private function setShowWeekNr($value):JSONResponse {
if (!\in_array($value, ['yes', 'no'])) {
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
}
@ -304,49 +186,12 @@ class SettingsController extends Controller {
return new JSONResponse();
}
/**
* get config value for showing week numbers
*
* @return JSONResponse
*/
private function getShowWeekNr() {
try {
$value = $this->config->getUserValue(
$this->userId,
$this->appName,
'showWeekNr',
'no'
);
} catch(\Exception $e) {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([
'value' => $value,
]);
}
/**
* check if value for showWeekNr is allowed
*
* @param $value
* @return bool
*/
private function isShowWeekNrValueAllowed($value) {
$allowedValues = [
'yes',
'no'
];
return in_array($value, $allowedValues);
}
/**
* remember that first run routines executed
*
* @return JSONResponse
*/
private function setFirstRun() {
private function setFirstRun():JSONResponse {
try {
$this->config->setUserValue(
$this->userId,
@ -361,35 +206,13 @@ class SettingsController extends Controller {
return new JSONResponse();
}
/**
* get stored value for first run
*
* @return JSONResponse
*/
private function getFirstRun() {
try {
$value = $this->config->getUserValue(
$this->userId,
$this->appName,
'firstRun',
'yes'
);
} catch(\Exception $e) {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([
'value' => $value,
]);
}
/**
* sets display timezone for user
*
* @param string $value
* @param string $value User-selected option for timezone to display events in
* @return JSONResponse
*/
private function setTimezone($value) {
private function setTimezone($value):JSONResponse {
try {
$this->config->setUserValue(
$this->userId,
@ -403,26 +226,4 @@ class SettingsController extends Controller {
return new JSONResponse();
}
/**
* gets display timezone for user
*
* @return JSONResponse
*/
private function getTimezone() {
try {
$value = $this->config->getUserValue(
$this->userId,
$this->appName,
'timezone',
'automatic'
);
} catch(\Exception $e) {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([
'value' => $value,
]);
}
}

View File

@ -29,7 +29,7 @@ const state = {
initialView: oca_calendar.initialView || 'month',
showPopover: oca_calendar.skipPopover === 'no',
showWeekends: oca_calendar.showWeekends === 'yes',
showWeekNumbers: oca_calendar.weekNumbers === 'yes',
showWeekNumbers: oca_calendar.showWeekNumbers === 'yes',
timezone: oca_calendar.timezone || 'automatic'
}