Skip to content

Commit

Permalink
polish PHP controllers
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <[email protected]>
  • Loading branch information
georgehrke committed Oct 19, 2019
1 parent 352b36b commit 1ec99c0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 232 deletions.
1 change: 0 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
['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'],
Expand Down
14 changes: 7 additions & 7 deletions lib/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Calendar App
*
* @author Georg Ehrke
* @copyright 2016 Georg Ehrke <[email protected]>
* @copyright 2018 Georg Ehrke <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Expand Down Expand Up @@ -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;
}
Expand All @@ -52,7 +52,7 @@ public function __construct($appName, IRequest $request, IManager $contacts) {
*
* @NoAdminRequired
*/
public function searchLocation($location) {
public function searchLocation(string $location):JSONResponse {
$result = $this->contacts->search($location, ['FN', 'ADR']);

$contacts = [];
Expand All @@ -62,7 +62,7 @@ public function searchLocation($location) {
}

$name = $this->getNameFromContact($r);
if (is_string($r['ADR'])) {
if (\is_string($r['ADR'])) {
$r['ADR'] = [$r['ADR']];
}

Expand All @@ -85,7 +85,7 @@ public function searchLocation($location) {
*
* @NoAdminRequired
*/
public function searchAttendee($search) {
public function searchAttendee(string $search):JSONResponse {
$result = $this->contacts->search($search, ['FN', 'EMAIL']);

$contacts = [];
Expand All @@ -95,7 +95,7 @@ public function searchAttendee($search) {
}

$name = $this->getNameFromContact($r);
if (is_string($r['EMAIL'])) {
if (\is_string($r['EMAIL'])) {
$r['EMAIL'] = [$r['EMAIL']];
}

Expand All @@ -115,7 +115,7 @@ public function searchAttendee($search) {
* @param array $r
* @return string
*/
private function getNameFromContact(array $r) {
private function getNameFromContact(array $r):string {
$name = '';
if (isset($r['FN'])) {
$name = $r['FN'];
Expand Down
11 changes: 8 additions & 3 deletions lib/Controller/EmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct($appName, IRequest $request, IUserSession $userSessi
* @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();

Expand Down Expand Up @@ -145,7 +145,7 @@ public function sendEmailPublicLink($recipient, $url, $calendarName) {
* @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;
}
Expand All @@ -160,7 +160,12 @@ private function sendEmail($recipient, $subject, $body, $textBody) {
$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;
}
Expand Down
Loading

0 comments on commit 1ec99c0

Please sign in to comment.