Overview

Namespaces

  • AlexisLefebvre
    • Bundle
      • AsyncTweetsBundle
        • Command
        • Controller
        • Entity
        • Utils

Classes

  • AlexisLefebvre\Bundle\AsyncTweetsBundle\AsyncTweetsBundle
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\BaseCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesHomeTimelineCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesHomeTimelineTestCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesReadCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesShowCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Controller\DefaultController
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Media
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Tweet
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\TweetRepository
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\User
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Utils\PersistTweet
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Controller;
  4: 
  5: use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6: use Symfony\Component\HttpFoundation\Cookie;
  7: use Symfony\Component\HttpFoundation\RedirectResponse;
  8: use Symfony\Component\HttpFoundation\Request;
  9: 
 10: class DefaultController extends Controller
 11: {
 12:     private $tweetRepository;
 13: 
 14:     /**
 15:      * @param Request     $request
 16:      * @param string|null $firstTweetId
 17:      *
 18:      * @return \Symfony\Component\HttpFoundation\Response $response
 19:      */
 20:     public function indexAction(Request $request, $firstTweetId = null)
 21:     {
 22:         $this->tweetRepository = $this->getDoctrine()
 23:             ->getRepository('AsyncTweetsBundle:Tweet');
 24: 
 25:         $tweets = $this->tweetRepository
 26:             ->getWithUsersAndMedias($firstTweetId);
 27: 
 28:         $variables = $this->getVariables($request, $tweets, $firstTweetId);
 29: 
 30:         $response = $this->render(
 31:             'AsyncTweetsBundle:Default:index.html.twig',
 32:             [
 33:                 'tweets' => $tweets,
 34:                 'vars'   => $variables,
 35:             ]
 36:         );
 37: 
 38:         if (!is_null($variables['cookie'])) {
 39:             $response->headers->setCookie($variables['cookie']);
 40:         }
 41: 
 42:         return $response;
 43:     }
 44: 
 45:     /**
 46:      * @param Request  $request
 47:      * @param Tweets[] $tweets
 48:      * @param int      $firstTweetId
 49:      *
 50:      * @return array $vars
 51:      */
 52:     private function getVariables(Request $request, $tweets, $firstTweetId)
 53:     {
 54:         $vars = [
 55:             'first'    => $firstTweetId,
 56:             'previous' => null,
 57:             'next'     => null,
 58:             'number'   => 0,
 59:             'cookieId' => $this->getLastTweetIdFromCookie($request),
 60:             // No cookie by default
 61:             'cookie' => null,
 62:         ];
 63: 
 64:         if (count($tweets) > 0) {
 65:             $vars = $this->getTweetsVars($tweets, $vars);
 66:         }
 67: 
 68:         return $vars;
 69:     }
 70: 
 71:     /**
 72:      * If a Tweet is displayed, fetch data from repository.
 73:      *
 74:      * @param Tweets[] $tweets
 75:      * @param array    $vars
 76:      *
 77:      * @return array $vars
 78:      */
 79:     private function getTweetsVars($tweets, $vars)
 80:     {
 81:         $firstTweetId = $tweets[0]->getId();
 82: 
 83:         $vars['previous'] = $this->tweetRepository
 84:             ->getPreviousTweetId($firstTweetId);
 85:         $vars['next'] = $this->tweetRepository
 86:             ->getNextTweetId($firstTweetId);
 87: 
 88:         // Only update the cookie if the last Tweet Id is bigger than
 89:         //  the one in the cookie
 90:         if ($firstTweetId > $vars['cookieId']) {
 91:             $vars['cookie'] = $this->createCookie($firstTweetId);
 92:             $vars['cookieId'] = $firstTweetId;
 93:         }
 94: 
 95:         $vars['number'] = $this->tweetRepository
 96:             ->countPendingTweets($vars['cookieId']);
 97: 
 98:         $vars['first'] = $firstTweetId;
 99: 
100:         return $vars;
101:     }
102: 
103:     /**
104:      * @param Request $request
105:      *
106:      * @return int|null
107:      */
108:     private function getLastTweetIdFromCookie(Request $request)
109:     {
110:         if ($request->cookies->has('lastTweetId')) {
111:             return $request->cookies->get('lastTweetId');
112:         }
113:         // else
114:     }
115: 
116:     /**
117:      * @param string $firstTweetId
118:      *
119:      * @return Cookie $cookie
120:      */
121:     private function createCookie($firstTweetId)
122:     {
123:         $nextYear = new \Datetime('now');
124:         $nextYear->add(new \DateInterval('P1Y'));
125: 
126:         // Set last Tweet Id
127:         $cookie = new Cookie('lastTweetId', $firstTweetId,
128:             $nextYear->format('U'));
129: 
130:         return $cookie;
131:     }
132: 
133:     /**
134:      * @return RedirectResponse $response
135:      */
136:     public function resetCookieAction()
137:     {
138:         /* @see http://www.craftitonline.com/2011/07/symfony2-how-to-set-a-cookie/ */
139:         $response = new RedirectResponse(
140:             $this->generateUrl('asynctweets_homepage')
141:         );
142: 
143:         // Reset last Tweet Id
144:         $cookie = new Cookie('lastTweetId', null);
145:         $response->headers->setCookie($cookie);
146: 
147:         return $response;
148:     }
149: 
150:     /**
151:      * @param Request $request
152:      *
153:      * @return RedirectResponse $response
154:      */
155:     public function deleteLessThanAction(Request $request)
156:     {
157:         $lastTweetId = $this->getLastTweetIdFromCookie($request);
158: 
159:         if (!is_null($lastTweetId)) {
160:             $count = $this->getDoctrine()
161:                 ->getRepository('AsyncTweetsBundle:Tweet')
162:                 ->deleteAndHideTweetsLessThanId($lastTweetId);
163: 
164:             $this->get('session')->getFlashBag()->add('message',
165:                 sprintf('%s tweets deleted.', $count)
166:             );
167:         }
168: 
169:         return $this->redirect($this->generateUrl('asynctweets_homepage'));
170:     }
171: }
172: 
AsyncTweetsBundle API documentation generated by ApiGen