Feed Controller split into method calls

This commit is contained in:
Benoît Burnichon
2015-05-14 17:43:25 +02:00
parent 549c63d083
commit a13726025e

View File

@@ -36,23 +36,67 @@ class Feed implements ControllerProviderInterface
$controllers = $this->createAuthenticatedCollection($app); $controllers = $this->createAuthenticatedCollection($app);
$controllers->post('/requestavailable/', function (Application $app, Request $request) { $controllers->post('/requestavailable/', 'controller.prod.feed:publishRecordsAction');
$controllers->post('/entry/create/', 'controller.prod.feed:createFeedEntryAction')
->bind('prod_feeds_entry_create')
->before('controller.prod.feed:ensureUserHasPublishRight');
$controllers->get('/entry/{id}/edit/', 'controller.prod.feed:editEntryAction')
->bind('prod_feeds_entry_edit')
->assert('id', '\d+')
->before('controller.prod.feed:ensureUserHasPublishRight');
$controllers->post('/entry/{id}/update/', 'controller.prod.feed:updateEntryAction')
->bind('prod_feeds_entry_update')
->assert('id', '\d+')
->before('controller.prod.feed:ensureUserHasPublishRight');
$controllers->post('/entry/{id}/delete/', 'controller.prod.feed:deleteEntryAction')
->bind('prod_feeds_entry_delete')
->assert('id', '\d+')
->before('controller.prod.feed:ensureUserHasPublishRight');
$controllers->get('/', 'controller.prod.feed:indexAction')
->bind('prod_feeds');
$controllers->get('/feed/{id}/', 'controller.prod.feed:showAction')
->bind('prod_feeds_feed')
->assert('id', '\d+');
$controllers->get('/subscribe/aggregated/', 'controller.prod.feed:subscribeAggregatedFeedAction')
->bind('prod_feeds_subscribe_aggregated');
$controllers->get('/subscribe/{id}/', 'controller.prod.feed:subscribeFeedAction')
->bind('prod_feeds_subscribe_feed')
->assert('id', '\d+');
return $controllers;
}
public function publishRecordsAction(Application $app, Request $request)
{
$feeds = $app['repo.feeds']->getAllForUser( $feeds = $app['repo.feeds']->getAllForUser(
$app['acl']->get($app['authentication']->getUser()) $app['acl']->get($app['authentication']->getUser())
); );
$publishing = RecordsRequest::fromRequest($app, $request, true, [], ['bas_chupub']); $publishing = RecordsRequest::fromRequest($app, $request, true, [], ['bas_chupub']);
return $app['twig']->render('prod/actions/publish/publish.html.twig', ['publishing' => $publishing, 'feeds' => $feeds]); return $app['twig']->render(
}); 'prod/actions/publish/publish.html.twig',
['publishing' => $publishing, 'feeds' => $feeds]
);
}
$controllers->post('/entry/create/', function (Application $app, Request $request) { public function createFeedEntryAction(Application $app, Request $request) {
$feed = $app['repo.feeds']->find($request->request->get('feed_id')); $feed = $app['repo.feeds']->find($request->request->get('feed_id'));
if (null === $feed) { if (null === $feed) {
$app->abort(404, "Feed not found"); $app->abort(404, "Feed not found");
} }
$publisher = $app['repo.feed-publishers']->findOneBy(['feed' => $feed, 'user' => $app['authentication']->getUser()]); $publisher = $app['repo.feed-publishers']->findOneBy(
['feed' => $feed, 'user' => $app['authentication']->getUser()]
);
if ('' === $title = trim($request->request->get('title', ''))) { if ('' === $title = trim($request->request->get('title', ''))) {
$app->abort(400, "Bad request"); $app->abort(400, "Bad request");
@@ -86,18 +130,17 @@ class Feed implements ControllerProviderInterface
$app['orm.em']->persist($feed); $app['orm.em']->persist($feed);
$app['orm.em']->flush(); $app['orm.em']->flush();
$app['dispatcher']->dispatch(PhraseaEvents::FEED_ENTRY_CREATE, new FeedEntryEvent($entry, $request->request->get('notify'))); $app['dispatcher']->dispatch(
PhraseaEvents::FEED_ENTRY_CREATE,
new FeedEntryEvent($entry, $request->request->get('notify'))
);
$datas = ['error' => false, 'message' => false]; $datas = ['error' => false, 'message' => false];
return $app->json($datas); return $app->json($datas);
}) }
->bind('prod_feeds_entry_create')
->before(function (Request $request) use ($app) {
$app['firewall']->requireRight('bas_chupub');
});
$controllers->get('/entry/{id}/edit/', function (Application $app, Request $request, $id) { public function editEntryAction(Application $app, Request $request, $id) {
$entry = $app['repo.feed-entries']->find($id); $entry = $app['repo.feed-entries']->find($id);
if (!$entry->isPublisher($app['authentication']->getUser())) { if (!$entry->isPublisher($app['authentication']->getUser())) {
@@ -106,17 +149,21 @@ class Feed implements ControllerProviderInterface
$feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser())); $feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser()));
$datas = $app['twig']->render('prod/actions/publish/publish_edit.html.twig', ['entry' => $entry, 'feeds' => $feeds]); $datas = $app['twig']->render(
'prod/actions/publish/publish_edit.html.twig',
['entry' => $entry, 'feeds' => $feeds]
);
return new Response($datas); return new Response($datas);
}) }
->bind('prod_feeds_entry_edit')
->assert('id', '\d+')
->before(function (Request $request) use ($app) {
$app['firewall']->requireRight('bas_chupub');
});
$controllers->post('/entry/{id}/update/', function (Application $app, Request $request, $id) { /**
* @param Application $app
* @param Request $request
* @param int $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function updateEntryAction(Application $app, Request $request, $id) {
$datas = ['error' => true, 'message' => '', 'datas' => '']; $datas = ['error' => true, 'message' => '', 'datas' => ''];
$entry = $app['repo.feed-entries']->find($id); $entry = $app['repo.feed-entries']->find($id);
@@ -135,7 +182,8 @@ class Feed implements ControllerProviderInterface
->setTitle($title) ->setTitle($title)
->setSubtitle($request->request->get('subtitle', '')); ->setSubtitle($request->request->get('subtitle', ''));
$currentFeedId = $entry->getFeed()->getId(); $currentFeedId = $entry->getFeed()
->getId();
$new_feed_id = $request->request->get('feed_id', $currentFeedId); $new_feed_id = $request->request->get('feed_id', $currentFeedId);
if ($currentFeedId !== (int)$new_feed_id) { if ($currentFeedId !== (int)$new_feed_id) {
@@ -166,20 +214,21 @@ class Feed implements ControllerProviderInterface
$app['orm.em']->persist($entry); $app['orm.em']->persist($entry);
$app['orm.em']->flush(); $app['orm.em']->flush();
return $app->json([ return $app->json(
[
'error' => false, 'error' => false,
'message' => 'succes', 'message' => 'succes',
'datas' => $app['twig']->render('prod/results/entry.html.twig', [ 'datas' => $app['twig']->render(
'prod/results/entry.html.twig',
[
'entry' => $entry 'entry' => $entry
]) ]
]); )
}) ]
->bind('prod_feeds_entry_update') );
->assert('id', '\d+')->before(function (Request $request) use ($app) { }
$app['firewall']->requireRight('bas_chupub');
});
$controllers->post('/entry/{id}/delete/', function (Application $app, Request $request, $id) { public function deleteEntryAction(Application $app, Request $request, $id) {
$datas = ['error' => true, 'message' => '']; $datas = ['error' => true, 'message' => ''];
$entry = $app['repo.feed-entries']->find($id); $entry = $app['repo.feed-entries']->find($id);
@@ -187,7 +236,9 @@ class Feed implements ControllerProviderInterface
if (null === $entry) { if (null === $entry) {
$app->abort(404, 'Entry not found'); $app->abort(404, 'Entry not found');
} }
if (!$entry->isPublisher($app['authentication']->getUser()) && $entry->getFeed()->isOwner($app['authentication']->getUser()) === false) { if (!$entry->isPublisher($app['authentication']->getUser()) && $entry->getFeed()
->isOwner($app['authentication']->getUser()) === false
) {
$app->abort(403, $app->trans('Action Forbidden : You are not the publisher')); $app->abort(403, $app->trans('Action Forbidden : You are not the publisher'));
} }
@@ -195,29 +246,28 @@ class Feed implements ControllerProviderInterface
$app['orm.em']->flush(); $app['orm.em']->flush();
return $app->json(['error' => false, 'message' => 'succes']); return $app->json(['error' => false, 'message' => 'succes']);
}) }
->bind('prod_feeds_entry_delete')
->assert('id', '\d+')->before(function (Request $request) use ($app) {
$app['firewall']->requireRight('bas_chupub');
});
$controllers->get('/', function (Application $app, Request $request) { public function indexAction(Application $app, Request $request) {
$request = $app['request']; $request = $app['request'];
$page = (int)$request->query->get('page'); $page = (int)$request->query->get('page');
$page = $page > 0 ? $page : 1; $page = $page > 0 ? $page : 1;
$feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser())); $feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser()));
$datas = $app['twig']->render('prod/results/feeds.html.twig', [ $datas = $app['twig']->render(
'prod/results/feeds.html.twig',
[
'feeds' => $feeds, 'feeds' => $feeds,
'feed' => new Aggregate($app['orm.em'], $feeds), 'feed' => new Aggregate($app['orm.em'], $feeds),
'page' => $page 'page' => $page
]); ]
);
return new Response($datas); return new Response($datas);
})->bind('prod_feeds'); }
$controllers->get('/feed/{id}/', function (Application $app, Request $request, $id) { public function showAction(Application $app, Request $request, $id) {
$page = (int)$request->query->get('page'); $page = (int)$request->query->get('page');
$page = $page > 0 ? $page : 1; $page = $page > 0 ? $page : 1;
@@ -227,55 +277,67 @@ class Feed implements ControllerProviderInterface
} }
$feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser())); $feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser()));
$datas = $app['twig']->render('prod/results/feeds.html.twig', ['feed' => $feed, 'feeds' => $feeds, 'page' => $page]); $datas = $app['twig']->render(
'prod/results/feeds.html.twig',
['feed' => $feed, 'feeds' => $feeds, 'page' => $page]
);
return new Response($datas); return new Response($datas);
}) }
->bind('prod_feeds_feed')
->assert('id', '\d+');
$controllers->get('/subscribe/aggregated/', function (Application $app, Request $request) { public function subscribeAggregatedFeedAction(Application $app, Request $request) {
$renew = ($request->query->get('renew') === 'true'); $renew = ($request->query->get('renew') === 'true');
$feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser())); $feeds = $app['repo.feeds']->getAllForUser($app['acl']->get($app['authentication']->getUser()));
$link = $app['feed.aggregate-link-generator']->generate(new Aggregate($app['orm.em'], $feeds), $link = $app['feed.aggregate-link-generator']->generate(
new Aggregate($app['orm.em'], $feeds),
$app['authentication']->getUser(), $app['authentication']->getUser(),
AggregateLinkGenerator::FORMAT_RSS, AggregateLinkGenerator::FORMAT_RSS,
null, $renew null,
$renew
); );
$output = [ $output = [
'texte' => '<p>' . $app->trans('publication::Voici votre fil RSS personnel. Il vous permettra d\'etre tenu au courrant des publications.') 'texte' => '<p>' . $app->trans(
. '</p><p>' . $app->trans('publications::Ne le partagez pas, il est strictement confidentiel') . '</p> 'publication::Voici votre fil RSS personnel. Il vous permettra d\'etre tenu au courrant des publications.'
<div><input type="text" readonly="readonly" class="input_select_copy" value="' . $link->getURI() . '"/></div>', ) . '</p><p>' . $app->trans('publications::Ne le partagez pas, il est strictement confidentiel') . '</p>
<div><input type="text" readonly="readonly" class="input_select_copy" value="' . $link->getURI()
. '"/></div>',
'titre' => $app->trans('publications::votre rss personnel') 'titre' => $app->trans('publications::votre rss personnel')
]; ];
return $app->json($output); return $app->json($output);
})->bind('prod_feeds_subscribe_aggregated'); }
$controllers->get('/subscribe/{id}/', function (Application $app, Request $request, $id) { public function subscribeFeedAction(Application $app, Request $request, $id) {
$renew = ($request->query->get('renew') === 'true'); $renew = ($request->query->get('renew') === 'true');
$feed = $app['repo.feeds']->find($id); $feed = $app['repo.feeds']->find($id);
if (!$feed->isAccessible($app['authentication']->getUser(), $app)) { if (!$feed->isAccessible($app['authentication']->getUser(), $app)) {
$app->abort(404, 'Feed not found'); $app->abort(404, 'Feed not found');
} }
$link = $app['feed.user-link-generator']->generate($feed, $app['authentication']->getUser(), FeedLinkGenerator::FORMAT_RSS, null, $renew); $link = $app['feed.user-link-generator']->generate(
$feed,
$app['authentication']->getUser(),
FeedLinkGenerator::FORMAT_RSS,
null,
$renew
);
$output = [ $output = [
'texte' => '<p>' . $app->trans('publication::Voici votre fil RSS personnel. Il vous permettra d\'etre tenu au courrant des publications.') 'texte' => '<p>' . $app->trans(
. '</p><p>' . $app->trans('publications::Ne le partagez pas, il est strictement confidentiel') . '</p> 'publication::Voici votre fil RSS personnel. Il vous permettra d\'etre tenu au courrant des publications.'
) . '</p><p>' . $app->trans('publications::Ne le partagez pas, il est strictement confidentiel') . '</p>
<div><input type="text" style="width:100%" value="' . $link->getURI() . '"/></div>', <div><input type="text" style="width:100%" value="' . $link->getURI() . '"/></div>',
'titre' => $app->trans('publications::votre rss personnel') 'titre' => $app->trans('publications::votre rss personnel')
]; ];
return $app->json($output); return $app->json($output);
}) }
->bind('prod_feeds_subscribe_feed')
->assert('id', '\d+');
return $controllers; public function ensureUserHasPublishRight(Application $app)
{
$this->getFirewall($app)->requireRight('bas_chupub');
} }
} }