From becce6b7520912257c3d72697a3aefec9923a467 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Thu, 29 Nov 2018 20:59:46 +0100 Subject: Define an official codestyle and adhere to it. --- lib/Controller/AdminController.php | 76 ++++++++++++++++-------------- lib/Controller/ApiController.php | 10 ++-- lib/Controller/EntityApiSerializer.php | 20 ++++---- lib/Controller/ExportController.php | 13 +++--- lib/Controller/FeedApiController.php | 48 +++++++++---------- lib/Controller/FeedController.php | 73 ++++++++++++++++------------- lib/Controller/FolderApiController.php | 30 ++++++------ lib/Controller/FolderController.php | 50 +++++++++----------- lib/Controller/ItemApiController.php | 80 +++++++++++++++++++------------- lib/Controller/ItemController.php | 82 +++++++++++++++++++++------------ lib/Controller/JSONHttpError.php | 7 +-- lib/Controller/PageController.php | 61 +++++++++++++++--------- lib/Controller/UserApiController.php | 6 +-- lib/Controller/UtilityApiController.php | 14 +++--- 14 files changed, 312 insertions(+), 258 deletions(-) (limited to 'lib/Controller') diff --git a/lib/Controller/AdminController.php b/lib/Controller/AdminController.php index 7feff907b..3ce09a0d1 100644 --- a/lib/Controller/AdminController.php +++ b/lib/Controller/AdminController.php @@ -28,9 +28,9 @@ use OCA\News\Service\ItemService; */ class AdminController extends Controller { - private $_config; - private $_configPath; - private $_itemService; + private $config; + private $configPath; + private $itemService; /** * AdminController constructor. @@ -41,13 +41,17 @@ class AdminController extends Controller * @param ItemService $itemService Service for items * @param string $configFile Path to the config */ - public function __construct($appName, IRequest $request, Config $config, - ItemService $itemService, $configFile + public function __construct( + $appName, + IRequest $request, + Config $config, + ItemService $itemService, + $configFile ) { parent::__construct($appName, $request); - $this->_config = $config; - $this->_configPath = $configFile; - $this->_itemService = $itemService; + $this->config = $config; + $this->configPath = $configFile; + $this->itemService = $itemService; } /** @@ -62,13 +66,13 @@ class AdminController extends Controller { $data = [ 'autoPurgeMinimumInterval' => - $this->_config->getAutoPurgeMinimumInterval(), - 'autoPurgeCount' => $this->_config->getAutoPurgeCount(), - 'maxRedirects' => $this->_config->getMaxRedirects(), - 'feedFetcherTimeout' => $this->_config->getFeedFetcherTimeout(), - 'useCronUpdates' => $this->_config->getUseCronUpdates(), - 'maxSize' => $this->_config->getMaxSize(), - 'exploreUrl' => $this->_config->getExploreUrl(), + $this->config->getAutoPurgeMinimumInterval(), + 'autoPurgeCount' => $this->config->getAutoPurgeCount(), + 'maxRedirects' => $this->config->getMaxRedirects(), + 'feedFetcherTimeout' => $this->config->getFeedFetcherTimeout(), + 'useCronUpdates' => $this->config->getUseCronUpdates(), + 'maxSize' => $this->config->getMaxSize(), + 'exploreUrl' => $this->config->getExploreUrl(), ]; return new TemplateResponse($this->appName, 'admin', $data, 'blank'); } @@ -87,29 +91,33 @@ class AdminController extends Controller * * @return array with the updated values */ - public function update($autoPurgeMinimumInterval, $autoPurgeCount, - $maxRedirects, $feedFetcherTimeout, $maxSize, - $useCronUpdates, $exploreUrl + public function update( + $autoPurgeMinimumInterval, + $autoPurgeCount, + $maxRedirects, + $feedFetcherTimeout, + $maxSize, + $useCronUpdates, + $exploreUrl ) { - $this->_config->setAutoPurgeMinimumInterval($autoPurgeMinimumInterval); - $this->_config->setAutoPurgeCount($autoPurgeCount); - $this->_config->setMaxRedirects($maxRedirects); - $this->_config->setMaxSize($maxSize); - $this->_config->setFeedFetcherTimeout($feedFetcherTimeout); - $this->_config->setUseCronUpdates($useCronUpdates); - $this->_config->setExploreUrl($exploreUrl); - $this->_config->write($this->_configPath); + $this->config->setAutoPurgeMinimumInterval($autoPurgeMinimumInterval); + $this->config->setAutoPurgeCount($autoPurgeCount); + $this->config->setMaxRedirects($maxRedirects); + $this->config->setMaxSize($maxSize); + $this->config->setFeedFetcherTimeout($feedFetcherTimeout); + $this->config->setUseCronUpdates($useCronUpdates); + $this->config->setExploreUrl($exploreUrl); + $this->config->write($this->configPath); return [ 'autoPurgeMinimumInterval' => - $this->_config->getAutoPurgeMinimumInterval(), - 'autoPurgeCount' => $this->_config->getAutoPurgeCount(), - 'maxRedirects' => $this->_config->getMaxRedirects(), - 'maxSize' => $this->_config->getMaxSize(), - 'feedFetcherTimeout' => $this->_config->getFeedFetcherTimeout(), - 'useCronUpdates' => $this->_config->getUseCronUpdates(), - 'exploreUrl' => $this->_config->getExploreUrl(), + $this->config->getAutoPurgeMinimumInterval(), + 'autoPurgeCount' => $this->config->getAutoPurgeCount(), + 'maxRedirects' => $this->config->getMaxRedirects(), + 'maxSize' => $this->config->getMaxSize(), + 'feedFetcherTimeout' => $this->config->getFeedFetcherTimeout(), + 'useCronUpdates' => $this->config->getUseCronUpdates(), + 'exploreUrl' => $this->config->getExploreUrl(), ]; } - } diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index d2a787a28..68caf9236 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -40,7 +40,8 @@ class ApiController extends BaseApiController * @param IRequest $request The request * @param IUserSession $userSession The user session */ - public function __construct($appName, IRequest $request, IUserSession $userSession) { + public function __construct($appName, IRequest $request, IUserSession $userSession) + { parent::__construct($appName, $request); $this->userSession = $userSession; } @@ -48,14 +49,16 @@ class ApiController extends BaseApiController /** * @return IUser */ - protected function getUser() { + protected function getUser() + { return $this->userSession->getUser(); } /** * @return string */ - protected function getUserId() { + protected function getUserId() + { return $this->getUser()->getUID(); } @@ -74,5 +77,4 @@ class ApiController extends BaseApiController 'apiLevels' => ['v1-2'] ]; } - } diff --git a/lib/Controller/EntityApiSerializer.php b/lib/Controller/EntityApiSerializer.php index f624eb84c..78a9b1031 100644 --- a/lib/Controller/EntityApiSerializer.php +++ b/lib/Controller/EntityApiSerializer.php @@ -13,13 +13,12 @@ namespace OCA\News\Controller; use \OCA\News\Db\IAPI; - class EntityApiSerializer { private $level; - public function __construct($level) + public function __construct($level) { $this->level = $level; } @@ -35,16 +34,16 @@ class EntityApiSerializer * * Response * @return array|mixed */ - public function serialize($data) + public function serialize($data) { - if($data instanceof IAPI) { + if ($data instanceof IAPI) { return [$this->level => [$data->toAPI()]]; } - if(is_array($data) && array_key_exists($this->level, $data)) { + if (is_array($data) && array_key_exists($this->level, $data)) { $data[$this->level] = $this->convert($data[$this->level]); - } elseif(is_array($data)) { + } elseif (is_array($data)) { $data = [$this->level => $this->convert($data)]; } @@ -52,12 +51,12 @@ class EntityApiSerializer } - private function convert($entities) + private function convert($entities) { $converted = []; - foreach($entities as $entity) { - if($entity instanceof IAPI) { + foreach ($entities as $entity) { + if ($entity instanceof IAPI) { $converted[] = $entity->toAPI(); // break if it contains anything else than entities @@ -68,5 +67,4 @@ class EntityApiSerializer return $converted; } - -} \ No newline at end of file +} diff --git a/lib/Controller/ExportController.php b/lib/Controller/ExportController.php index 07ce06f45..16bdfbd4e 100644 --- a/lib/Controller/ExportController.php +++ b/lib/Controller/ExportController.php @@ -33,7 +33,8 @@ class ExportController extends Controller private $itemService; private $userId; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, FolderService $folderService, FeedService $feedService, @@ -60,7 +61,7 @@ class ExportController extends Controller $folders = $this->folderService->findAll($this->userId); $opml = $this->opmlExporter->build($folders, $feeds)->saveXML(); $date = date('Y-m-d'); - $name = "subscriptions-".$date.".opml"; + $name = "subscriptions-" . $date . ".opml"; $mimeType = 'text/xml'; return new TextDownloadResponse($opml, $name, $mimeType); } @@ -77,12 +78,12 @@ class ExportController extends Controller // build assoc array for fast access $feedsDict = []; - foreach($feeds as $feed) { + foreach ($feeds as $feed) { $feedsDict['feed' . $feed->getId()] = $feed; } $articles = []; - foreach($items as $item) { + foreach ($items as $item) { $articles[] = $item->toExport($feedsDict); } @@ -93,6 +94,4 @@ class ExportController extends Controller ); return $response; } - - -} \ No newline at end of file +} diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php index 2e4a85eef..160a8bcd6 100644 --- a/lib/Controller/FeedApiController.php +++ b/lib/Controller/FeedApiController.php @@ -25,10 +25,8 @@ use \OCA\News\Service\ItemService; use \OCA\News\Service\ServiceNotFoundException; use \OCA\News\Service\ServiceConflictException; - class FeedApiController extends ApiController { - use JSONHttpError; private $itemService; @@ -37,7 +35,8 @@ class FeedApiController extends ApiController private $loggerParams; private $serializer; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, IUserSession $userSession, FeedService $feedService, @@ -59,7 +58,7 @@ class FeedApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function index() + public function index() { $result = [ @@ -73,7 +72,7 @@ class FeedApiController extends ApiController $this->itemService->getNewestItemId($this->getUserId()); // in case there are no items, ignore - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { } return $this->serializer->serialize($result); @@ -89,7 +88,7 @@ class FeedApiController extends ApiController * @param int $folderId * @return array|mixed|\OCP\AppFramework\Http\JSONResponse */ - public function create($url, $folderId=0) + public function create($url, $folderId = 0) { try { $this->feedService->purgeDeleted($this->getUserId(), false); @@ -102,14 +101,13 @@ class FeedApiController extends ApiController $this->itemService->getNewestItemId($this->getUserId()); // in case there are no items, ignore - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { } return $this->serializer->serialize($result); - - } catch(ServiceConflictException $ex) { + } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } } @@ -123,11 +121,11 @@ class FeedApiController extends ApiController * @param int $feedId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function delete($feedId) + public function delete($feedId) { try { $this->feedService->delete($feedId, $this->getUserId()); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -143,7 +141,7 @@ class FeedApiController extends ApiController * @param int $feedId * @param int $newestItemId */ - public function read($feedId, $newestItemId) + public function read($feedId, $newestItemId) { $this->itemService->readFeed($feedId, $newestItemId, $this->getUserId()); } @@ -158,13 +156,15 @@ class FeedApiController extends ApiController * @param int $folderId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function move($feedId, $folderId) + public function move($feedId, $folderId) { try { $this->feedService->patch( - $feedId, $this->getUserId(), ['folderId' => $folderId] + $feedId, + $this->getUserId(), + ['folderId' => $folderId] ); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -181,13 +181,15 @@ class FeedApiController extends ApiController * @param string $feedTitle * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function rename($feedId, $feedTitle) + public function rename($feedId, $feedTitle) { try { $this->feedService->patch( - $feedId, $this->getUserId(), ['title' => $feedTitle] + $feedId, + $this->getUserId(), + ['title' => $feedTitle] ); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -199,7 +201,7 @@ class FeedApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function fromAllUsers() + public function fromAllUsers() { $feeds = $this->feedService->findAllFromAllUsers(); $result = ['feeds' => []]; @@ -221,18 +223,16 @@ class FeedApiController extends ApiController * @param string $userId * @param int $feedId */ - public function update($userId, $feedId) + public function update($userId, $feedId) { try { $this->feedService->update($feedId, $userId); // ignore update failure - } catch(\Exception $ex) { + } catch (\Exception $ex) { $this->logger->debug( 'Could not update feed ' . $ex->getMessage(), $this->loggerParams ); } } - - } diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index cf323b58e..dbb36bb02 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -25,10 +25,8 @@ use OCA\News\Service\ServiceNotFoundException; use OCA\News\Service\ServiceConflictException; use OCA\News\Db\FeedType; - class FeedController extends Controller { - use JSONHttpError; private $feedService; @@ -37,7 +35,8 @@ class FeedController extends Controller private $userId; private $settings; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, FolderService $folderService, FeedService $feedService, @@ -88,32 +87,32 @@ class FeedController extends Controller { $feedId = (int) $this->settings->getUserValue( $this->userId, - $this->appName, 'lastViewedFeedId' + $this->appName, + 'lastViewedFeedId' ); $feedType = $this->settings->getUserValue( - $this->userId, $this->appName, + $this->userId, + $this->appName, 'lastViewedFeedType' ); // cast from null to int is 0 - if($feedType !== null) { + if ($feedType !== null) { $feedType = (int) $feedType; } // check if feed or folder exists try { - if($feedType === FeedType::FOLDER) { + if ($feedType === FeedType::FOLDER) { $this->folderService->find($feedId, $this->userId); - } elseif ($feedType === FeedType::FEED) { $this->feedService->find($feedId, $this->userId); // if its the first launch, those values will be null - } elseif($feedType === null) { + } elseif ($feedType === null) { throw new ServiceNotFoundException(''); } - - } catch (ServiceNotFoundException $ex){ + } catch (ServiceNotFoundException $ex) { $feedId = 0; $feedType = FeedType::SUBSCRIPTIONS; } @@ -137,8 +136,12 @@ class FeedController extends Controller * @param string $password * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function create($url, $parentFolderId, $title=null, - $user=null, $password=null + public function create( + $url, + $parentFolderId, + $title = null, + $user = null, + $password = null ) { try { // we need to purge deleted feeds if a feed is created to @@ -146,9 +149,12 @@ class FeedController extends Controller $this->feedService->purgeDeleted($this->userId, false); $feed = $this->feedService->create( - $url, $parentFolderId, - $this->userId, $title, - $user, $password + $url, + $parentFolderId, + $this->userId, + $title, + $user, + $password ); $params = ['feeds' => [$feed]]; @@ -162,13 +168,11 @@ class FeedController extends Controller } return $params; - - } catch(ServiceConflictException $ex) { + } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); } - } @@ -182,7 +186,7 @@ class FeedController extends Controller { try { $this->feedService->markDeleted($feedId, $this->userId); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -211,11 +215,9 @@ class FeedController extends Controller ] ] ]; - - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } - } @@ -225,7 +227,7 @@ class FeedController extends Controller * @param array $json * @return array */ - public function import($json) + public function import($json) { $feed = $this->feedService->importArticles($json, $this->userId); @@ -233,7 +235,7 @@ class FeedController extends Controller 'starred' => $this->itemService->starredCount($this->userId) ]; - if($feed) { + if ($feed) { $params['feeds'] = [$feed]; } @@ -273,7 +275,7 @@ class FeedController extends Controller { try { $this->feedService->unmarkDeleted($feedId, $this->userId); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -291,9 +293,14 @@ class FeedController extends Controller * @param int $folderId * @param string $title */ - public function patch($feedId, $pinned=null, $fullTextEnabled=null, - $updateMode=null, $ordering=null, $title=null, - $folderId=null + public function patch( + $feedId, + $pinned = null, + $fullTextEnabled = null, + $updateMode = null, + $ordering = null, + $title = null, + $folderId = null ) { $attributes = [ 'pinned' => $pinned, @@ -305,18 +312,18 @@ class FeedController extends Controller ]; $diff = array_filter( - $attributes, function ($value) { + $attributes, + function ($value) { return $value !== null; } ); try { $this->feedService->patch($feedId, $this->userId, $diff); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } return []; } - } diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php index 348fefda6..eb98b8107 100644 --- a/lib/Controller/FolderApiController.php +++ b/lib/Controller/FolderApiController.php @@ -25,17 +25,16 @@ use \OCA\News\Service\ServiceNotFoundException; use \OCA\News\Service\ServiceConflictException; use \OCA\News\Service\ServiceValidationException; - class FolderApiController extends ApiController { - use JSONHttpError; private $folderService; private $itemService; private $serializer; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, IUserSession $userSession, FolderService $folderService, @@ -53,7 +52,7 @@ class FolderApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function index() + public function index() { return $this->serializer->serialize( $this->folderService->findAll($this->getUserId()) @@ -69,16 +68,16 @@ class FolderApiController extends ApiController * @param string $name * @return array|mixed|\OCP\AppFramework\Http\JSONResponse */ - public function create($name) + public function create($name) { try { $this->folderService->purgeDeleted($this->getUserId(), false); return $this->serializer->serialize( $this->folderService->create($name, $this->getUserId()) ); - } catch(ServiceValidationException $ex) { + } catch (ServiceValidationException $ex) { return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); - } catch(ServiceConflictException $ex) { + } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); } } @@ -92,11 +91,11 @@ class FolderApiController extends ApiController * @param int $folderId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function delete($folderId) + public function delete($folderId) { try { $this->folderService->delete($folderId, $this->getUserId()); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -112,16 +111,15 @@ class FolderApiController extends ApiController * @param string $name * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function update($folderId, $name) + public function update($folderId, $name) { try { $this->folderService->rename($folderId, $name, $this->getUserId()); - - } catch(ServiceValidationException $ex) { + } catch (ServiceValidationException $ex) { return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); - } catch(ServiceConflictException $ex) { + } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -137,10 +135,8 @@ class FolderApiController extends ApiController * @param int $folderId * @param int $newestItemId */ - public function read($folderId, $newestItemId) + public function read($folderId, $newestItemId) { $this->itemService->readFolder($folderId, $newestItemId, $this->getUserId()); } - - } diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index 8b4dadcca..d3089178d 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -24,10 +24,8 @@ use \OCA\News\Service\ServiceNotFoundException; use \OCA\News\Service\ServiceConflictException; use \OCA\News\Service\ServiceValidationException; - class FolderController extends Controller { - use JSONHttpError; private $folderService; @@ -35,7 +33,8 @@ class FolderController extends Controller private $itemService; private $userId; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, FolderService $folderService, FeedService $feedService, @@ -53,7 +52,7 @@ class FolderController extends Controller /** * @NoAdminRequired */ - public function index() + public function index() { $folders = $this->folderService->findAll($this->userId); return ['folders' => $folders]; @@ -67,11 +66,11 @@ class FolderController extends Controller * @param bool $open * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function open($folderId, $open) + public function open($folderId, $open) { try { $this->folderService->open($folderId, $open, $this->userId); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -85,7 +84,7 @@ class FolderController extends Controller * @param string $folderName * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function create($folderName) + public function create($folderName) { try { // we need to purge deleted folders if a folder is created to @@ -94,13 +93,11 @@ class FolderController extends Controller $folder = $this->folderService->create($folderName, $this->userId); return ['folders' => [$folder]]; - - } catch(ServiceConflictException $ex) { + } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); - } catch(ServiceValidationException $ex) { + } catch (ServiceValidationException $ex) { return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); } - } @@ -110,11 +107,11 @@ class FolderController extends Controller * @param int $folderId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function delete($folderId) + public function delete($folderId) { try { $this->folderService->markDeleted($folderId, $this->userId); - } catch (ServiceNotFoundException $ex){ + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -129,24 +126,23 @@ class FolderController extends Controller * @param int $folderId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function rename($folderName, $folderId) + public function rename($folderName, $folderId) { try { $folder = $this->folderService->rename( - $folderId, $folderName, + $folderId, + $folderName, $this->userId ); return ['folders' => [$folder]]; - - } catch(ServiceConflictException $ex) { + } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); - } catch(ServiceValidationException $ex) { + } catch (ServiceValidationException $ex) { return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); - } catch (ServiceNotFoundException $ex){ + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } - } /** @@ -156,10 +152,12 @@ class FolderController extends Controller * @param int $highestItemId * @return array */ - public function read($folderId, $highestItemId) + public function read($folderId, $highestItemId) { $this->itemService->readFolder( - $folderId, $highestItemId, $this->userId + $folderId, + $highestItemId, + $this->userId ); return ['feeds' => $this->feedService->findAll($this->userId)]; @@ -172,16 +170,14 @@ class FolderController extends Controller * @param int $folderId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function restore($folderId) + public function restore($folderId) { try { $this->folderService->unmarkDeleted($folderId, $this->userId); - } catch (ServiceNotFoundException $ex){ + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } return []; } - - -} \ No newline at end of file +} diff --git a/lib/Controller/ItemApiController.php b/lib/Controller/ItemApiController.php index 601a3b409..cf4c7c730 100644 --- a/lib/Controller/ItemApiController.php +++ b/lib/Controller/ItemApiController.php @@ -24,13 +24,13 @@ use \OCA\News\Service\ServiceNotFoundException; class ItemApiController extends ApiController { - use JSONHttpError; private $itemService; private $serializer; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, IUserSession $userSession, ItemService $itemService @@ -54,12 +54,22 @@ class ItemApiController extends ApiController * @param bool $oldestFirst * @return array|mixed */ - public function index($type=3, $id=0, $getRead=true, $batchSize=-1, - $offset=0, $oldestFirst=false + public function index( + $type = 3, + $id = 0, + $getRead = true, + $batchSize = -1, + $offset = 0, + $oldestFirst = false ) { return $this->serializer->serialize( $this->itemService->findAll( - $id, $type, $batchSize, $offset, $getRead, $oldestFirst, + $id, + $type, + $batchSize, + $offset, + $getRead, + $oldestFirst, $this->getUserId() ) ); @@ -76,7 +86,7 @@ class ItemApiController extends ApiController * @param int $lastModified * @return array|mixed */ - public function updated($type=3, $id=0, $lastModified=0) + public function updated($type = 3, $id = 0, $lastModified = 0) { // needs to be turned into a millisecond timestamp to work properly if (strlen((string) $lastModified) <= 10) { @@ -86,18 +96,21 @@ class ItemApiController extends ApiController } return $this->serializer->serialize( $this->itemService->findAllNew( - $id, $type, $paddedLastModified, - true, $this->getUserId() + $id, + $type, + $paddedLastModified, + true, + $this->getUserId() ) ); } - private function setRead($isRead, $itemId) + private function setRead($isRead, $itemId) { try { $this->itemService->read($itemId, $isRead, $this->getUserId()); - } catch(ServiceNotFoundException $ex){ + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -113,7 +126,7 @@ class ItemApiController extends ApiController * @param int $itemId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function read($itemId) + public function read($itemId) { return $this->setRead(true, $itemId); } @@ -127,19 +140,22 @@ class ItemApiController extends ApiController * @param int $itemId * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function unread($itemId) + public function unread($itemId) { return $this->setRead(false, $itemId); } - private function setStarred($isStarred, $feedId, $guidHash) + private function setStarred($isStarred, $feedId, $guidHash) { try { $this->itemService->star( - $feedId, $guidHash, $isStarred, $this->getUserId() + $feedId, + $guidHash, + $isStarred, + $this->getUserId() ); - } catch(ServiceNotFoundException $ex){ + } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -156,7 +172,7 @@ class ItemApiController extends ApiController * @param string $guidHash * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function star($feedId, $guidHash) + public function star($feedId, $guidHash) { return $this->setStarred(true, $feedId, $guidHash); } @@ -171,7 +187,7 @@ class ItemApiController extends ApiController * @param string $guidHash * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function unstar($feedId, $guidHash) + public function unstar($feedId, $guidHash) { return $this->setStarred(false, $feedId, $guidHash); } @@ -184,18 +200,18 @@ class ItemApiController extends ApiController * * @param int $newestItemId */ - public function readAll($newestItemId) + public function readAll($newestItemId) { $this->itemService->readAll($newestItemId, $this->getUserId()); } - private function setMultipleRead($isRead, $items) + private function setMultipleRead($isRead, $items) { - foreach($items as $id) { + foreach ($items as $id) { try { $this->itemService->read($id, $isRead, $this->getUserId()); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { continue; } } @@ -209,7 +225,7 @@ class ItemApiController extends ApiController * * @param int[] item ids */ - public function readMultiple($items) + public function readMultiple($items) { $this->setMultipleRead(true, $items); } @@ -222,21 +238,23 @@ class ItemApiController extends ApiController * * @param int[] item ids */ - public function unreadMultiple($items) + public function unreadMultiple($items) { $this->setMultipleRead(false, $items); } - private function setMultipleStarred($isStarred, $items) + private function setMultipleStarred($isStarred, $items) { - foreach($items as $item) { + foreach ($items as $item) { try { $this->itemService->star( - $item['feedId'], $item['guidHash'], - $isStarred, $this->getUserId() + $item['feedId'], + $item['guidHash'], + $isStarred, + $this->getUserId() ); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { continue; } } @@ -250,7 +268,7 @@ class ItemApiController extends ApiController * * @param int[] item ids */ - public function starMultiple($items) + public function starMultiple($items) { $this->setMultipleStarred(true, $items); } @@ -263,10 +281,8 @@ class ItemApiController extends ApiController * * @param int[] item ids */ - public function unstarMultiple($items) + public function unstarMultiple($items) { $this->setMultipleStarred(false, $items); } - - } diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index 9c0801b1d..156f4d1d4 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -23,10 +23,8 @@ use \OCA\News\Service\ServiceNotFoundException; use \OCA\News\Service\ItemService; use \OCA\News\Service\FeedService; - class ItemController extends Controller { - use JSONHttpError; private $itemService; @@ -34,7 +32,8 @@ class ItemController extends Controller private $userId; private $settings; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, FeedService $feedService, ItemService $itemService, @@ -61,31 +60,45 @@ class ItemController extends Controller * @param string $search * @return array */ - public function index($type=3, $id=0, $limit=50, $offset=0, $showAll=null, - $oldestFirst=null, $search='' + public function index( + $type = 3, + $id = 0, + $limit = 50, + $offset = 0, + $showAll = null, + $oldestFirst = null, + $search = '' ) { // in case this is called directly and not from the website use the // internal state if ($showAll === null) { $showAll = $this->settings->getUserValue( - $this->userId, $this->appName, 'showAll' + $this->userId, + $this->appName, + 'showAll' ) === '1'; } if ($oldestFirst === null) { $oldestFirst = $this->settings->getUserValue( - $this->userId, $this->appName, 'oldestFirst' + $this->userId, + $this->appName, + 'oldestFirst' ) === '1'; } $this->settings->setUserValue( - $this->userId, $this->appName, - 'lastViewedFeedId', $id + $this->userId, + $this->appName, + 'lastViewedFeedId', + $id ); $this->settings->setUserValue( - $this->userId, $this->appName, - 'lastViewedFeedType', $type + $this->userId, + $this->appName, + 'lastViewedFeedType', + $type ); $params = []; @@ -100,11 +113,10 @@ class ItemController extends Controller } try { - // the offset is 0 if the user clicks on a new feed // we need to pass the newest feeds to not let the unread count get // out of sync - if($offset === 0) { + if ($offset === 0) { $params['newestItemId'] = $this->itemService->getNewestItemId($this->userId); $params['feeds'] = $this->feedService->findAll($this->userId); @@ -113,13 +125,19 @@ class ItemController extends Controller } $params['items'] = $this->itemService->findAll( - $id, $type, $limit, $offset, $showAll, $oldestFirst, - $this->userId, $search + $id, + $type, + $limit, + $offset, + $showAll, + $oldestFirst, + $this->userId, + $search ); // this gets thrown if there are no items // in that case just return an empty array - } catch(ServiceException $ex) { + } catch (ServiceException $ex) { } return $params; @@ -134,10 +152,11 @@ class ItemController extends Controller * @param int $lastModified * @return array */ - public function newItems($type, $id, $lastModified=0) + public function newItems($type, $id, $lastModified = 0) { $showAll = $this->settings->getUserValue( - $this->userId, $this->appName, + $this->userId, + $this->appName, 'showAll' ) === '1'; @@ -150,13 +169,16 @@ class ItemController extends Controller $params['starred'] = $this->itemService->starredCount($this->userId); $params['items'] = $this->itemService->findAllNew( - $id, $type, - $lastModified, $showAll, $this->userId + $id, + $type, + $lastModified, + $showAll, + $this->userId ); // this gets thrown if there are no items // in that case just return an empty array - } catch(ServiceException $ex) { + } catch (ServiceException $ex) { } return $params; @@ -175,10 +197,12 @@ class ItemController extends Controller { try { $this->itemService->star( - $feedId, $guidHash, $isStarred, + $feedId, + $guidHash, + $isStarred, $this->userId ); - } catch(ServiceException $ex) { + } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -193,11 +217,11 @@ class ItemController extends Controller * @param bool $isRead * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function read($itemId, $isRead=true) + public function read($itemId, $isRead = true) { try { $this->itemService->read($itemId, $isRead, $this->userId); - } catch(ServiceException $ex) { + } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -223,16 +247,14 @@ class ItemController extends Controller * * @param int[] item ids */ - public function readMultiple($itemIds) + public function readMultiple($itemIds) { - foreach($itemIds as $id) { + foreach ($itemIds as $id) { try { $this->itemService->read($id, true, $this->userId); - } catch(ServiceNotFoundException $ex) { + } catch (ServiceNotFoundException $ex) { continue; } } } - - } diff --git a/lib/Controller/JSONHttpError.php b/lib/Controller/JSONHttpError.php index b79b77567..16d80f857 100644 --- a/lib/Controller/JSONHttpError.php +++ b/lib/Controller/JSONHttpError.php @@ -13,7 +13,6 @@ namespace OCA\News\Controller; use \OCP\AppFramework\Http\JSONResponse; - trait JSONHttpError { @@ -24,10 +23,8 @@ trait JSONHttpError * @param int $code the http error code * @return \OCP\AppFramework\Http\JSONResponse */ - public function error(\Exception $exception, $code) + public function error(\Exception $exception, $code) { return new JSONResponse(['message' => $exception->getMessage()], $code); } - - -} \ No newline at end of file +} diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 3d3bab76b..6ae01811d 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -31,6 +31,7 @@ use OCA\News\Db\FeedType; class PageController extends Controller { + use JSONHttpError; private $settings; private $l10n; @@ -38,11 +39,11 @@ class PageController extends Controller private $urlGenerator; private $config; private $recommendedSites; - private $statusService; - use JSONHttpError; + private $statusService; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, IConfig $settings, IURLGenerator $urlGenerator, @@ -67,20 +68,22 @@ class PageController extends Controller * @NoAdminRequired * @NoCSRFRequired */ - public function index() + public function index() { $status = $this->statusService->getStatus(); $response = new TemplateResponse( - $this->appName, 'index', [ - 'warnings' => $status['warnings'], - 'url_generator' => $this->urlGenerator + $this->appName, + 'index', + [ + 'warnings' => $status['warnings'], + 'url_generator' => $this->urlGenerator ] ); $csp = new ContentSecurityPolicy(); $csp->addAllowedImageDomain('*') ->addAllowedMediaDomain('*') - ->addAllowedConnectDomain('*') // chrome breaks on audio elements + ->addAllowedConnectDomain('*')// chrome breaks on audio elements ->addAllowedFrameDomain('https://youtube.com') ->addAllowedFrameDomain('https://www.youtube.com') ->addAllowedFrameDomain('https://player.vimeo.com') @@ -96,7 +99,7 @@ class PageController extends Controller /** * @NoAdminRequired */ - public function settings() + public function settings() { $settings = [ 'showAll', @@ -110,7 +113,8 @@ class PageController extends Controller if (trim($exploreUrl) === '') { // default url should not feature the sites.en.json $exploreUrl = $this->urlGenerator->linkToRoute( - 'news.page.explore', ['lang' => 'en'] + 'news.page.explore', + ['lang' => 'en'] ); $exploreUrl = preg_replace('/feeds\.en\.json$/', '', $exploreUrl); } @@ -122,7 +126,9 @@ class PageController extends Controller foreach ($settings as $setting) { $result[$setting] = $this->settings->getUserValue( - $this->userId, $this->appName, $setting + $this->userId, + $this->appName, + $setting ) === '1'; } return ['settings' => $result]; @@ -137,10 +143,15 @@ class PageController extends Controller * @param bool $preventReadOnScroll * @param bool $oldestFirst */ - public function updateSettings($showAll, $compact, $preventReadOnScroll, - $oldestFirst, $compactExpand + public function updateSettings( + $showAll, + $compact, + $preventReadOnScroll, + $oldestFirst, + $compactExpand ) { - $settings = ['showAll', + $settings = [ + 'showAll', 'compact', 'preventReadOnScroll', 'oldestFirst', @@ -154,8 +165,10 @@ class PageController extends Controller $value = '0'; } $this->settings->setUserValue( - $this->userId, $this->appName, - $setting, $value + $this->userId, + $this->appName, + $setting, + $value ); } } @@ -165,15 +178,19 @@ class PageController extends Controller * * @param string $lang */ - public function explore($lang) + public function explore($lang) { $this->settings->setUserValue( - $this->userId, $this->appName, - 'lastViewedFeedId', 0 + $this->userId, + $this->appName, + 'lastViewedFeedId', + 0 ); $this->settings->setUserValue( - $this->userId, $this->appName, - 'lastViewedFeedType', FeedType::EXPLORE + $this->userId, + $this->appName, + 'lastViewedFeedType', + FeedType::EXPLORE ); try { @@ -182,6 +199,4 @@ class PageController extends Controller return $this->error($ex, Http::STATUS_NOT_FOUND); } } - - } diff --git a/lib/Controller/UserApiController.php b/lib/Controller/UserApiController.php index 2e0b04b5c..1a5f34f70 100644 --- a/lib/Controller/UserApiController.php +++ b/lib/Controller/UserApiController.php @@ -27,7 +27,8 @@ class UserApiController extends ApiController private $userSession; private $rootFolder; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, IUserSession $userSession, IRootFolder $rootFolder @@ -41,7 +42,7 @@ class UserApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function index() + public function index() { $user = $this->getUser(); @@ -71,5 +72,4 @@ class UserApiController extends ApiController 'avatar' => $avatar ]; } - } diff --git a/lib/Controller/UtilityApiController.php b/lib/Controller/UtilityApiController.php index f88230c3b..ee9ca0900 100644 --- a/lib/Controller/UtilityApiController.php +++ b/lib/Controller/UtilityApiController.php @@ -23,7 +23,6 @@ use \OCP\AppFramework\Http; use \OCA\News\Utility\Updater; use \OCA\News\Service\StatusService; - class UtilityApiController extends ApiController { @@ -31,7 +30,8 @@ class UtilityApiController extends ApiController private $settings; private $statusService; - public function __construct($appName, + public function __construct( + $appName, IRequest $request, IUserSession $userSession, Updater $updater, @@ -50,7 +50,7 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function version() + public function version() { $version = $this->settings->getAppValue( $this->appName, @@ -64,7 +64,7 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function beforeUpdate() + public function beforeUpdate() { $this->updater->beforeUpdate(); } @@ -74,7 +74,7 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function afterUpdate() + public function afterUpdate() { $this->updater->afterUpdate(); } @@ -85,10 +85,8 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @NoAdminRequired */ - public function status() + public function status() { return $this->statusService->getStatus(); } - - } -- cgit v1.2.3