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: 16: 17: 18: 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: 47: 48: 49: 50: 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:
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: 73: 74: 75: 76: 77: 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:
89:
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: 105: 106: 107:
108: private function getLastTweetIdFromCookie(Request $request)
109: {
110: if ($request->cookies->has('lastTweetId')) {
111: return $request->cookies->get('lastTweetId');
112: }
113:
114: }
115:
116: 117: 118: 119: 120:
121: private function createCookie($firstTweetId)
122: {
123: $nextYear = new \Datetime('now');
124: $nextYear->add(new \DateInterval('P1Y'));
125:
126:
127: $cookie = new Cookie('lastTweetId', $firstTweetId,
128: $nextYear->format('U'));
129:
130: return $cookie;
131: }
132:
133: 134: 135:
136: public function resetCookieAction()
137: {
138:
139: $response = new RedirectResponse(
140: $this->generateUrl('asynctweets_homepage')
141: );
142:
143:
144: $cookie = new Cookie('lastTweetId', null);
145: $response->headers->setCookie($cookie);
146:
147: return $response;
148: }
149:
150: 151: 152: 153: 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: