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