Completed
Push — master ( ea2a0f...14cc66 )
by Alexis
07:06 queued 04:53
created

src/Controller/DefaultController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Controller;
8
use Symfony\Component\HttpFoundation\Cookie;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Session\Session;
12
13
class DefaultController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
14
{
15
    /** @var TweetRepository */
16
    private $tweetRepository;
17
18
    /**
19
     * @param Request     $request
20
     * @param string|null $firstTweetId
21
     *
22
     * @return \Symfony\Component\HttpFoundation\Response $response
23
     */
24 7
    public function indexAction(Request $request, $firstTweetId = null)
25
    {
26
        /** @var TweetRepository $tweetRepository */
27 7
        $tweetRepository = $this->getDoctrine()
28 7
            ->getRepository('AsyncTweetsBundle:Tweet');
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 6
            $response->headers->setCookie($variables['cookie']);
47
        }
48
49 7
        return $response;
50
    }
51
52
    /**
53
     * @param Request $request
54
     * @param Tweet[] $tweets
55
     * @param string  $firstTweetId
56
     *
57
     * @return array $vars
58
     */
59 7
    private function getVariables(Request $request, $tweets, $firstTweetId)
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 Tweet[] $tweets
82
     * @param array   $vars
83
     *
84
     * @return array $vars
85
     */
86 6
    private function getTweetsVars($tweets, $vars)
87
    {
88
        /** @var string $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 6
        $vars['number'] = $this->tweetRepository
104 6
            ->countPendingTweets($vars['cookieId']);
105
106 6
        $vars['first'] = $firstTweetId;
107
108 6
        return $vars;
109
    }
110
111
    /**
112
     * @param Request $request
113
     *
114
     * @return int|null
115
     */
116 7
    private function getLastTweetIdFromCookie(Request $request)
117
    {
118 7
        if ($request->cookies->has('lastTweetId')) {
119 4
            return $request->cookies->get('lastTweetId');
120
        }
121
        // else
122 7
    }
123
124
    /**
125
     * @param string $firstTweetId
126
     *
127
     * @return Cookie $cookie
128
     */
129 6
    private function createCookie($firstTweetId)
130
    {
131 6
        $nextYear = new \DateTime('now');
132 6
        $nextYear->add(new \DateInterval('P1Y'));
133
134
        // Set last Tweet Id
135 6
        $cookie = new Cookie(
136 6
            'lastTweetId',
137 6
            $firstTweetId,
138 6
            $nextYear->format('U')
139
        );
140
141 6
        return $cookie;
142
    }
143
144
    /**
145
     * @return RedirectResponse $response
146
     */
147 1
    public function resetCookieAction()
148
    {
149
        /* @see http://www.craftitonline.com/2011/07/symfony2-how-to-set-a-cookie/ */
150 1
        $response = new RedirectResponse(
151 1
            $this->generateUrl('asynctweets_homepage')
152
        );
153
154
        // Reset last Tweet Id
155 1
        $cookie = new Cookie('lastTweetId', null);
156 1
        $response->headers->setCookie($cookie);
157
158 1
        return $response;
159
    }
160
161
    /**
162
     * @param Request $request
163
     *
164
     * @return RedirectResponse $response
165
     */
166 2
    public function deleteLessThanAction(Request $request)
167
    {
168 2
        $lastTweetId = $this->getLastTweetIdFromCookie($request);
169
170 2
        if (!is_null($lastTweetId)) {
171
            /** @var TweetRepository $tweetRepository */
172 2
            $tweetRepository = $this->getDoctrine()
173 2
                ->getRepository('AsyncTweetsBundle:Tweet');
174
175
            $count = $tweetRepository
176 2
                ->deleteAndHideTweetsLessThanId($lastTweetId);
177
178
            /** @var Session $session */
179 2
            $session = $this->get('session');
180
181 2
            $session->getFlashBag()->add(
182 2
                'message',
183 2
                sprintf('%s tweets deleted.', $count)
184
            );
185
        }
186
187 2
        return $this->redirect($this->generateUrl('asynctweets_homepage'));
188
    }
189
}
190