1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
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 $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
|
|
|
array( |
33
|
7 |
|
'tweets' => $tweets, |
34
|
7 |
|
'vars' => $variables, |
35
|
|
|
) |
36
|
7 |
|
); |
37
|
|
|
|
38
|
7 |
|
if (! is_null($variables['cookie'])) { |
39
|
6 |
|
$response->headers->setCookie($variables['cookie']); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
7 |
|
return $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param Tweets[] $tweets |
48
|
|
|
* @param integer $firstTweetId |
49
|
|
|
* |
50
|
|
|
* @return array $vars |
51
|
|
|
*/ |
52
|
7 |
|
private function getVariables(Request $request, $tweets, $firstTweetId) |
53
|
|
|
{ |
54
|
|
|
$vars = array( |
55
|
7 |
|
'first' => $firstTweetId, |
56
|
7 |
|
'previous' => null, |
57
|
7 |
|
'next' => null, |
58
|
7 |
|
'number' => 0, |
59
|
7 |
|
'cookieId' => $this->getLastTweetIdFromCookie($request), |
60
|
|
|
# No cookie by default |
61
|
7 |
|
'cookie' => null, |
62
|
7 |
|
); |
63
|
|
|
|
64
|
7 |
|
if (count($tweets) > 0) { |
65
|
6 |
|
$vars = $this->getTweetsVars($tweets, $vars); |
66
|
6 |
|
} |
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
|
6 |
|
} |
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
|
|
|
* @return integer|null |
106
|
|
|
*/ |
107
|
7 |
|
private function getLastTweetIdFromCookie(Request $request) |
108
|
|
|
{ |
109
|
7 |
|
if ($request->cookies->has('lastTweetId')) { |
110
|
4 |
|
return($request->cookies->get('lastTweetId')); |
111
|
|
|
} |
112
|
|
|
// else |
113
|
7 |
|
return(null); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $firstTweetId |
118
|
|
|
* @return Cookie $cookie |
119
|
|
|
*/ |
120
|
6 |
|
private function createCookie($firstTweetId) |
121
|
|
|
{ |
122
|
6 |
|
$nextYear = new \Datetime('now'); |
123
|
6 |
|
$nextYear->add(new \DateInterval('P1Y')); |
124
|
|
|
|
125
|
|
|
# Set last Tweet Id |
126
|
6 |
|
$cookie = new Cookie('lastTweetId', $firstTweetId, |
127
|
6 |
|
$nextYear->format('U')); |
128
|
|
|
|
129
|
6 |
|
return($cookie); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return RedirectResponse $response |
134
|
|
|
*/ |
135
|
1 |
|
public function resetCookieAction() |
136
|
|
|
{ |
137
|
|
|
/** @see http://www.craftitonline.com/2011/07/symfony2-how-to-set-a-cookie/ */ |
138
|
1 |
|
$response = new RedirectResponse( |
139
|
1 |
|
$this->generateUrl('asynctweets_homepage') |
140
|
1 |
|
); |
141
|
|
|
|
142
|
|
|
# Reset last Tweet Id |
143
|
1 |
|
$cookie = new Cookie('lastTweetId', null); |
144
|
1 |
|
$response->headers->setCookie($cookie); |
145
|
|
|
|
146
|
1 |
|
return $response; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Request $request |
151
|
|
|
* @return RedirectResponse $response |
152
|
|
|
*/ |
153
|
2 |
|
public function deleteLessThanAction(Request $request) |
154
|
|
|
{ |
155
|
2 |
|
$lastTweetId = $this->getLastTweetIdFromCookie($request); |
156
|
|
|
|
157
|
2 |
|
if (! is_null($lastTweetId)) { |
158
|
2 |
|
$count = $this->getDoctrine() |
159
|
2 |
|
->getRepository('AsyncTweetsBundle:Tweet') |
160
|
2 |
|
->deleteAndHideTweetsLessThanId($lastTweetId); |
161
|
|
|
|
162
|
2 |
|
$this->get('session')->getFlashBag()->add('message', |
163
|
2 |
|
sprintf('%s tweets deleted.', $count) |
164
|
2 |
|
); |
165
|
2 |
|
} |
166
|
|
|
|
167
|
2 |
|
return $this->redirect($this->generateUrl('asynctweets_homepage')); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|