From 517e4ca5435106ab5304849248cbea4e9dffd4b0 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 19 Apr 2014 13:20:54 +0200 Subject: split up api class for easier testing and clearer code --- app/news.php | 109 ++-- appinfo/app.php | 17 +- businesslayer/feedbusinesslayer.php | 30 +- businesslayer/folderbusinesslayer.php | 9 +- controller/apicontroller.php | 13 +- controller/exportcontroller.php | 23 +- controller/feedapicontroller.php | 44 +- controller/feedcontroller.php | 61 ++- controller/folderapicontroller.php | 29 +- controller/foldercontroller.php | 38 +- controller/itemapicontroller.php | 33 +- controller/itemcontroller.php | 54 +- controller/pagecontroller.php | 23 +- core/api.php | 546 --------------------- core/db.php | 57 +++ core/logger.php | 62 +++ core/settings.php | 111 +++++ db/feedmapper.php | 6 +- db/foldermapper.php | 6 +- db/itemmapper.php | 6 +- db/mapper.php | 13 +- db/mapperfactory.php | 16 +- db/postgres/itemmapper.php | 6 +- fetcher/feedfetcher.php | 6 +- tests/integration/db/ItemMapperIntegrationTest.php | 81 --- tests/unit/articleenhancer/EnhancerTest.php | 2 +- .../articleenhancer/RegexArticleEnhancerTest.php | 2 +- .../articleenhancer/XPathArticleEnhancerTest.php | 2 +- tests/unit/businesslayer/BusinessLayerTest.php | 4 +- tests/unit/businesslayer/FeedBusinessLayerTest.php | 26 +- .../unit/businesslayer/FolderBusinessLayerTest.php | 19 +- tests/unit/businesslayer/ItemBusinessLayerTest.php | 4 +- tests/unit/businesslayer/StatusFlagTest.php | 2 +- tests/unit/controller/ApiControllerTest.php | 19 +- tests/unit/controller/ExportControllerTest.php | 16 +- tests/unit/controller/FeedApiControllerTest.php | 102 ++-- tests/unit/controller/FeedControllerTest.php | 87 +--- tests/unit/controller/FolderApiControllerTest.php | 87 ++-- tests/unit/controller/FolderControllerTest.php | 55 +-- tests/unit/controller/ItemApiControllerTest.php | 156 +++--- tests/unit/controller/ItemControllerTest.php | 67 +-- tests/unit/controller/PageControllerTest.php | 36 +- tests/unit/db/FeedMapperTest.php | 2 +- tests/unit/db/FolderMapperTest.php | 2 +- tests/unit/db/ItemMapperTest.php | 2 +- tests/unit/db/MapperFactoryTest.php | 19 +- tests/unit/db/MapperTest.php | 18 +- tests/unit/db/postgres/ItemMapperTest.php | 2 +- tests/unit/fetcher/FeedFetcherTest.php | 5 +- tests/unit/fetcher/FetcherTest.php | 2 +- tests/unit/utility/ConfigTest.php | 12 +- tests/unit/utility/OPMLExporterTest.php | 2 +- utility/config.php | 12 +- utility/controllertestutility.php | 2 +- utility/mappertestutility.php | 23 +- utility/testutility.php | 46 -- 56 files changed, 802 insertions(+), 1432 deletions(-) delete mode 100644 core/api.php create mode 100644 core/db.php create mode 100644 core/logger.php create mode 100644 core/settings.php delete mode 100644 tests/integration/db/ItemMapperIntegrationTest.php delete mode 100644 utility/testutility.php diff --git a/app/news.php b/app/news.php index 259163904..aeb12f620 100644 --- a/app/news.php +++ b/app/news.php @@ -28,8 +28,9 @@ namespace OCA\News\App; use \OC\Files\View; use \OCP\AppFramework\App; - -use \OCA\News\Core\API; +use \OCA\News\Core\Logger; +use \OCA\News\Core\Db; +use \OCA\News\Core\Settings; use \OCA\News\Controller\PageController; use \OCA\News\Controller\FolderController; @@ -89,82 +90,96 @@ class News extends App { */ $container->registerService('PageController', function($c) { return new PageController( - $c->query('API'), - $c->query('Request') + $c->query('AppName'), + $c->query('Request'), + $c->query('Settings'), + $c->query('L10N') ); }); $container->registerService('FolderController', function($c) { return new FolderController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), $c->query('FolderBusinessLayer'), $c->query('FeedBusinessLayer'), - $c->query('ItemBusinessLayer') + $c->query('ItemBusinessLayer'), + $c->query('UserId') ); }); $container->registerService('FeedController', function($c) { return new FeedController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), $c->query('FolderBusinessLayer'), $c->query('FeedBusinessLayer'), - $c->query('ItemBusinessLayer') + $c->query('ItemBusinessLayer'), + $c->query('UserId'), + $c->query('Settings') ); }); $container->registerService('ItemController', function($c) { return new ItemController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), $c->query('FeedBusinessLayer'), - $c->query('ItemBusinessLayer') + $c->query('ItemBusinessLayer'), + $c->query('UserId'), + $c->query('Settings') ); }); $container->registerService('ExportController', function($c) { return new ExportController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), $c->query('FeedBusinessLayer'), $c->query('FolderBusinessLayer'), $c->query('ItemBusinessLayer'), - $c->query('OPMLExporter')); + $c->query('OPMLExporter'), + $c->query('UserId') + ); }); $container->registerService('ApiController', function($c) { return new ApiController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), - $c->query('Updater') + $c->query('Updater'), + $c->query('Settings') ); }); $container->registerService('FolderApiController', function($c) { return new FolderApiController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), $c->query('FolderBusinessLayer'), - $c->query('ItemBusinessLayer') + $c->query('ItemBusinessLayer'), + $c->query('UserId') ); }); $container->registerService('FeedApiController', function($c) { return new FeedApiController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), $c->query('FolderBusinessLayer'), $c->query('FeedBusinessLayer'), - $c->query('ItemBusinessLayer') + $c->query('ItemBusinessLayer'), + $c->query('Logger'), + $c->query('UserId') ); }); $container->registerService('ItemApiController', function($c) { return new ItemApiController( - $c->query('API'), + $c->query('AppName'), $c->query('Request'), - $c->query('ItemBusinessLayer') + $c->query('ItemBusinessLayer'), + $c->query('UserId') ); }); @@ -175,7 +190,7 @@ class News extends App { $container->registerService('FolderBusinessLayer', function($c) { return new FolderBusinessLayer( $c->query('FolderMapper'), - $c->query('API'), + $c->query('L10N'), $c->query('TimeFactory'), $c->query('Config') ); @@ -186,7 +201,8 @@ class News extends App { $c->query('FeedMapper'), $c->query('Fetcher'), $c->query('ItemMapper'), - $c->query('API'), + $c->query('Logger'), + $c->query('L10N'), $c->query('TimeFactory'), $c->query('Config'), $c->query('Enhancer'), @@ -209,38 +225,55 @@ class News extends App { */ $container->registerService('MapperFactory', function($c) { return new MapperFactory( - $c->query('API') + $c->query('Settings'), $c->query('Db') ); }); $container->registerService('FolderMapper', function($c) { return new FolderMapper( - $c->query('API') + $c->query('Db') ); }); $container->registerService('FeedMapper', function($c) { return new FeedMapper( - $c->query('API') + $c->query('Db') ); }); $container->registerService('ItemMapper', function($c) { return $c->query('MapperFactory')->getItemMapper( - $c->query('API') + $c->query('Db') ); }); - /** - * Utility - */ - $container->registerService('API', function($c){ - return new API( - $c->query('AppName') - ); + * Core + */ + $container->registerService('L10N', function($c) { + return \OC_L10N::get($c['AppName']); + }); + + $container->registerService('UserId', function($c) { + return \OCP\User::getUser(); + }); + + $container->registerService('Logger', function($c) { + return new Logger($c['AppName']); + }); + + $container->registerService('Db', function($c) { + return new Db($c['AppName']); + }); + + $container->registerService('Settings', function($c) { + return new Settings($c['AppName'], $c['UserId']); }); + + /** + * Utility + */ $container->registerService('ConfigView', function($c) { $view = new View('/news/config'); if (!$view->file_exists('')) { @@ -251,13 +284,13 @@ class News extends App { }); $container->registerService('Config', function($c) { - $config = new Config($c->query('ConfigView'), $c->query('API')); + $config = new Config($c->query('ConfigView'), $c->query('Logger')); $config->read('config.ini', true); return $config; }); $container->registerService('simplePieCacheDirectory', function($c) { - $directory = $c->query('API')->getSystemValue('datadirectory') . + $directory = $c->query('Settings')->getSystemValue('datadirectory') . '/news/cache/simplepie'; if(!is_dir($directory)) { @@ -267,7 +300,7 @@ class News extends App { }); $container->registerService('HTMLPurifier', function($c) { - $directory = $c->query('API')->getSystemValue('datadirectory') . + $directory = $c->query('Settings')->getSystemValue('datadirectory') . '/news/cache/purifier'; if(!is_dir($directory)) { @@ -329,9 +362,7 @@ class News extends App { }); $container->registerService('FeedFetcher', function($c) { - return new FeedFetcher( - $c->query('API'), - $c->query('SimplePieAPIFactory'), + return new FeedFetcher($c->query('SimplePieAPIFactory'), $c->query('FaviconFetcher'), $c->query('TimeFactory'), $c->query('simplePieCacheDirectory'), diff --git a/appinfo/app.php b/appinfo/app.php index 33dc340ab..486c234a7 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -24,31 +24,28 @@ namespace OCA\News; -use \OCA\News\Core\API; -$api = new API('news'); - -$api->addNavigationEntry(array( +\OCP\App::addNavigationEntry(array( // the string under which your app will be referenced in owncloud - 'id' => $api->getAppName(), + 'id' => 'news', // sorting weight for the navigation. The higher the number, the higher // will it be listed in the navigation 'order' => 10, // the route that will be shown on startup - 'href' => $api->linkToRoute('news.page.index'), + 'href' => \OCP\Util::linkToRoute('news.page.index'), // the icon that will be shown in the navigation // this file needs to exist in img/example.png - 'icon' => $api->imagePath('news.svg'), + 'icon' => \OCP\Util::imagePath('news', 'news.svg'), // the title of your application. This will be used in the // navigation or on the settings page of your app - 'name' => $api->getTrans()->t('News') + 'name' => \OC_L10N::get('news')->t('News') )); -$api->addRegularTask('OCA\News\Backgroundjob\Task', 'run'); -$api->connectHook('OC_User', 'pre_deleteUser', 'OCA\News\Hooks\User', 'deleteUser'); +\OCP\Backgroundjob::addRegularTask('OCA\News\Backgroundjob\Task', 'run'); +\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\News\Hooks\User', 'deleteUser'); diff --git a/businesslayer/feedbusinesslayer.php b/businesslayer/feedbusinesslayer.php index 1322bc73f..caa81e0d3 100644 --- a/businesslayer/feedbusinesslayer.php +++ b/businesslayer/feedbusinesslayer.php @@ -24,8 +24,7 @@ */ namespace OCA\News\BusinessLayer; - -use \OCA\News\Core\API; +use \OCA\News\Core\Logger; use \OCA\News\Db\DoesNotExistException; use \OCA\News\Db\Feed; use \OCA\News\Db\Item; @@ -41,14 +40,18 @@ class FeedBusinessLayer extends BusinessLayer { private $feedFetcher; private $itemMapper; - private $api; + private $logger; + private $l10n; private $timeFactory; private $autoPurgeMinimumInterval; private $enhancer; private $purifier; - public function __construct(FeedMapper $feedMapper, Fetcher $feedFetcher, - ItemMapper $itemMapper, API $api, + public function __construct(FeedMapper $feedMapper, + Fetcher $feedFetcher, + ItemMapper $itemMapper, + Logger $logger, + $l10n, $timeFactory, Config $config, Enhancer $enhancer, @@ -56,7 +59,8 @@ class FeedBusinessLayer extends BusinessLayer { parent::__construct($feedMapper); $this->feedFetcher = $feedFetcher; $this->itemMapper = $itemMapper; - $this->api = $api; + $this->logger = $logger; + $this->l10n = $l10n; $this->timeFactory = $timeFactory; $this->autoPurgeMinimumInterval = $config->getAutoPurgeMinimumInterval(); $this->enhancer = $enhancer; @@ -100,7 +104,7 @@ class FeedBusinessLayer extends BusinessLayer { try { $this->mapper->findByUrlHash($feed->getUrlHash(), $userId); throw new BusinessLayerConflictException( - $this->api->getTrans()->t('Can not add feed: Exists already')); + $this->l10n->t('Can not add feed: Exists already')); } catch(DoesNotExistException $ex){} // insert feed @@ -135,9 +139,9 @@ class FeedBusinessLayer extends BusinessLayer { return $feed; } catch(FetcherException $ex){ - $this->api->log($ex->getMessage(), 'debug'); + $this->logger->log($ex->getMessage(), 'debug'); throw new BusinessLayerException( - $this->api->getTrans()->t( + $this->l10n->t( 'Can not add feed: URL does not exist or has invalid xml')); } } @@ -153,7 +157,7 @@ class FeedBusinessLayer extends BusinessLayer { try { $this->update($feed->getId(), $feed->getUserId()); } catch(BusinessLayerException $ex){ - $this->api->log('Could not update feed ' . $ex->getMessage(), + $this->logger->log('Could not update feed ' . $ex->getMessage(), 'debug'); } } @@ -203,9 +207,9 @@ class FeedBusinessLayer extends BusinessLayer { } catch(FetcherException $ex){ // failed updating is not really a problem, so only log it - $this->api->log('Can not update feed with url ' . $existingFeed->getUrl() . + $this->logger->log('Can not update feed with url ' . $existingFeed->getUrl() . ': Not found or bad source', 'debug'); - $this->api->log($ex->getMessage(), 'debug'); + $this->logger->log($ex->getMessage(), 'debug'); } return $this->mapper->find($feedId, $userId); @@ -282,7 +286,7 @@ class FeedBusinessLayer extends BusinessLayer { $feed->setUserId($userId); $feed->setLink($url); $feed->setUrl($url); - $feed->setTitle($this->api->getTrans()->t('Articles without feed')); + $feed->setTitle($this->l10n->t('Articles without feed')); $feed->setAdded($this->timeFactory->getTime()); $feed->setFolderId(0); $feed->setPreventUpdate(true); diff --git a/businesslayer/folderbusinesslayer.php b/businesslayer/folderbusinesslayer.php index 80634abc8..5c5914c7a 100644 --- a/businesslayer/folderbusinesslayer.php +++ b/businesslayer/folderbusinesslayer.php @@ -25,7 +25,6 @@ namespace OCA\News\BusinessLayer; -use \OCA\News\Core\API; use \OCA\News\Db\Folder; use \OCA\News\Db\FolderMapper; use \OCA\News\Utility\Config; @@ -33,16 +32,16 @@ use \OCA\News\Utility\Config; class FolderBusinessLayer extends BusinessLayer { - private $api; + private $l10n; private $timeFactory; private $autoPurgeMinimumInterval; public function __construct(FolderMapper $folderMapper, - API $api, + $l10n, $timeFactory, Config $config){ parent::__construct($folderMapper); - $this->api = $api; + $this->l10n = $l10n; $this->timeFactory = $timeFactory; $this->autoPurgeMinimumInterval = $config->getAutoPurgeMinimumInterval(); } @@ -62,7 +61,7 @@ class FolderBusinessLayer extends BusinessLayer { if(count($existingFolders) > 0){ throw new BusinessLayerConflictException( - $this->api->getTrans()->t('Can not add folder: Exists already')); + $this->l10n->t('Can not add folder: Exists already')); } if(mb_strlen($folderName) === 0) { diff --git a/controller/apicontroller.php b/controller/apicontroller.php index 9e0ff2401..25b230016 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -32,17 +32,18 @@ use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Http\Response; use \OCA\News\Utility\Updater; -use \OCA\News\Core\API; +use \OCA\News\Core\Settings; class ApiController extends Controller { private $updater; - private $api; + private $settings; - public function __construct(API $api, IRequest $request, Updater $updater){ - parent::__construct($api->getAppName(), $request); + public function __construct($appName, IRequest $request, Updater $updater, + Settings $settings){ + parent::__construct($appName, $request); $this->updater = $updater; - $this->api = $api; + $this->settings = $settings; } @@ -52,7 +53,7 @@ class ApiController extends Controller { * @API */ public function version() { - $version = $this->api->getAppValue('installed_version'); + $version = $this->settings->getAppValue('installed_version'); $response = new JSONResponse(array('version' => $version)); return $response; } diff --git a/controller/exportcontroller.php b/controller/exportcontroller.php index 33cb05204..a0011f9e6 100644 --- a/controller/exportcontroller.php +++ b/controller/exportcontroller.php @@ -32,7 +32,6 @@ use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Http\Response; use \OCA\News\Http\TextDownloadResponse; -use \OCA\News\Core\API; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; @@ -44,19 +43,21 @@ class ExportController extends Controller { private $folderBusinessLayer; private $feedBusinessLayer; private $itemBusinessLayer; - private $api; + private $userId; - public function __construct(API $api, IRequest $request, + public function __construct($appName, + IRequest $request, FeedBusinessLayer $feedBusinessLayer, FolderBusinessLayer $folderBusinessLayer, ItemBusinessLayer $itemBusinessLayer, - OPMLExporter $opmlExporter){ - parent::__construct($api->getAppName(), $request); + OPMLExporter $opmlExporter, + $userId){ + parent::__construct($appName, $request); $this->feedBusinessLayer = $feedBusinessLayer; $this->folderBusinessLayer = $folderBusinessLayer; $this->opmlExporter = $opmlExporter; $this->itemBusinessLayer = $itemBusinessLayer; - $this->api = $api; + $this->userId = $userId; } @@ -65,9 +66,8 @@ class ExportController extends Controller { * @NoCSRFRequired */ public function opml(){ - $userId = $this->api->getUserId(); - $feeds = $this->feedBusinessLayer->findAll($userId); - $folders = $this->folderBusinessLayer->findAll($userId); + $feeds = $this->feedBusinessLayer->findAll($this->userId); + $folders = $this->folderBusinessLayer->findAll($this->userId); $opml = $this->opmlExporter->build($folders, $feeds)->saveXML(); return new TextDownloadResponse($opml, 'subscriptions.opml', 'text/xml'); } @@ -78,9 +78,8 @@ class ExportController extends Controller { * @NoCSRFRequired */ public function articles(){ - $userId = $this->api->getUserId(); - $feeds = $this->feedBusinessLayer->findAll($userId); - $items = $this->itemBusinessLayer->getUnreadOrStarred($userId); + $feeds = $this->feedBusinessLayer->findAll($this->userId); + $items = $this->itemBusinessLayer->getUnreadOrStarred($this->userId); // build assoc array for fast access $feedsDict = array(); diff --git a/controller/feedapicontroller.php b/controller/feedapicontroller.php index 6537e51df..51aa48dd9 100644 --- a/controller/feedapicontroller.php +++ b/controller/feedapicontroller.php @@ -30,7 +30,7 @@ use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\API; +use \OCA\News\Core\Logger; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; @@ -43,18 +43,22 @@ class FeedApiController extends Controller { private $itemBusinessLayer; private $feedBusinessLayer; private $folderBusinessLayer; - private $api; + private $userId; + private $logger; - public function __construct(API $api, + public function __construct($appName, IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api->getAppName(), $request); + ItemBusinessLayer $itemBusinessLayer, + Logger $logger, + $userId){ + parent::__construct($appName, $request); $this->folderBusinessLayer = $folderBusinessLayer; $this->feedBusinessLayer = $feedBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; - $this->api = $api; + $this->userId = $userId; + $this->logger = $logger; } @@ -64,21 +68,20 @@ class FeedApiController extends Controller { * @API */ public function index() { - $userId = $this->api->getUserId(); $result = array( 'feeds' => array(), - 'starredCount' => $this->itemBusinessLayer->starredCount($userId) + 'starredCount' => $this->itemBusinessLayer->starredCount($this->userId) ); - foreach ($this->feedBusinessLayer->findAll($userId) as $feed) { + foreach ($this->feedBusinessLayer->findAll($this->userId) as $feed) { array_push($result['feeds'], $feed->toAPI()); } // check case when there are no items try { $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); + $this->itemBusinessLayer->getNewestItemId($this->userId); } catch(BusinessLayerException $ex) {} return new JSONResponse($result); @@ -91,21 +94,20 @@ class FeedApiController extends Controller { * @API */ public function create() { - $userId = $this->api->getUserId(); $feedUrl = $this->params('url'); $folderId = (int) $this->params('folderId', 0); try { - $this->feedBusinessLayer->purgeDeleted($userId, false); + $this->feedBusinessLayer->purgeDeleted($this->userId, false); - $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId); + $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $this->userId); $result = array( 'feeds' => array($feed->toAPI()) ); try { $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); + $this->itemBusinessLayer->getNewestItemId($this->userId); } catch(BusinessLayerException $ex) {} return new JSONResponse($result); @@ -126,11 +128,10 @@ class FeedApiController extends Controller { * @API */ public function delete() { - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); try { - $this->feedBusinessLayer->delete($feedId, $userId); + $this->feedBusinessLayer->delete($feedId, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), @@ -145,11 +146,10 @@ class FeedApiController extends Controller { * @API */ public function read() { - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $newestItemId = (int) $this->params('newestItemId'); - $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $userId); + $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $this->userId); return new JSONResponse(); } @@ -160,12 +160,11 @@ class FeedApiController extends Controller { * @API */ public function move() { - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $folderId = (int) $this->params('folderId'); try { - $this->feedBusinessLayer->move($feedId, $folderId, $userId); + $this->feedBusinessLayer->move($feedId, $folderId, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), @@ -180,12 +179,11 @@ class FeedApiController extends Controller { * @API */ public function rename() { - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $feedTitle = $this->params('feedTitle'); try { - $this->feedBusinessLayer->rename($feedId, $feedTitle, $userId); + $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), @@ -225,7 +223,7 @@ class FeedApiController extends Controller { $this->feedBusinessLayer->update($feedId, $userId); // ignore update failure (feed could not be reachable etc, we dont care) } catch(\Exception $ex) { - $this->api->log('Could not update feed ' . $ex->getMessage(), + $this->logger->log('Could not update feed ' . $ex->getMessage(), 'debug'); } return new JSONResponse(); diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index cfd4458a9..9d7f091bc 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -30,7 +30,7 @@ use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\API; +use \OCA\News\Core\Settings; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; @@ -44,17 +44,22 @@ class FeedController extends Controller { private $feedBusinessLayer; private $folderBusinessLayer; private $itemBusinessLayer; - private $api; + private $userId; + private $settings; - public function __construct(API $api, IRequest $request, + public function __construct($appName, + IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api->getAppName(), $request); + ItemBusinessLayer $itemBusinessLayer, + $userId, + Settings $settings){ + parent::__construct($appName, $request); $this->feedBusinessLayer = $feedBusinessLayer; $this->folderBusinessLayer = $folderBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; - $this->api = $api; + $this->userId = $userId; + $this->settings = $settings; } @@ -62,19 +67,18 @@ class FeedController extends Controller { * @NoAdminRequired */ public function index(){ - $userId = $this->api->getUserId(); // this method is also used to update the interface // because of this we also pass the starred count and the newest // item id which will be used for marking feeds read $params = array( - 'feeds' => $this->feedBusinessLayer->findAll($userId), - 'starred' => $this->itemBusinessLayer->starredCount($userId) + 'feeds' => $this->feedBusinessLayer->findAll($this->userId), + 'starred' => $this->itemBusinessLayer->starredCount($this->userId) ); try { $params['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); + $this->itemBusinessLayer->getNewestItemId($this->userId); } catch (BusinessLayerException $ex) {} return new JSONResponse($params); @@ -85,9 +89,8 @@ class FeedController extends Controller { * @NoAdminRequired */ public function active(){ - $userId = $this->api->getUserId(); - $feedId = (int) $this->api->getUserValue('lastViewedFeedId'); - $feedType = $this->api->getUserValue('lastViewedFeedType'); + $feedId = (int) $this->settings->getUserValue('lastViewedFeedId'); + $feedType = $this->settings->getUserValue('lastViewedFeedType'); // cast from null to int is 0 if($feedType !== null){ @@ -97,10 +100,10 @@ class FeedController extends Controller { // check if feed or folder exists try { if($feedType === FeedType::FOLDER){ - $this->folderBusinessLayer->find($feedId, $userId); + $this->folderBusinessLayer->find($feedId, $this->userId); } elseif ($feedType === FeedType::FEED){ - $this->feedBusinessLayer->find($feedId, $userId); + $this->feedBusinessLayer->find($feedId, $this->userId); // if its the first launch, those values will be null } elseif($feedType === null){ @@ -129,21 +132,20 @@ class FeedController extends Controller { public function create(){ $url = $this->params('url'); $parentFolderId = (int) $this->params('parentFolderId'); - $userId = $this->api->getUserId(); try { // we need to purge deleted feeds if a feed is created to // prevent already exists exceptions - $this->feedBusinessLayer->purgeDeleted($userId, false); + $this->feedBusinessLayer->purgeDeleted($this->userId, false); - $feed = $this->feedBusinessLayer->create($url, $parentFolderId, $userId); + $feed = $this->feedBusinessLayer->create($url, $parentFolderId, $this->userId); $params = array( 'feeds' => array($feed) ); try { $params['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); + $this->itemBusinessLayer->getNewestItemId($this->userId); } catch (BusinessLayerException $ex) {} return new JSONResponse($params); @@ -166,10 +168,9 @@ class FeedController extends Controller { */ public function delete(){ $feedId = (int) $this->params('feedId'); - $userId = $this->api->getUserId(); try { - $this->feedBusinessLayer->markDeleted($feedId, $userId); + $this->feedBusinessLayer->markDeleted($feedId, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( @@ -185,9 +186,8 @@ class FeedController extends Controller { public function update(){ try { $feedId = (int) $this->params('feedId'); - $userId = $this->api->getUserId(); - $feed = $this->feedBusinessLayer->update($feedId, $userId); + $feed = $this->feedBusinessLayer->update($feedId, $this->userId); $params = array( 'feeds' => array( @@ -216,10 +216,9 @@ class FeedController extends Controller { public function move(){ $feedId = (int) $this->params('feedId'); $parentFolderId = (int) $this->params('parentFolderId'); - $userId = $this->api->getUserId(); try { - $this->feedBusinessLayer->move($feedId, $parentFolderId, $userId); + $this->feedBusinessLayer->move($feedId, $parentFolderId, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( @@ -234,10 +233,9 @@ class FeedController extends Controller { public function rename() { $feedId = (int) $this->params('feedId'); $feedTitle = $this->params('feedTitle'); - $userId = $this->api->getUserId(); try { - $this->feedBusinessLayer->rename($feedId, $feedTitle, $userId); + $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( @@ -251,9 +249,8 @@ class FeedController extends Controller { */ public function import() { $json = $this->params('json'); - $userId = $this->api->getUserId(); - $feed = $this->feedBusinessLayer->importArticles($json, $userId); + $feed = $this->feedBusinessLayer->importArticles($json, $this->userId); $params = array(); if($feed) { @@ -268,11 +265,10 @@ class FeedController extends Controller { * @NoAdminRequired */ public function read(){ - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $highestItemId = (int) $this->params('highestItemId'); - $this->itemBusinessLayer->readFeed($feedId, $highestItemId, $userId); + $this->itemBusinessLayer->readFeed($feedId, $highestItemId, $this->userId); $params = array( 'feeds' => array( @@ -291,10 +287,9 @@ class FeedController extends Controller { */ public function restore(){ $feedId = (int) $this->params('feedId'); - $userId = $this->api->getUserId(); try { - $this->feedBusinessLayer->unmarkDeleted($feedId, $userId); + $this->feedBusinessLayer->unmarkDeleted($feedId, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php index 0954380c3..24a7ba151 100644 --- a/controller/folderapicontroller.php +++ b/controller/folderapicontroller.php @@ -30,7 +30,6 @@ use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\API; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\BusinessLayerException; @@ -42,16 +41,17 @@ class FolderApiController extends Controller { private $folderBusinessLayer; private $itemBusinessLayer; - private $api; + private $userId; - public function __construct(API $api, + public function __construct($appName, IRequest $request, FolderBusinessLayer $folderBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api->getAppName(), $request); + ItemBusinessLayer $itemBusinessLayer, + $userId){ + parent::__construct($appName, $request); $this->folderBusinessLayer = $folderBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; - $this->api = $api; + $this->userId = $userId; } @@ -61,12 +61,11 @@ class FolderApiController extends Controller { * @API */ public function index() { - $userId = $this->api->getUserId(); $result = array( 'folders' => array() ); - foreach ($this->folderBusinessLayer->findAll($userId) as $folder) { + foreach ($this->folderBusinessLayer->findAll($this->userId) as $folder) { array_push($result['folders'], $folder->toAPI()); } @@ -80,15 +79,14 @@ class FolderApiController extends Controller { * @API */ public function create() { - $userId = $this->api->getUserId(); $folderName = $this->params('name'); $result = array( 'folders' => array() ); try { - $this->folderBusinessLayer->purgeDeleted($userId, false); - $folder = $this->folderBusinessLayer->create($folderName, $userId); + $this->folderBusinessLayer->purgeDeleted($this->userId, false); + $folder = $this->folderBusinessLayer->create($folderName, $this->userId); array_push($result['folders'], $folder->toAPI()); return new JSONResponse($result); @@ -110,11 +108,10 @@ class FolderApiController extends Controller { * @API */ public function delete() { - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); try { - $this->folderBusinessLayer->delete($folderId, $userId); + $this->folderBusinessLayer->delete($folderId, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), @@ -129,12 +126,11 @@ class FolderApiController extends Controller { * @API */ public function update() { - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); $folderName = $this->params('name'); try { - $this->folderBusinessLayer->rename($folderId, $folderName, $userId); + $this->folderBusinessLayer->rename($folderId, $folderName, $this->userId); return new JSONResponse(); } catch(BusinessLayerValidationException $ex) { @@ -158,11 +154,10 @@ class FolderApiController extends Controller { * @API */ public function read() { - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); $newestItemId = (int) $this->params('newestItemId'); - $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $userId); + $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $this->userId); return new JSONResponse(); } diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index 557315665..1e0eea390 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -30,7 +30,6 @@ use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\API; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; @@ -43,17 +42,18 @@ class FolderController extends Controller { private $folderBusinessLayer; private $feedBusinessLayer; private $itemBusinessLayer; - private $api; + private $userId; - public function __construct(API $api, IRequest $request, + public function __construct($appName, IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api->getAppName(), $request); + ItemBusinessLayer $itemBusinessLayer, + $userId){ + parent::__construct($appName, $request); $this->folderBusinessLayer = $folderBusinessLayer; $this->feedBusinessLayer = $feedBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; - $this->api = $api; + $this->userId = $userId; } @@ -61,7 +61,7 @@ class FolderController extends Controller { * @NoAdminRequired */ public function index(){ - $folders = $this->folderBusinessLayer->findAll($this->api->getUserId()); + $folders = $this->folderBusinessLayer->findAll($this->userId); $result = array( 'folders' => $folders ); @@ -70,10 +70,9 @@ class FolderController extends Controller { private function setOpened($isOpened){ - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); - $this->folderBusinessLayer->open($folderId, $isOpened, $userId); + $this->folderBusinessLayer->open($folderId, $isOpened, $this->userId); } @@ -111,15 +110,14 @@ class FolderController extends Controller { * @NoAdminRequired */ public function create(){ - $userId = $this->api->getUserId(); $folderName = $this->params('folderName'); try { // we need to purge deleted folders if a folder is created to // prevent already exists exceptions - $this->folderBusinessLayer->purgeDeleted($userId, false); + $this->folderBusinessLayer->purgeDeleted($this->userId, false); - $folder = $this->folderBusinessLayer->create($folderName, $userId); + $folder = $this->folderBusinessLayer->create($folderName, $this->userId); $params = array( 'folders' => array($folder) @@ -146,11 +144,10 @@ class FolderController extends Controller { * @NoAdminRequired */ public function delete(){ - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); try { - $this->folderBusinessLayer->markDeleted($folderId, $userId); + $this->folderBusinessLayer->markDeleted($folderId, $this->userId); return new JSONResponse(); } catch (BusinessLayerException $ex){ return new JSONResponse(array( @@ -164,12 +161,13 @@ class FolderController extends Controller { * @NoAdminRequired */ public function rename(){ - $userId = $this->api->getUserId(); + $userId = $this->userId; $folderName = $this->params('folderName'); $folderId = (int) $this->params('folderId'); try { - $folder = $this->folderBusinessLayer->rename($folderId, $folderName, $userId); + $folder = $this->folderBusinessLayer->rename($folderId, $folderName, + $this->userId); $params = array( 'folders' => array($folder) @@ -197,14 +195,13 @@ class FolderController extends Controller { * @NoAdminRequired */ public function read(){ - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); $highestItemId = (int) $this->params('highestItemId'); - $this->itemBusinessLayer->readFolder($folderId, $highestItemId, $userId); + $this->itemBusinessLayer->readFolder($folderId, $highestItemId, $this->userId); $params = array( - 'feeds' => $this->feedBusinessLayer->findAll($userId) + 'feeds' => $this->feedBusinessLayer->findAll($this->userId) ); return new JSONResponse($params); } @@ -214,11 +211,10 @@ class FolderController extends Controller { * @NoAdminRequired */ public function restore(){ - $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); try { - $this->folderBusinessLayer->unmarkDeleted($folderId, $userId); + $this->folderBusinessLayer->unmarkDeleted($folderId, $this->userId); return new JSONResponse(); } catch (BusinessLayerException $ex){ return new JSONResponse(array( diff --git a/controller/itemapicontroller.php b/controller/itemapicontroller.php index 3b0a5457d..7b1852545 100644 --- a/controller/itemapicontroller.php +++ b/controller/itemapicontroller.php @@ -32,19 +32,19 @@ use \OCP\AppFramework\Http\JSONResponse; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\Core\API; class ItemApiController extends Controller { private $itemBusinessLayer; - private $api; + private $userId; - public function __construct(API $api, + public function __construct($appName, IRequest $request, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api->getAppName(), $request); + ItemBusinessLayer $itemBusinessLayer, + $userId){ + parent::__construct($appName, $request); $this->itemBusinessLayer = $itemBusinessLayer; - $this->api = $api; + $this->userId = $userId; } @@ -58,7 +58,6 @@ class ItemApiController extends Controller { 'items' => array() ); - $userId = $this->api->getUserId(); $batchSize = (int) $this->params('batchSize', 20); $offset = (int) $this->params('offset', 0); $type = (int) $this->params('type'); @@ -77,7 +76,7 @@ class ItemApiController extends Controller { $batchSize, $offset, $showAll, - $userId + $this->userId ); foreach ($items as $item) { @@ -98,7 +97,6 @@ class ItemApiController extends Controller { 'items' => array() ); - $userId = $this->api->getUserId(); $lastModified = (int) $this->params('lastModified', 0); $type = (int) $this->params('type'); $id = (int) $this->params('id'); @@ -108,7 +106,7 @@ class ItemApiController extends Controller { $type, $lastModified, true, - $userId + $this->userId ); foreach ($items as $item) { @@ -120,10 +118,9 @@ class ItemApiController extends Controller { private function setRead($isRead) { - $userId = $this->api->getUserId(); $itemId = (int) $this->params('itemId'); try { - $this->itemBusinessLayer->read($itemId, $isRead, $userId); + $this->itemBusinessLayer->read($itemId, $isRead, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex){ return new JSONResponse(array('message' => $ex->getMessage()), @@ -133,11 +130,10 @@ class ItemApiController extends Controller { private function setStarred($isStarred) { - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $guidHash = $this->params('guidHash'); try { - $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); + $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $this->userId); return new JSONResponse(); } catch(BusinessLayerException $ex){ return new JSONResponse(array('message' => $ex->getMessage()), @@ -192,21 +188,19 @@ class ItemApiController extends Controller { * @API */ public function readAll() { - $userId = $this->api->getUserId(); $newestItemId = (int) $this->params('newestItemId'); - $this->itemBusinessLayer->readAll($newestItemId, $userId); + $this->itemBusinessLayer->readAll($newestItemId, $this->userId); return new JSONResponse(); } private function setMultipleRead($isRead) { - $userId = $this->api->getUserId(); $items = $this->params('items'); foreach($items as $id) { try { - $this->itemBusinessLayer->read($id, $isRead, $userId); + $this->itemBusinessLayer->read($id, $isRead, $this->userId); } catch(BusinessLayerException $ex) { continue; } @@ -237,13 +231,12 @@ class ItemApiController extends Controller { private function setMultipleStarred($isStarred) { - $userId = $this->api->getUserId(); $items = $this->params('items'); foreach($items as $item) { try { $this->itemBusinessLayer->star($item['feedId'], - $item['guidHash'], $isStarred, $userId); + $item['guidHash'], $isStarred, $this->userId); } catch(BusinessLayerException $ex) { continue; } diff --git a/controller/itemcontroller.php b/controller/itemcontroller.php index 1e5170ac8..ff340ce30 100644 --- a/controller/itemcontroller.php +++ b/controller/itemcontroller.php @@ -30,7 +30,7 @@ use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\API; +use \OCA\News\Core\Settings; use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\FeedBusinessLayer; @@ -40,15 +40,20 @@ class ItemController extends Controller { private $itemBusinessLayer; private $feedBusinessLayer; - private $api; + private $userId; + private $settings; - public function __construct(API $api, IRequest $request, + public function __construct($appName, + IRequest $request, FeedBusinessLayer $feedBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api->getAppName(), $request); + ItemBusinessLayer $itemBusinessLayer, + $userId, + Settings $settings){ + parent::__construct($appName, $request); $this->itemBusinessLayer = $itemBusinessLayer; $this->feedBusinessLayer = $feedBusinessLayer; - $this->api = $api; + $this->userId = $userId; + $this->settings = $settings; } @@ -56,16 +61,15 @@ class ItemController extends Controller { * @NoAdminRequired */ public function index(){ - $userId = $this->api->getUserId(); - $showAll = $this->api->getUserValue('showAll') === '1'; + $showAll = $this->settings->getUserValue('showAll') === '1'; $limit = $this->params('limit'); $type = (int) $this->params('type'); $id = (int) $this->params('id'); $offset = (int) $this->params('offset', 0); - $this->api->setUserValue('lastViewedFeedId', $id); - $this->api->setUserValue('lastViewedFeedType', $type); + $this->settings->setUserValue('lastViewedFeedId', $id); + $this->settings->setUserValue('lastViewedFeedType', $type); $params = array(); @@ -76,13 +80,13 @@ class ItemController extends Controller { // out of sync if($offset === 0) { $params['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); - $params['feeds'] = $this->feedBusinessLayer->findAll($userId); - $params['starred'] = $this->itemBusinessLayer->starredCount($userId); + $this->itemBusinessLayer->getNewestItemId($this->userId); + $params['feeds'] = $this->feedBusinessLayer->findAll($this->userId); + $params['starred'] = $this->itemBusinessLayer->starredCount($this->userId); } $params['items'] = $this->itemBusinessLayer->findAll($id, $type, $limit, - $offset, $showAll, $userId); + $offset, $showAll, $this->userId); // this gets thrown if there are no items // in that case just return an empty array } catch(BusinessLayerException $ex) {} @@ -95,8 +99,7 @@ class ItemController extends Controller { * @NoAdminRequired */ public function newItems() { - $userId = $this->api->getUserId(); - $showAll = $this->api->getUserValue('showAll') === '1'; + $showAll = $this->settings->getUserValue('showAll') === '1'; $type = (int) $this->params('type'); $id = (int) $this->params('id'); @@ -105,11 +108,11 @@ class ItemController extends Controller { $params = array(); try { - $params['newestItemId'] = $this->itemBusinessLayer->getNewestItemId($userId); - $params['feeds'] = $this->feedBusinessLayer->findAll($userId); - $params['starred'] = $this->itemBusinessLayer->starredCount($userId); + $params['newestItemId'] = $this->itemBusinessLayer->getNewestItemId($this->userId); + $params['feeds'] = $this->feedBusinessLayer->findAll($this->userId); + $params['starred'] = $this->itemBusinessLayer->starredCount($this->userId); $params['items'] = $this->itemBusinessLayer->findAllNew($id, $type, - $lastModified, $showAll, $userId); + $lastModified, $showAll, $this->userId); // this gets thrown if there are no items // in that case just return an empty array } catch(BusinessLayerException $ex) {} @@ -119,11 +122,10 @@ class ItemController extends Controller { private function setStarred($isStarred){ - $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $guidHash = $this->params('guidHash'); - $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); + $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $this->userId); } @@ -158,10 +160,9 @@ class ItemController extends Controller { private function setRead($isRead){ - $userId = $this->api->getUserId(); $itemId = (int) $this->params('itemId'); - $this->itemBusinessLayer->read($itemId, $isRead, $userId); + $this->itemBusinessLayer->read($itemId, $isRead, $this->userId); } @@ -199,13 +200,12 @@ class ItemController extends Controller { * @NoAdminRequired */ public function readAll(){ - $userId = $this->api->getUserId(); $highestItemId = (int) $this->params('highestItemId'); - $this->itemBusinessLayer->readAll($highestItemId, $userId); + $this->itemBusinessLayer->readAll($highestItemId, $this->userId); $params = array( - 'feeds' => $this->feedBusinessLayer->findAll($userId) + 'feeds' => $this->feedBusinessLayer->findAll($this->userId) ); return new JSONResponse($params); } diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php index 137dcf514..25caf19e2 100644 --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -29,15 +29,18 @@ use \OCP\IRequest; use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Controller; -use \OCA\News\Core\API; +use \OCA\News\Core\Settings; class PageController extends Controller { - private $api; + private $settings; + private $l10n; - public function __construct(API $api, IRequest $request){ - parent::__construct($api->getAppName(), $request); - $this->api = $api; + public function __construct($appName, IRequest $request, Settings $settings, + $l10n){ + parent::__construct($appName, $request); + $this->settings = $settings; + $this->l10n = $l10n; } @@ -54,9 +57,9 @@ class PageController extends Controller { * @NoAdminRequired */ public function settings() { - $showAll = $this->api->getUserValue('showAll'); - $compact = $this->api->getUserValue('compact'); - $language = $this->api->getTrans()->findLanguage(); + $showAll = $this->settings->getUserValue('showAll'); + $compact = $this->settings->getUserValue('compact'); + $language = $this->l10n->findLanguage(); $settings = array( 'showAll' => $showAll === '1', @@ -76,11 +79,11 @@ class PageController extends Controller { $isCompact = $this->params('compact', null); if($isShowAll !== null) { - $this->api->setUserValue('showAll', $isShowAll); + $this->settings->setUserValue('showAll', $isShowAll); } if($isCompact !== null) { - $this->api->setUserValue('compact', $isCompact); + $this->settings->setUserValue('compact', $isCompact); } return new JSONResponse(); diff --git a/core/api.php b/core/api.php deleted file mode 100644 index 96b534a4c..000000000 --- a/core/api.php +++ /dev/null @@ -1,546 +0,0 @@ -. - * - */ - - -namespace OCA\News\Core; - - -/** - * This is used to wrap the owncloud static api calls into an object to make the - * code better abstractable for use in the dependency injection container - * - * Should you find yourself in need for more methods, simply inherit from this - * class and add your methods - */ -class API { - - private $appName; - - /** - * constructor - * @param string $appName the name of your application - */ - public function __construct($appName){ - $this->appName = $appName; - } - - - /** - * used to return the appname of the set application - * @return string the name of your application - */ - public function getAppName(){ - return $this->appName; - } - - - /** - * Creates a new navigation entry - * @param array $entry containing: id, name, order, icon and href key - */ - public function addNavigationEntry(array $entry){ - \OCP\App::addNavigationEntry($entry); - } - - - /** - * Gets the userid of the current user - * @return string the user id of the current user - */ - public function getUserId(){ - return \OCP\User::getUser(); - } - - - /** - * Sets the current navigation entry to the currently running app - */ - public function activateNavigationEntry(){ - \OCP\App::setActiveNavigationEntry($this->appName); - } - - - /** - * Adds a new javascript file - * @param string $scriptName the name of the javascript in js/ without the suffix - * @param string $appName the name of the app, defaults to the current one - */ - public function addScript($scriptName, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - \OCP\Util::addScript($appName, $scriptName); - } - - - /** - * Adds a new css file - * @param string $styleName the name of the css file in css/without the suffix - * @param string $appName the name of the app, defaults to the current one - */ - public function addStyle($styleName, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - \OCP\Util::addStyle($appName, $styleName); - } - - - /** - * shorthand for addScript for files in the 3rdparty directory - * @param string $name the name of the file without the suffix - */ - public function add3rdPartyScript($name){ - \OCP\Util::addScript($this->appName . '/3rdparty', $name); - } - - - /** - * shorthand for addStyle for files in the 3rdparty directory - * @param string $name the name of the file without the suffix - */ - public function add3rdPartyStyle($name){ - \OCP\Util::addStyle($this->appName . '/3rdparty', $name); - } - - /** - * Looks up a systemwide defined value - * @param string $key the key of the value, under which it was saved - * @return string the saved value - */ - public function getSystemValue($key){ - return \OCP\Config::getSystemValue($key, ''); - } - - - /** - * Sets a new systemwide value - * @param string $key the key of the value, under which will be saved - * @param string $value the value that should be stored - */ - public function setSystemValue($key, $value){ - return \OCP\Config::setSystemValue($key, $value); - } - - - /** - * Looks up an appwide defined value - * @param string $key the key of the value, under which it was saved - * @return string the saved value - */ - public function getAppValue($key, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Config::getAppValue($appName, $key, ''); - } - - - /** - * Writes a new appwide value - * @param string $key the key of the value, under which will be saved - * @param string $value the value that should be stored - */ - public function setAppValue($key, $value, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Config::setAppValue($appName, $key, $value); - } - - - - /** - * Shortcut for setting a user defined value - * @param string $key the key under which the value is being stored - * @param string $value the value that you want to store - * @param string $userId the userId of the user that we want to store the value under, defaults to the current one - */ - public function setUserValue($key, $value, $userId=null){ - if($userId === null){ - $userId = $this->getUserId(); - } - \OCP\Config::setUserValue($userId, $this->appName, $key, $value); - } - - - /** - * Shortcut for getting a user defined value - * @param string $key the key under which the value is being stored - * @param string $userId the userId of the user that we want to store the value under, defaults to the current one - */ - public function getUserValue($key, $userId=null){ - if($userId === null){ - $userId = $this->getUserId(); - } - return \OCP\Config::getUserValue($userId, $this->appName, $key); - } - - - /** - * Returns the translation object - * @return \OC_L10N the translation object - */ - public function getTrans(){ - # TODO: use public api - return \OC_L10N::get($this->appName); - } - - - /** - * Used to abstract the owncloud database access away - * @param string $sql the sql query with ? placeholder for params - * @param int $limit the maximum number of rows - * @param int $offset from which row we want to start - * @return \OCP\DB a query object - */ - public function prepareQuery($sql, $limit=null, $offset=null){ - return \OCP\DB::prepare($sql, $limit, $offset); - } - - - /** - * Used to get the id of the just inserted element - * @param string $tableName the name of the table where we inserted the item - * @return int the id of the inserted element - */ - public function getInsertId($tableName){ - return \OCP\DB::insertid($tableName); - } - - - /** - * Returns the URL for a route - * @param string $routeName the name of the route - * @param array $arguments an array with arguments which will be filled into the url - * @return string the url - */ - public function linkToRoute($routeName, $arguments=array()){ - return \OCP\Util::linkToRoute($routeName, $arguments); - } - - - /** - * Returns an URL for an image or file - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - */ - public function linkTo($file, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Util::linkTo($appName, $file); - } - - - /** - * Returns the link to an image, like link to but only with prepending img/ - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - */ - public function imagePath($file, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Util::imagePath($appName, $file); - } - - - /** - * Makes an URL absolute - * @param string $url the url - * @return string the absolute url - */ - public function getAbsoluteURL($url){ - # TODO: use public api - return \OC_Helper::makeURLAbsolute($url); - } - - - /** - * links to a file - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - * @deprecated replaced with linkToRoute() - * @return string the url - */ - public function linkToAbsolute($file, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Util::linkToAbsolute($appName, $file); - } - - - /** - * Checks if the current user is logged in - * @return bool true if logged in - */ - public function isLoggedIn(){ - return \OCP\User::isLoggedIn(); - } - - - /** - * Checks if a user is an admin - * @param string $userId the id of the user - * @return bool true if admin - */ - public function isAdminUser($userId){ - # TODO: use public api - return \OC_User::isAdminUser($userId); - } - - - /** - * Checks if a user is an subadmin - * @param string $userId the id of the user - * @return bool true if subadmin - */ - public function isSubAdminUser($userId){ - # TODO: use public api - return \OC_SubAdmin::isSubAdmin($userId); - } - - - /** - * Checks if the CSRF check was correct - * @return bool true if CSRF check passed - */ - public function passesCSRFCheck(){ - # TODO: use public api - return \OC_Util::isCallRegistered(); - } - - - /** - * Checks if an app is enabled - * @param string $appName the name of an app - * @return bool true if app is enabled - */ - public function isAppEnabled($appName){ - return \OCP\App::isEnabled($appName); - } - - - /** - * Writes a function into the error log - * @param string $msg the error message to be logged - * @param int $level the error level - */ - public function log($msg, $level=null){ - switch($level){ - case 'debug': - $level = \OCP\Util::DEBUG; - break; - case 'info': - $level = \OCP\Util::INFO; - break; - case 'warn': - $level = \OCP\Util::WARN; - break; - case 'fatal': - $level = \OCP\Util::FATAL; - break; - default: - $level = \OCP\Util::ERROR; - break; - } - \OCP\Util::writeLog($this->appName, $msg, $level); - } - - - /** - * Returns a template - * @param string $templateName the name of the template - * @param string $renderAs how it should be rendered - * @param string $appName the name of the app - * @return \OCP\Template a new template - */ - public function getTemplate($templateName, $renderAs='user', $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - - if($renderAs === 'blank'){ - return new \OCP\Template($appName, $templateName); - } else { - return new \OCP\Template($appName, $templateName, $renderAs); - } - } - - - /** - * turns an owncloud path into a path on the filesystem - * @param string path the path to the file on the oc filesystem - * @return string the filepath in the filesystem - */ - public function getLocalFilePath($path){ - # TODO: use public api - return \OC_Filesystem::getLocalFile($path); - } - - - /** - * used to return and open a new eventsource - * @return \OC_EventSource a new open EventSource class - */ - public function openEventSource(){ - # TODO: use public api - return new \OC_EventSource(); - } - - /** - * @brief connects a function to a hook - * @param string $signalClass class name of emitter - * @param string $signalName name of signal - * @param string $slotCl