Refactor RssFeedController

This commit is contained in:
Benoît Burnichon
2015-06-18 18:08:02 +02:00
parent 821439e772
commit aa849d75c0
4 changed files with 144 additions and 79 deletions

View File

@@ -13,7 +13,6 @@ namespace Alchemy\Phrasea;
use Alchemy\Geonames\GeonamesServiceProvider; use Alchemy\Geonames\GeonamesServiceProvider;
use Alchemy\Phrasea\ControllerProvider\Root\Root; use Alchemy\Phrasea\ControllerProvider\Root\Root;
use Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds;
use Alchemy\Phrasea\ControllerProvider\Root\Session; use Alchemy\Phrasea\ControllerProvider\Root\Session;
use Alchemy\Phrasea\ControllerProvider\Thesaurus\Thesaurus; use Alchemy\Phrasea\ControllerProvider\Thesaurus\Thesaurus;
use Alchemy\Phrasea\ControllerProvider\Thesaurus\Xmlhttp as ThesaurusXMLHttp; 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\Account' => [],
'Alchemy\Phrasea\ControllerProvider\Root\Developers' => [], 'Alchemy\Phrasea\ControllerProvider\Root\Developers' => [],
'Alchemy\Phrasea\ControllerProvider\Root\Login' => [], 'Alchemy\Phrasea\ControllerProvider\Root\Login' => [],
'Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds' => [],
'Alchemy\Phrasea\ControllerProvider\Datafiles' => [], 'Alchemy\Phrasea\ControllerProvider\Datafiles' => [],
'Alchemy\Phrasea\ControllerProvider\Lightbox' => [], 'Alchemy\Phrasea\ControllerProvider\Lightbox' => [],
'Alchemy\Phrasea\ControllerProvider\MediaAccessor' => [], 'Alchemy\Phrasea\ControllerProvider\MediaAccessor' => [],
@@ -622,7 +622,6 @@ class Application extends SilexApplication
public function bindRoutes() public function bindRoutes()
{ {
$this->mount('/', new Root()); $this->mount('/', new Root());
$this->mount('/feeds/', new RSSFeeds());
$this->mount('/user/preferences/', new Preferences()); $this->mount('/user/preferences/', new Preferences());
$this->mount('/user/notifications/', new Notifications()); $this->mount('/user/notifications/', new Notifications());
@@ -651,6 +650,7 @@ class Application extends SilexApplication
'/datafiles' => 'Alchemy\Phrasea\ControllerProvider\Datafiles', '/datafiles' => 'Alchemy\Phrasea\ControllerProvider\Datafiles',
'/developers/' => 'Alchemy\Phrasea\ControllerProvider\Root\Developers', '/developers/' => 'Alchemy\Phrasea\ControllerProvider\Root\Developers',
'/download/' => 'Alchemy\Phrasea\ControllerProvider\Prod\DoDownload', '/download/' => 'Alchemy\Phrasea\ControllerProvider\Prod\DoDownload',
'/feeds/' => 'Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds',
'/include/minify' => 'Alchemy\Phrasea\ControllerProvider\Minifier', '/include/minify' => 'Alchemy\Phrasea\ControllerProvider\Minifier',
'/login/' => 'Alchemy\Phrasea\ControllerProvider\Root\Login', '/login/' => 'Alchemy\Phrasea\ControllerProvider\Root\Login',
'/lightbox' => 'Alchemy\Phrasea\ControllerProvider\Lightbox', '/lightbox' => 'Alchemy\Phrasea\ControllerProvider\Lightbox',

View File

@@ -0,0 +1,121 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2015 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Controller\Root;
use Alchemy\Phrasea\Application\Helper\EntityManagerAware;
use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Feed\Aggregate;
use Alchemy\Phrasea\Feed\FeedInterface;
use Alchemy\Phrasea\Feed\Formatter\FeedFormatterInterface;
use Alchemy\Phrasea\Model\Entities\Feed;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Repositories\AggregateTokenRepository;
use Alchemy\Phrasea\Model\Repositories\FeedRepository;
use Alchemy\Phrasea\Model\Repositories\FeedTokenRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RSSFeedController extends Controller
{
use EntityManagerAware;
public function showPublicFeedAction(Request $request, $id, $format)
{
$feed = $this->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'];
}
}

View File

@@ -11,19 +11,34 @@
namespace Alchemy\Phrasea\ControllerProvider\Root; 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\ControllerProvider\ControllerProviderTrait;
use Alchemy\Phrasea\Feed\Aggregate;
use Silex\Application; use Silex\Application;
use Silex\ControllerProviderInterface; use Silex\ControllerProviderInterface;
use Silex\ServiceProviderInterface;
class RSSFeeds implements ControllerProviderInterface class RSSFeeds implements ControllerProviderInterface, ServiceProviderInterface
{ {
use ControllerProviderTrait; 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) public function connect(Application $app)
{ {
$app['controller.rss-feeds'] = $this;
$controllers = $this->createCollection($app); $controllers = $this->createCollection($app);
$controllers->get('/feed/{id}/{format}/', 'controller.rss-feeds:showPublicFeedAction') $controllers->get('/feed/{id}/{format}/', 'controller.rss-feeds:showPublicFeedAction')
->bind('feed_public') ->bind('feed_public')
@@ -48,76 +63,4 @@ class RSSFeeds implements ControllerProviderInterface
return $controllers; 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);
}
} }

View File

@@ -14,6 +14,7 @@ namespace Alchemy\Phrasea\Feed\Formatter;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Feed\FeedInterface; use Alchemy\Phrasea\Feed\FeedInterface;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Symfony\Component\HttpFoundation\Response;
interface FeedFormatterInterface interface FeedFormatterInterface
{ {
@@ -39,7 +40,7 @@ interface FeedFormatterInterface
* @param string $generator * @param string $generator
* @param Application $app * @param Application $app
* *
* @return string * @return Response
*/ */
public function createResponse(Application $app, FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet'); public function createResponse(Application $app, FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet');
} }