Clean-up and simplify PHP controllers

This commit is contained in:
Raimund Schlüßler 2019-01-26 21:47:53 +01:00
parent 862caefbd3
commit e23c805627
No known key found for this signature in database
GPG Key ID: 036FA7EB1A599178
8 changed files with 145 additions and 162 deletions

View File

@ -20,19 +20,5 @@
*
*/
namespace OCA\Tasks\AppInfo;
\OC::$server->getNavigationManager()->add(function () {
$urlGenerator = \OC::$server->getURLGenerator();
return [
'id' => 'tasks',
'order' => 100,
'href' => $urlGenerator->linkToRoute('tasks.page.index'),
'icon' => $urlGenerator->imagePath('tasks', 'tasks.svg'),
'name' => \OC::$server->getL10N('tasks')->t('Tasks'),
];
});
$app = new \OCA\Tasks\AppInfo\Application();
$app->registerNavigation();

View File

@ -1,108 +0,0 @@
<?php
/**
* Nextcloud - Tasks
*
* @author Raimund Schlüßler
* @copyright 2018 Raimund Schlüßler <raimund.schluessler@mailbox.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\Tasks\AppInfo;
use \OCP\AppFramework\App;
use \OCP\AppFramework\IAppContainer;
use \OCA\Tasks\Controller\PageController;
use \OCA\Tasks\Controller\CollectionsController;
use \OCA\Tasks\Controller\SettingsController;
use \OCA\Tasks\Service\CollectionsService;
use \OCA\Tasks\Service\SettingsService;
class Application extends App {
public function __construct (array $urlParams=array()) {
parent::__construct('tasks', $urlParams);
$container = $this->getContainer();
/**
* Controllers
*/
$container->registerService('PageController', function(IAppContainer $c) {
return new PageController(
$c->query('AppName'),
$c->query('Request'),
$c->query('UserSession'),
$c->query('UserId'),
$c->query('ServerContainer')->getConfig()
);
});
$container->registerService('CollectionsController', function(IAppContainer $c) {
return new CollectionsController(
$c->query('AppName'),
$c->query('Request'),
$c->query('CollectionsService')
);
});
$container->registerService('SettingsController', function(IAppContainer $c) {
return new SettingsController(
$c->query('AppName'),
$c->query('Request'),
$c->query('SettingsService')
);
});
/**
* Services
*/
$container->registerService('CollectionsService', function(IAppContainer $c) {
return new CollectionsService(
$c->query('UserId'),
$c->query('L10N'),
$c->query('Settings'),
$c->query('AppName')
);
});
$container->registerService('SettingsService', function(IAppContainer $c) {
return new SettingsService(
$c->query('UserId'),
$c->query('Settings'),
$c->query('AppName')
);
});
/**
* Core
*/
$container->registerService('UserId', function(IAppContainer $c) {
$user = $c->query('ServerContainer')->getUserSession()->getUser();
return ($user) ? $user->getUID() : '';
});
$container->registerService('L10N', function(IAppContainer $c) {
return $c->query('ServerContainer')->getL10N($c->query('AppName'));
});
$container->registerService('Settings', function(IAppContainer $c) {
return $c->query('ServerContainer')->getConfig();
});
}
}

View File

@ -0,0 +1,58 @@
<?php
/**
* Nextcloud - Tasks
*
* @author Raimund Schlüßler
* @copyright 2019 Raimund Schlüßler <raimund.schluessler@mailbox.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\Tasks\AppInfo;
use OCP\AppFramework\App;
class Application extends App {
/**
* @param array $params
*/
public function __construct(array $params=[]) {
parent::__construct('tasks', $params);
}
/**
* Register navigation
*/
public function registerNavigation() {
$appName = $this->getContainer()->getAppName();
$server = $this->getContainer()->getServer();
$urlGenerator = $server->getURLGenerator();
$server->getNavigationManager()->add(function() use ($appName, $server, $urlGenerator) {
return [
'id' => $appName,
'order' => 100,
'href' => $urlGenerator->linkToRoute('tasks.page.index'),
'icon' => $urlGenerator->imagePath($appName, 'tasks.svg'),
'name' => $server->getL10N($appName)->t('Tasks'),
];
});
}
}

View File

@ -28,11 +28,19 @@ use \OCP\AppFramework\Controller;
class CollectionsController extends Controller {
/**
* @var CollectionsService
*/
private $collectionsService;
use Response;
public function __construct($appName, IRequest $request, CollectionsService $collectionsService){
/**
* @param string $appName
* @param IRequest $request an instance of the request
* @param CollectionsService $collectionsService
*/
public function __construct(string $appName, IRequest $request, CollectionsService $collectionsService){
parent::__construct($appName, $request);
$this->collectionsService = $collectionsService;
}

View File

@ -3,7 +3,7 @@
* Nextcloud - Tasks
*
* @author Raimund Schlüßler
* @copyright 2018 Raimund Schlüßler <raimund.schluessler@mailbox.org>
* @copyright 2019 Raimund Schlüßler <raimund.schluessler@mailbox.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@ -24,7 +24,6 @@ namespace OCA\Tasks\Controller;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Http\NotFoundResponse;
use \OCP\IRequest;
use \OCP\IUserSession;
use \OCP\IConfig;
@ -34,44 +33,40 @@ use \OCP\IConfig;
*/
class PageController extends Controller {
/**
* @var IUserSession
*/
private $userSession;
/**
* @var IConfig
*/
private $config;
/**
* @param string $appName
* @param IRequest $request an instance of the request
* @param IUserSession $userSession
* @param IConfig $config
*/
public function __construct($appName, IRequest $request, IUserSession $userSession,
$userId, IConfig $config) {
public function __construct(string $appName, IRequest $request, IUserSession $userSession, IConfig $config) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userSession = $userSession;
$this->userId = $userId;
$this->config = $config;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse
*/
public function index() {
public function index():TemplateResponse {
\OCP\Util::connectHook('\OCP\Config', 'js', $this, 'addJavaScriptVariablesForIndex');
return new TemplateResponse('tasks', 'main');
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function templates($template) {
$templates = array( 'confirmation');
if (in_array($template, $templates)) {
$response = new TemplateResponse('tasks', $template, [], 'blank');
} else {
$response = new NotFoundResponse();
}
return $response;
}
/**
* Add parameters to javascript for user sites
*

View File

@ -27,12 +27,20 @@ use \OCP\AppFramework\Controller;
use \OCP\IRequest;
class SettingsController extends Controller {
/**
* @var SettingsService
*/
private $settingsService;
use Response;
public function __construct($appName, IRequest $request, SettingsService $settingsService){
/**
* @param string $appName
* @param IRequest $request an instance of the request
* @param SettingsService $settingsService
*/
public function __construct(string $appName, IRequest $request, SettingsService $settingsService){
parent::__construct($appName, $request);
$this->settingsService = $settingsService;
}
@ -49,7 +57,7 @@ class SettingsController extends Controller {
/**
* @NoAdminRequired
*/
public function set($setting, $value){
public function set($setting, $value) {
return $this->generateResponse(function () use ($setting, $value) {
return $this->settingsService->set($setting, $value);
});

View File

@ -27,12 +27,33 @@ use OCP\IL10N;
class CollectionsService {
/**
* @var string
*/
private $userId;
/**
* @var IL10N
*/
private $l10n;
/**
* @var IConfig
*/
private $settings;
/**
* @var string
*/
private $appName;
public function __construct($userId, IL10N $l10n, IConfig $settings, $appName) {
/**
* @param string $userId
* @param IL10N $l10n
* @param IConfig $settings
* @param string $appName
*/
public function __construct(string $userId, IL10N $l10n, IConfig $settings, string $appName) {
$this->userId = $userId;
$this->l10n = $l10n;
$this->settings = $settings;
@ -40,11 +61,11 @@ class CollectionsService {
}
/**
* get all collections
* Get all collections
*
* @return array
*/
public function getAll() {
public function getAll():array {
$collections = array(
array(
'id' => "starred",
@ -89,13 +110,13 @@ class CollectionsService {
}
/**
* set the visibility of a collection by collectionID
* Set the visibility of a collection by collectionID
*
* @param int $collectionID
* @param string $collectionID
* @param int $visibility
* @return bool
*/
public function setVisibility($collectionID, $visibility){
public function setVisibility(string $collectionID, int $visibility):bool {
if (in_array($visibility, array(0,1,2))){
$this->settings->setUserValue($this->userId, $this->appName,'show_'.$collectionID,$visibility);
}

View File

@ -26,22 +26,38 @@ use OCP\IConfig;
class SettingsService {
/**
* @var string
*/
private $userId;
/**
* @var IConfig
*/
private $settings;
/**
* @var string
*/
private $appName;
public function __construct($userId, IConfig $settings, $appName) {
/**
* @param string $userId
* @param IConfig $settings
* @param string $appName
*/
public function __construct(string $userId, IConfig $settings, string $appName) {
$this->userId = $userId;
$this->settings = $settings;
$this->appName = $appName;
}
/**
* get the current settings
* Get the current settings
*
* @return array
*/
public function get() {
public function get():array {
$settings = array(
'defaultCalendarId' => (string)$this->settings->getUserValue($this->userId, $this->appName,'various_defaultCalendarId'),
'showHidden' => (int)$this->settings->getUserValue($this->userId, $this->appName,'various_showHidden'),
@ -53,14 +69,13 @@ class SettingsService {
}
/**
* set setting of type to new value
* Set setting of type to new value
*
* @param $setting
* @param $type
* @param $value
* @return bool
*/
public function set($setting, $value) {
public function set($setting, $value):bool {
$this->settings->setUserValue($this->userId, $this->appName, 'various_'.$setting, $value);
return true;
}