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
|
7 |
|
public function indexAction(Request $request, $firstTweetId = null) |
21
|
|
|
{ |
22
|
7 |
|
$this->tweetRepository = $this->getDoctrine() |
23
|
7 |
|
->getRepository('AsyncTweetsBundle:Tweet'); |
24
|
|
|
|
25
|
7 |
|
$tweets = $this->tweetRepository |
26
|
7 |
|
->getWithUsersAndMedias($firstTweetId); |
27
|
|
|
|
28
|
7 |
|
$variables = $this->getVariables($request, $tweets, $firstTweetId); |
29
|
|
|
|
30
|
7 |
|
$response = $this->render( |
31
|
7 |
|
'AsyncTweetsBundle:Default:index.html.twig', |
32
|
|
|
[ |
33
|
7 |
|
'tweets' => $tweets, |
34
|
7 |
|
'vars' => $variables, |
35
|
|
|
] |
36
|
|
|
); |
37
|
|
|
|
38
|
7 |
|
if (!is_null($variables['cookie'])) { |
39
|
6 |
|
$response->headers->setCookie($variables['cookie']); |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
return $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param Tweets[] $tweets |
48
|
|
|
* @param int $firstTweetId |
49
|
|
|
* |
50
|
|
|
* @return array $vars |
51
|
|
|
*/ |
52
|
7 |
|
private function getVariables(Request $request, $tweets, $firstTweetId) |
53
|
|
|
{ |
54
|
|
|
$vars = [ |
55
|
7 |
|
'first' => $firstTweetId, |
56
|
|
|
'previous' => null, |
57
|
|
|
'next' => null, |
58
|
7 |
|
'number' => 0, |
59
|
7 |
|
'cookieId' => $this->getLastTweetIdFromCookie($request), |
60
|
|
|
// No cookie by default |
61
|
|
|
'cookie' => null, |
62
|
|
|
]; |
63
|
|
|
|
64
|
7 |
|
if (count($tweets) > 0) { |
65
|
6 |
|
$vars = $this->getTweetsVars($tweets, $vars); |
66
|
|
|
} |
67
|
|
|
|
68
|
7 |
|
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
|
6 |
|
private function getTweetsVars($tweets, $vars) |
80
|
|
|
{ |
81
|
6 |
|
$firstTweetId = $tweets[0]->getId(); |
82
|
|
|
|
83
|
6 |
|
$vars['previous'] = $this->tweetRepository |
84
|
6 |
|
->getPreviousTweetId($firstTweetId); |
85
|
6 |
|
$vars['next'] = $this->tweetRepository |
86
|
6 |
|
->getNextTweetId($firstTweetId); |
87
|
|
|
|
88
|
|
|
// Only update the cookie if the last Tweet Id is bigger than |
89
|
|
|
// the one in the cookie |
90
|
6 |
|
if ($firstTweetId > $vars['cookieId']) { |
91
|
6 |
|
$vars['cookie'] = $this->createCookie($firstTweetId); |
92
|
6 |
|
$vars['cookieId'] = $firstTweetId; |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
$vars['number'] = $this->tweetRepository |
96
|
6 |
|
->countPendingTweets($vars['cookieId']); |
97
|
|
|
|
98
|
6 |
|
$vars['first'] = $firstTweetId; |
99
|
|
|
|
100
|
6 |
|
return $vars; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Request $request |
105
|
|
|
* |
106
|
|
|
* @return int|null |
107
|
|
|
*/ |
108
|
7 |
|
private function getLastTweetIdFromCookie(Request $request) |
109
|
|
|
{ |
110
|
7 |
|
if ($request->cookies->has('lastTweetId')) { |
111
|
4 |
|
return $request->cookies->get('lastTweetId'); |
112
|
|
|
} |
113
|
|
|
// else |
114
|
7 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $firstTweetId |
118
|
|
|
* |
119
|
|
|
* @return Cookie $cookie |
120
|
|
|
*/ |
121
|
6 |
|
private function createCookie($firstTweetId) |
122
|
|
|
{ |
123
|
6 |
|
$nextYear = new \Datetime('now'); |
124
|
6 |
|
$nextYear->add(new \DateInterval('P1Y')); |
125
|
|
|
|
126
|
|
|
// Set last Tweet Id |
127
|
6 |
|
$cookie = new Cookie('lastTweetId', $firstTweetId, |
128
|
6 |
|
$nextYear->format('U')); |
129
|
|
|
|
130
|
6 |
|
return $cookie; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return RedirectResponse $response |
135
|
|
|
*/ |
136
|
1 |
|
public function resetCookieAction() |
137
|
|
|
{ |
138
|
|
|
/* @see http://www.craftitonline.com/2011/07/symfony2-how-to-set-a-cookie/ */ |
139
|
1 |
|
$response = new RedirectResponse( |
140
|
1 |
|
$this->generateUrl('asynctweets_homepage') |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
// Reset last Tweet Id |
144
|
1 |
|
$cookie = new Cookie('lastTweetId', null); |
145
|
1 |
|
$response->headers->setCookie($cookie); |
146
|
|
|
|
147
|
1 |
|
return $response; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Request $request |
152
|
|
|
* |
153
|
|
|
* @return RedirectResponse $response |
154
|
|
|
*/ |
155
|
2 |
|
public function deleteLessThanAction(Request $request) |
156
|
|
|
{ |
157
|
2 |
|
$lastTweetId = $this->getLastTweetIdFromCookie($request); |
158
|
|
|
|
159
|
2 |
|
if (!is_null($lastTweetId)) { |
160
|
2 |
|
$count = $this->getDoctrine() |
161
|
2 |
|
->getRepository('AsyncTweetsBundle:Tweet') |
162
|
2 |
|
->deleteAndHideTweetsLessThanId($lastTweetId); |
163
|
|
|
|
164
|
2 |
|
$this->get('session')->getFlashBag()->add('message', |
165
|
2 |
|
sprintf('%s tweets deleted.', $count) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
return $this->redirect($this->generateUrl('asynctweets_homepage')); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|