From aa849d75c09e81795ccd9672265149d5f44b45a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Thu, 18 Jun 2015 18:08:02 +0200 Subject: [PATCH] Refactor RssFeedController --- lib/Alchemy/Phrasea/Application.php | 4 +- .../Controller/Root/RSSFeedController.php | 121 ++++++++++++++++++ .../ControllerProvider/Root/RSSFeeds.php | 95 +++----------- .../Feed/Formatter/FeedFormatterInterface.php | 3 +- 4 files changed, 144 insertions(+), 79 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Controller/Root/RSSFeedController.php diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index f6bbfca422..d0471aa9b0 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -13,7 +13,6 @@ namespace Alchemy\Phrasea; use Alchemy\Geonames\GeonamesServiceProvider; use Alchemy\Phrasea\ControllerProvider\Root\Root; -use Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds; use Alchemy\Phrasea\ControllerProvider\Root\Session; use Alchemy\Phrasea\ControllerProvider\Thesaurus\Thesaurus; use Alchemy\Phrasea\ControllerProvider\Thesaurus\Xmlhttp as ThesaurusXMLHttp; @@ -317,6 +316,7 @@ class Application extends SilexApplication 'Alchemy\Phrasea\ControllerProvider\Root\Account' => [], 'Alchemy\Phrasea\ControllerProvider\Root\Developers' => [], 'Alchemy\Phrasea\ControllerProvider\Root\Login' => [], + 'Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds' => [], 'Alchemy\Phrasea\ControllerProvider\Datafiles' => [], 'Alchemy\Phrasea\ControllerProvider\Lightbox' => [], 'Alchemy\Phrasea\ControllerProvider\MediaAccessor' => [], @@ -622,7 +622,6 @@ class Application extends SilexApplication public function bindRoutes() { $this->mount('/', new Root()); - $this->mount('/feeds/', new RSSFeeds()); $this->mount('/user/preferences/', new Preferences()); $this->mount('/user/notifications/', new Notifications()); @@ -651,6 +650,7 @@ class Application extends SilexApplication '/datafiles' => 'Alchemy\Phrasea\ControllerProvider\Datafiles', '/developers/' => 'Alchemy\Phrasea\ControllerProvider\Root\Developers', '/download/' => 'Alchemy\Phrasea\ControllerProvider\Prod\DoDownload', + '/feeds/' => 'Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds', '/include/minify' => 'Alchemy\Phrasea\ControllerProvider\Minifier', '/login/' => 'Alchemy\Phrasea\ControllerProvider\Root\Login', '/lightbox' => 'Alchemy\Phrasea\ControllerProvider\Lightbox', diff --git a/lib/Alchemy/Phrasea/Controller/Root/RSSFeedController.php b/lib/Alchemy/Phrasea/Controller/Root/RSSFeedController.php new file mode 100644 index 0000000000..dd4591c56f --- /dev/null +++ b/lib/Alchemy/Phrasea/Controller/Root/RSSFeedController.php @@ -0,0 +1,121 @@ +getFeedRepository()->find($id); + + if (! $feed instanceof Feed) { + $this->app->abort(404, 'Feed not found'); + } + + if (!$feed->isPublic()) { + $this->app->abort(403, 'Forbidden'); + } + + return $this->createFormattedFeedResponse($format, $feed, (int) $request->query->get('page')); + } + + public function showUserFeedAction(Request $request, $id, $format) + { + $token = $this->getFeedTokenRepository()->find($id); + + $page = (int)$request->query->get('page'); + + return $this->createFormattedFeedResponse($format, $token->getFeed(), $page, $token->getUser()); + } + + public function showAggregatedUserFeedAction(Request $request, $token, $format) + { + $token = $this->getAggregateTokenRepository()->findOneBy(["value" => $token]); + $user = $token->getUser(); + + $feeds = $this->getFeedRepository()->getAllForUser($this->getAclForUser($user)); + + $aggregate = new Aggregate($this->getEntityManager(), $feeds, $token); + + $page = (int) $request->query->get('page'); + + return $this->createFormattedFeedResponse($format, $aggregate, $page, $user); + } + + public function showCoolirisPublicFeedAction(Request $request) + { + $feed = Aggregate::getPublic($this->app); + + $page = (int) $request->query->get('page'); + + return $this->createFormattedFeedResponse('cooliris', $feed, $page); + } + + public function showAggregatedPublicFeedAction(Request $request, $format) { + $feed = Aggregate::getPublic($this->app); + + $page = (int) $request->query->get('page'); + + return $this->createFormattedFeedResponse($format, $feed, $page); + } + + /** + * @return FeedRepository + */ + private function getFeedRepository() + { + return $this->app['repo.feeds']; + } + + /** + * @param string $format + * @param FeedInterface $feed + * @param int $page + * @return Response + */ + private function createFormattedFeedResponse($format, $feed, $page, User $user = null, $generator = 'Phraseanet') + { + /** @var FeedFormatterInterface $formatter */ + $formatter = $this->app['feed.formatter-strategy']($format); + + return $formatter->createResponse($this->app, $feed, $page < 1 ? 1 : $page, $user, $generator); + } + + /** + * @return FeedTokenRepository + */ + private function getFeedTokenRepository() + { + return $this->app["repo.feed-tokens"]; + } + + /** + * @return AggregateTokenRepository + */ + private function getAggregateTokenRepository() + { + return $this->app['repo.aggregate-tokens']; + } +} diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Root/RSSFeeds.php b/lib/Alchemy/Phrasea/ControllerProvider/Root/RSSFeeds.php index eb0b69191c..d4a9379f84 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Root/RSSFeeds.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Root/RSSFeeds.php @@ -11,19 +11,34 @@ namespace Alchemy\Phrasea\ControllerProvider\Root; +use Alchemy\Phrasea\Application as PhraseaApplication; +use Alchemy\Phrasea\Controller\LazyLocator; +use Alchemy\Phrasea\Controller\Root\RSSFeedController; use Alchemy\Phrasea\ControllerProvider\ControllerProviderTrait; -use Alchemy\Phrasea\Feed\Aggregate; use Silex\Application; use Silex\ControllerProviderInterface; +use Silex\ServiceProviderInterface; -class RSSFeeds implements ControllerProviderInterface +class RSSFeeds implements ControllerProviderInterface, ServiceProviderInterface { use ControllerProviderTrait; + public function register(Application $app) + { + $app['controller.rss-feeds'] = $app->share(function (PhraseaApplication $app) { + return (new RSSFeedController($app)) + ->setEntityManagerLocator(new LazyLocator($app, 'orm.em')) + ; + }); + } + + public function boot(Application $app) + { + // no-op + } + public function connect(Application $app) { - $app['controller.rss-feeds'] = $this; - $controllers = $this->createCollection($app); $controllers->get('/feed/{id}/{format}/', 'controller.rss-feeds:showPublicFeedAction') ->bind('feed_public') @@ -48,76 +63,4 @@ class RSSFeeds implements ControllerProviderInterface return $controllers; } - - public function showPublicFeedAction(Application $app, $id, $format) - { - $feed = $app['repo.feeds']->find($id); - - if (null === $feed) { - $app->abort(404, 'Feed not found'); - } - - if (!$feed->isPublic()) { - $app->abort(403, 'Forbidden'); - } - - $request = $app['request']; - - $page = (int) $request->query->get('page'); - $page = $page < 1 ? 1 : $page; - - return $app['feed.formatter-strategy']($format)->createResponse($app, $feed, $page); - } - - public function showUserFeedAction(Application $app, $token, $id, $format) - { - $token = $app["repo.feed-tokens"]->find($id); - - $request = $app['request']; - - $page = (int) $request->query->get('page'); - $page = $page < 1 ? 1 : $page; - - return $app['feed.formatter-strategy']($format) - ->createResponse($app, $token->getFeed(), $page, $token->getUser()); - } - - public function showAggregatedUserFeedAction(Application $app, $token, $format) - { - $token = $app['repo.aggregate-tokens']->findOneBy(["value" => $token]); - - $user = $token->getUser(); - - $feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($user)); - - $aggregate = new Aggregate($app['orm.em'], $feeds, $token); - - $request = $app['request']; - - $page = (int) $request->query->get('page'); - $page = $page < 1 ? 1 : $page; - - return $app['feed.formatter-strategy']($format)->createResponse($app, $aggregate, $page, $user); - } - - public function showCoolirisPublicFeedAction(Application $app) - { - $feed = Aggregate::getPublic($app); - - $request = $app['request']; - $page = (int) $request->query->get('page'); - $page = $page < 1 ? 1 : $page; - - return $app['feed.formatter-strategy']('cooliris')->createResponse($app, $feed, $page, null, 'Phraseanet', $app); - } - - public function showAggregatedPublicFeedAction(Application $app, $format) { - $feed = Aggregate::getPublic($app); - - $request = $app['request']; - $page = (int) $request->query->get('page'); - $page = $page < 1 ? 1 : $page; - - return $app['feed.formatter-strategy']($format)->createResponse($app, $feed, $page); - } } diff --git a/lib/Alchemy/Phrasea/Feed/Formatter/FeedFormatterInterface.php b/lib/Alchemy/Phrasea/Feed/Formatter/FeedFormatterInterface.php index 9a22726160..261d32c65c 100644 --- a/lib/Alchemy/Phrasea/Feed/Formatter/FeedFormatterInterface.php +++ b/lib/Alchemy/Phrasea/Feed/Formatter/FeedFormatterInterface.php @@ -14,6 +14,7 @@ namespace Alchemy\Phrasea\Feed\Formatter; use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Feed\FeedInterface; use Alchemy\Phrasea\Model\Entities\User; +use Symfony\Component\HttpFoundation\Response; interface FeedFormatterInterface { @@ -39,7 +40,7 @@ interface FeedFormatterInterface * @param string $generator * @param Application $app * - * @return string + * @return Response */ public function createResponse(Application $app, FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet'); }