diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-04-19 13:20:54 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-04-19 13:21:11 +0200 |
commit | 517e4ca5435106ab5304849248cbea4e9dffd4b0 (patch) | |
tree | 0f00076f012791b39c710994f695abf723546062 | |
parent | bd35b98d2c130f058b182f726636ee971625823b (diff) |
split up api class for easier testing and clearer code
56 files changed, 802 insertions, 1432 deletions
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 $folderBusinessLay |