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/Service/FeedService.php | 86 +++++++++++--------- lib/Service/FolderService.php | 23 +++--- lib/Service/ItemService.php | 122 +++++++++++++++++++---------- lib/Service/Service.php | 6 +- lib/Service/ServiceConflictException.php | 4 +- lib/Service/ServiceException.php | 4 +- lib/Service/ServiceNotFoundException.php | 4 +- lib/Service/ServiceValidationException.php | 4 +- lib/Service/StatusService.php | 19 +++-- 9 files changed, 155 insertions(+), 117 deletions(-) (limited to 'lib/Service') diff --git a/lib/Service/FeedService.php b/lib/Service/FeedService.php index fe9058b8e..2ccbb014b 100644 --- a/lib/Service/FeedService.php +++ b/lib/Service/FeedService.php @@ -28,7 +28,6 @@ use OCA\News\Fetcher\FetcherException; use OCA\News\Config\Config; use OCA\News\Utility\Time; - class FeedService extends Service { @@ -42,7 +41,8 @@ class FeedService extends Service private $purifier; private $loggerParams; - public function __construct(FeedMapper $feedMapper, + public function __construct( + FeedMapper $feedMapper, Fetcher $feedFetcher, ItemMapper $itemMapper, ILogger $logger, @@ -82,7 +82,7 @@ class FeedService extends Service * * @return array of feeds */ - public function findAllFromAllUsers() + public function findAllFromAllUsers() { return $this->feedMapper->findAll(); } @@ -103,8 +103,13 @@ class FeedService extends Service * @throws ServiceNotFoundException if the url points to an invalid feed * @return Feed the newly created feed */ - public function create($feedUrl, $folderId, $userId, $title=null, - $basicAuthUser=null, $basicAuthPassword=null + public function create( + $feedUrl, + $folderId, + $userId, + $title = null, + $basicAuthUser = null, + $basicAuthPassword = null ) { // first try if the feed exists already try { @@ -113,8 +118,12 @@ class FeedService extends Service * @var Item[] $items */ list($feed, $items) = $this->feedFetcher->fetch( - $feedUrl, true, - null, null, false, $basicAuthUser, + $feedUrl, + true, + null, + null, + false, + $basicAuthUser, $basicAuthPassword ); @@ -126,7 +135,7 @@ class FeedService extends Service ); // If no matching feed was found everything was ok - } catch(DoesNotExistException $ex){ + } catch (DoesNotExistException $ex) { } // insert feed @@ -146,7 +155,7 @@ class FeedService extends Service // insert items in reverse order because the first one is usually // the newest item $unreadCount = 0; - for($i=$itemCount-1; $i>=0; $i--){ + for ($i = $itemCount - 1; $i >= 0; $i--) { $item = $items[$i]; $item->setFeedId($feed->getId()); @@ -154,10 +163,12 @@ class FeedService extends Service // and ignore it if it does try { $this->itemMapper->findByGuidHash( - $item->getGuidHash(), $item->getFeedId(), $userId + $item->getGuidHash(), + $item->getFeedId(), + $userId ); continue; - } catch(DoesNotExistException $ex){ + } catch (DoesNotExistException $ex) { $unreadCount += 1; $item->setBody($this->purifier->purify($item->getBody())); $this->itemMapper->insert($item); @@ -168,7 +179,7 @@ class FeedService extends Service $feed->setUnreadCount($unreadCount); return $feed; - } catch(FetcherException $ex){ + } catch (FetcherException $ex) { $this->logger->debug($ex->getMessage(), $this->loggerParams); throw new ServiceNotFoundException($ex->getMessage()); } @@ -182,10 +193,10 @@ class FeedService extends Service { // TODO: this method is not covered by any tests $feeds = $this->feedMapper->findAll(); - foreach($feeds as $feed){ + foreach ($feeds as $feed) { try { $this->update($feed->getId(), $feed->getUserId()); - } catch(\Exception $ex){ + } catch (\Exception $ex) { // something is really wrong here, log it $this->logger->error( 'Unexpected error when updating feed ' . $ex->getMessage(), @@ -205,12 +216,12 @@ class FeedService extends Service * @throws ServiceNotFoundException if the feed does not exist * @return Feed the updated feed entity */ - public function update($feedId, $userId, $forceUpdate=false) + public function update($feedId, $userId, $forceUpdate = false) { /** @var Feed $existingFeed */ $existingFeed = $this->find($feedId, $userId); - if($existingFeed->getPreventUpdate() === true) { + if ($existingFeed->getPreventUpdate() === true) { return $existingFeed; } @@ -257,20 +268,21 @@ class FeedService extends Service // insert items in reverse order because the first one is // usually the newest item - for($i=$itemCount-1; $i>=0; $i--){ + for ($i = $itemCount - 1; $i >= 0; $i--) { $item = $items[$i]; $item->setFeedId($existingFeed->getId()); try { $dbItem = $this->itemMapper->findByGuidHash( - $item->getGuidHash(), $feedId, $userId + $item->getGuidHash(), + $feedId, + $userId ); // in case of update - if ($forceUpdate + if ($forceUpdate || $item->getUpdatedDate() > $dbItem->getUpdatedDate() ) { - $dbItem->setTitle($item->getTitle()); $dbItem->setUrl($item->getUrl()); $dbItem->setAuthor($item->getAuthor()); @@ -292,7 +304,7 @@ class FeedService extends Service $this->itemMapper->update($dbItem); } - } catch(DoesNotExistException $ex){ + } catch (DoesNotExistException $ex) { $item->setBody( $this->purifier->purify($item->getBody()) ); @@ -303,10 +315,9 @@ class FeedService extends Service // mark feed as successfully updated $existingFeed->setUpdateErrorCount(0); $existingFeed->setLastUpdateError(''); - - } catch(FetcherException $ex){ + } catch (FetcherException $ex) { $existingFeed->setUpdateErrorCount( - $existingFeed->getUpdateErrorCount()+1 + $existingFeed->getUpdateErrorCount() + 1 ); $existingFeed->setLastUpdateError($ex->getMessage()); } @@ -323,7 +334,7 @@ class FeedService extends Service * @param string $userId the username * @return Feed if one had to be created for nonexistent feeds */ - public function importArticles($json, $userId) + public function importArticles($json, $userId) { $url = 'http://nextcloud/nofeed'; $urlHash = md5($url); @@ -331,7 +342,7 @@ class FeedService extends Service // build assoc array for fast access $feeds = $this->findAll($userId); $feedsDict = []; - foreach($feeds as $feed) { + foreach ($feeds as $feed) { $feedsDict[$feed->getLink()] = $feed; } @@ -343,10 +354,10 @@ class FeedService extends Service $item = Item::fromImport($entry); $feedLink = $entry['feedLink']; // this is not set on the item yet - if(array_key_exists($feedLink, $feedsDict)) { + if (array_key_exists($feedLink, $feedsDict)) { $feed = $feedsDict[$feedLink]; $item->setFeedId($feed->getId()); - } elseif(array_key_exists($url, $feedsDict)) { + } elseif (array_key_exists($url, $feedsDict)) { $feed = $feedsDict[$url]; $item->setFeedId($feed->getId()); } else { @@ -369,18 +380,20 @@ class FeedService extends Service try { // if item exists, copy the status $existingItem = $this->itemMapper->findByGuidHash( - $item->getGuidHash(), $feed->getId(), $userId + $item->getGuidHash(), + $feed->getId(), + $userId ); $existingItem->setStatus($item->getStatus()); $this->itemMapper->update($existingItem); - } catch(DoesNotExistException $ex){ + } catch (DoesNotExistException $ex) { $item->setBody($this->purifier->purify($item->getBody())); $item->generateSearchIndex(); $this->itemMapper->insert($item); } } - if($createdFeed) { + if ($createdFeed) { return $this->feedMapper->findByUrlHash($urlHash, $userId); } @@ -395,7 +408,7 @@ class FeedService extends Service * @param string $userId the name of the user for security reasons * @throws ServiceNotFoundException when feed does not exist */ - public function markDeleted($feedId, $userId) + public function markDeleted($feedId, $userId) { $feed = $this->find($feedId, $userId); $feed->setDeletedAt($this->timeFactory->getTime()); @@ -410,7 +423,7 @@ class FeedService extends Service * @param string $userId the name of the user for security reasons * @throws ServiceNotFoundException when feed does not exist */ - public function unmarkDeleted($feedId, $userId) + public function unmarkDeleted($feedId, $userId) { $feed = $this->find($feedId, $userId); $feed->setDeletedAt(0); @@ -426,7 +439,7 @@ class FeedService extends Service * entries in a given interval to give the user a chance to undo the * deletion */ - public function purgeDeleted($userId=null, $useInterval=true) + public function purgeDeleted($userId = null, $useInterval = true) { $deleteOlderThan = null; @@ -449,7 +462,7 @@ class FeedService extends Service * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser($userId) { $this->feedMapper->deleteUser($userId); } @@ -467,7 +480,7 @@ class FeedService extends Service * ] * @throws ServiceNotFoundException if feed does not exist */ - public function patch($feedId, $userId, $diff=[]) + public function patch($feedId, $userId, $diff = []) { $feed = $this->find($feedId, $userId); @@ -487,5 +500,4 @@ class FeedService extends Service return $this->feedMapper->update($feed); } - } diff --git a/lib/Service/FolderService.php b/lib/Service/FolderService.php index 75aa294e1..9ba6b35e2 100644 --- a/lib/Service/FolderService.php +++ b/lib/Service/FolderService.php @@ -19,7 +19,6 @@ use OCA\News\Db\FolderMapper; use OCA\News\Config\Config; use OCA\News\Utility\Time; - class FolderService extends Service { @@ -28,7 +27,8 @@ class FolderService extends Service private $autoPurgeMinimumInterval; private $folderMapper; - public function __construct(FolderMapper $folderMapper, + public function __construct( + FolderMapper $folderMapper, IL10N $l10n, Time $timeFactory, Config $config @@ -47,7 +47,7 @@ class FolderService extends Service * @param string $userId the name of the user * @return array of folders */ - public function findAll($userId) + public function findAll($userId) { return $this->folderMapper->findAllFromUser($userId); } @@ -57,14 +57,13 @@ class FolderService extends Service { $existingFolders = $this->folderMapper->findByName($folderName, $userId); - if(count($existingFolders) > 0) { - + if (count($existingFolders) > 0) { throw new ServiceConflictException( $this->l10n->t('Can not add folder: Exists already') ); } - if(mb_strlen($folderName) === 0) { + if (mb_strlen($folderName) === 0) { throw new ServiceValidationException( 'Folder name can not be empty' ); @@ -83,7 +82,7 @@ class FolderService extends Service * @throws ServiceValidationException if the folder has invalid parameters * @return Folder the newly created folder */ - public function create($folderName, $userId, $parentId=0) + public function create($folderName, $userId, $parentId = 0) { $this->validateFolder($folderName, $userId); @@ -135,7 +134,7 @@ class FolderService extends Service * @param string $userId the name of the user for security reasons * @throws ServiceNotFoundException when folder does not exist */ - public function markDeleted($folderId, $userId) + public function markDeleted($folderId, $userId) { $folder = $this->find($folderId, $userId); $folder->setDeletedAt($this->timeFactory->getTime()); @@ -150,7 +149,7 @@ class FolderService extends Service * @param string $userId the name of the user for security reasons * @throws ServiceNotFoundException when folder does not exist */ - public function unmarkDeleted($folderId, $userId) + public function unmarkDeleted($folderId, $userId) { $folder = $this->find($folderId, $userId); $folder->setDeletedAt(0); @@ -166,7 +165,7 @@ class FolderService extends Service * entries in a given interval to give the user a chance to undo the * deletion */ - public function purgeDeleted($userId=null, $useInterval=true) + public function purgeDeleted($userId = null, $useInterval = true) { $deleteOlderThan = null; @@ -188,10 +187,8 @@ class FolderService extends Service * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser($userId) { $this->folderMapper->deleteUser($userId); } - - } diff --git a/lib/Service/ItemService.php b/lib/Service/ItemService.php index bc108b034..c41217ff6 100644 --- a/lib/Service/ItemService.php +++ b/lib/Service/ItemService.php @@ -22,7 +22,6 @@ use OCA\News\Db\FeedType; use OCA\News\Config\Config; use OCA\News\Utility\Time; - class ItemService extends Service { @@ -31,7 +30,8 @@ class ItemService extends Service private $itemMapper; private $systemConfig; - public function __construct(ItemMapper $itemMapper, + public function __construct( + ItemMapper $itemMapper, Time $timeFactory, Config $config, IConfig $systemConfig @@ -58,19 +58,28 @@ class ItemService extends Service */ public function findAllNew($id, $type, $updatedSince, $showAll, $userId) { - switch($type){ - case FeedType::FEED: - return $this->itemMapper->findAllNewFeed( - $id, $updatedSince, $showAll, $userId - ); - case FeedType::FOLDER: - return $this->itemMapper->findAllNewFolder( - $id, $updatedSince, $showAll, $userId - ); - default: - return $this->itemMapper->findAllNew( - $updatedSince, $type, $showAll, $userId - ); + switch ($type) { + case FeedType::FEED: + return $this->itemMapper->findAllNewFeed( + $id, + $updatedSince, + $showAll, + $userId + ); + case FeedType::FOLDER: + return $this->itemMapper->findAllNewFolder( + $id, + $updatedSince, + $showAll, + $userId + ); + default: + return $this->itemMapper->findAllNew( + $updatedSince, + $type, + $showAll, + $userId + ); } } @@ -90,24 +99,47 @@ class ItemService extends Service * or body * @return array of items */ - public function findAll($id, $type, $limit, $offset, $showAll, $oldestFirst, - $userId, $search=[] + public function findAll( + $id, + $type, + $limit, + $offset, + $showAll, + $oldestFirst, + $userId, + $search = [] ) { - switch($type){ - case FeedType::FEED: - return $this->itemMapper->findAllFeed( - $id, $limit, $offset, $showAll, $oldestFirst, $userId, - $search - ); - case FeedType::FOLDER: - return $this->itemMapper->findAllFolder( - $id, $limit, $offset, $showAll, $oldestFirst, $userId, - $search - ); - default: - return $this->itemMapper->findAll( - $limit, $offset, $type, $showAll, $oldestFirst, $userId, $search - ); + switch ($type) { + case FeedType::FEED: + return $this->itemMapper->findAllFeed( + $id, + $limit, + $offset, + $showAll, + $oldestFirst, + $userId, + $search + ); + case FeedType::FOLDER: + return $this->itemMapper->findAllFolder( + $id, + $limit, + $offset, + $showAll, + $oldestFirst, + $userId, + $search + ); + default: + return $this->itemMapper->findAll( + $limit, + $offset, + $type, + $showAll, + $oldestFirst, + $userId, + $search + ); } } @@ -126,16 +158,18 @@ class ItemService extends Service { try { /** - * @var Item $item + * @var Item $item */ $item = $this->itemMapper->findByGuidHash( - $guidHash, $feedId, $userId + $guidHash, + $feedId, + $userId ); $item->setStarred($isStarred); $this->itemMapper->update($item); - } catch(DoesNotExistException $ex) { + } catch (DoesNotExistException $ex) { throw new ServiceNotFoundException($ex->getMessage()); } } @@ -155,7 +189,7 @@ class ItemService extends Service try { $lastModified = $this->timeFactory->getMicroTime(); $this->itemMapper->readItem($itemId, $isRead, $lastModified, $userId); - } catch(DoesNotExistException $ex) { + } catch (DoesNotExistException $ex) { throw new ServiceNotFoundException($ex->getMessage()); } } @@ -189,7 +223,10 @@ class ItemService extends Service { $time = $this->timeFactory->getMicroTime(); $this->itemMapper->readFolder( - $folderId, $highestItemId, $time, $userId + $folderId, + $highestItemId, + $time, + $userId ); } @@ -232,11 +269,11 @@ class ItemService extends Service * @throws ServiceNotFoundException if there is no newest item * @return int */ - public function getNewestItemId($userId) + public function getNewestItemId($userId) { try { return $this->itemMapper->getNewestItemId($userId); - } catch(DoesNotExistException $ex) { + } catch (DoesNotExistException $ex) { throw new ServiceNotFoundException($ex->getMessage()); } } @@ -258,7 +295,7 @@ class ItemService extends Service * @param string $userId from which user the items should be taken * @return array of items which are starred or unread */ - public function getUnreadOrStarred($userId) + public function getUnreadOrStarred($userId) { return $this->itemMapper->findAllUnreadOrStarred($userId); } @@ -269,7 +306,7 @@ class ItemService extends Service * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser($userId) { $this->itemMapper->deleteUser($userId); } @@ -278,9 +315,8 @@ class ItemService extends Service /** * Regenerates the search index for all items */ - public function generateSearchIndices() + public function generateSearchIndices() { $this->itemMapper->updateSearchIndices(); } - } diff --git a/lib/Service/Service.php b/lib/Service/Service.php index 28cfeeb76..ac9b6e1a3 100644 --- a/lib/Service/Service.php +++ b/lib/Service/Service.php @@ -18,7 +18,6 @@ use \OCP\AppFramework\Db\MultipleObjectsReturnedException; use \OCA\News\Db\NewsMapper; - abstract class Service { @@ -58,11 +57,10 @@ abstract class Service { try { return $this->mapper->find($id, $userId); - } catch(DoesNotExistException $ex){ + } catch (DoesNotExistException $ex) { throw new ServiceNotFoundException($ex->getMessage()); - } catch(MultipleObjectsReturnedException $ex){ + } catch (MultipleObjectsReturnedException $ex) { throw new ServiceNotFoundException($ex->getMessage()); } } - } diff --git a/lib/Service/ServiceConflictException.php b/lib/Service/ServiceConflictException.php index 95cb1da1f..95955eecb 100644 --- a/lib/Service/ServiceConflictException.php +++ b/lib/Service/ServiceConflictException.php @@ -13,7 +13,6 @@ namespace OCA\News\Service; - class ServiceConflictException extends ServiceException { @@ -26,5 +25,4 @@ class ServiceConflictException extends ServiceException { parent::__construct($msg); } - -} \ No newline at end of file +} diff --git a/lib/Service/ServiceException.php b/lib/Service/ServiceException.php index 4d17dd155..ef1aa1102 100644 --- a/lib/Service/ServiceException.php +++ b/lib/Service/ServiceException.php @@ -13,7 +13,6 @@ namespace OCA\News\Service; - class ServiceException extends \Exception { @@ -26,5 +25,4 @@ class ServiceException extends \Exception { parent::__construct($msg); } - -} \ No newline at end of file +} diff --git a/lib/Service/ServiceNotFoundException.php b/lib/Service/ServiceNotFoundException.php index 5c831775b..65ba092d7 100644 --- a/lib/Service/ServiceNotFoundException.php +++ b/lib/Service/ServiceNotFoundException.php @@ -13,7 +13,6 @@ namespace OCA\News\Service; - class ServiceNotFoundException extends ServiceException { @@ -26,5 +25,4 @@ class ServiceNotFoundException extends ServiceException { parent::__construct($msg); } - -} \ No newline at end of file +} diff --git a/lib/Service/ServiceValidationException.php b/lib/Service/ServiceValidationException.php index f06c2500b..20485642b 100644 --- a/lib/Service/ServiceValidationException.php +++ b/lib/Service/ServiceValidationException.php @@ -13,7 +13,6 @@ namespace OCA\News\Service; - class ServiceValidationException extends ServiceException { @@ -26,5 +25,4 @@ class ServiceValidationException extends ServiceException { parent::__construct($msg); } - -} \ No newline at end of file +} diff --git a/lib/Service/StatusService.php b/lib/Service/StatusService.php index 623f71587..e611a34ea 100644 --- a/lib/Service/StatusService.php +++ b/lib/Service/StatusService.php @@ -20,7 +20,6 @@ use OCP\IDBConnection; use OCA\News\Config\Config; - class StatusService { @@ -32,8 +31,11 @@ class StatusService */ private $connection; - public function __construct(IConfig $settings, IDBConnection $connection, - Config $config, $AppName + public function __construct( + IConfig $settings, + IDBConnection $connection, + Config $config, + $AppName ) { $this->settings = $settings; $this->config = $config; @@ -41,10 +43,11 @@ class StatusService $this->connection = $connection; } - public function isProperlyConfigured() + public function isProperlyConfigured() { $cronMode = $this->settings->getAppValue( - 'core', 'backgroundjobs_mode' + 'core', + 'backgroundjobs_mode' ); $cronOff = !$this->config->getUseCronUpdates(); @@ -53,10 +56,11 @@ class StatusService } - public function getStatus() + public function getStatus() { $version = $this->settings->getAppValue( - $this->appName, 'installed_version' + $this->appName, + 'installed_version' ); return [ @@ -67,5 +71,4 @@ class StatusService ] ]; } - } -- cgit v1.2.3