Test Failed
Push — master ( c2c60e...83ec37 )
by Alexis
14:02 queued 09:54
created

DefaultController::getLastTweetIdFromCookie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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
// Compatibility layer for Symfony 3.4
14
if (class_exists('Symfony\Bundle\FrameworkBundle\Controller\AbstractController')) {
15
    class_alias('Symfony\Bundle\FrameworkBundle\Controller\AbstractController', 'Symfony\Bundle\FrameworkBundle\Controller\Controller');
16
}
17
18
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 "Symfony\Bundle\FrameworkBundle\Controller\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...
19
{
20
    /** @var TweetRepository */
21
    private $tweetRepository;
22
23
    /**
24
     * @param Request     $request
25
     * @param string|null $firstTweetId
26
     *
27
     * @return \Symfony\Component\HttpFoundation\Response $response
28
     */
29 7
    public function indexAction(Request $request, $firstTweetId = null)
30
    {
31
        /** @var TweetRepository $tweetRepository */
32 7
        $tweetRepository = $this->getDoctrine()
33 7
            ->getRepository('AsyncTweetsBundle:Tweet');
34
35 7
        $this->tweetRepository = $tweetRepository;
36
37 7
        $tweets = $this->tweetRepository
38 7
            ->getWithUsersAndMedias($firstTweetId);
39
40 7
        $variables = $this->getVariables($request, $tweets, $firstTweetId);
41
42 7
        $response = $this->render(
43 7
            'AsyncTweetsBundle:Default:index.html.twig',
44
            [
45 7
                'tweets' => $tweets,
46 7
                'vars'   => $variables,
47
            ]
48
        );
49
50 7
        if (!is_null($variables['cookie'])) {
51 6
            $response->headers->setCookie($variables['cookie']);
52
        }
53
54 7
        return $response;
55
    }
56
57
    /**
58
     * @param Request $request
59
     * @param Tweet[] $tweets
60
     * @param string  $firstTweetId
61
     *
62
     * @return array $vars
63
     */
64 7
    private function getVariables(Request $request, $tweets, $firstTweetId)
65
    {
66
        $vars = [
67 7
            'first'    => $firstTweetId,
68
            'previous' => null,
69
            'next'     => null,
70 7
            'number'   => 0,
71 7
            'cookieId' => $this->getLastTweetIdFromCookie($request),
72
            // No cookie by default
73
            'cookie' => null,
74
        ];
75
76 7
        if (count($tweets) > 0) {
77 6
            $vars = $this->getTweetsVars($tweets, $vars);
78
        }
79
80 7
        return $vars;
81
    }
82
83
    /**
84
     * If a Tweet is displayed, fetch data from repository.
85
     *
86
     * @param Tweet[] $tweets
87
     * @param array   $vars
88
     *
89
     * @return array $vars
90
     */
91 6
    private function getTweetsVars($tweets, $vars)
92
    {
93
        /** @var string $firstTweetId */
94 6
        $firstTweetId = $tweets[0]->getId();
95
96 6
        $vars['previous'] = $this->tweetRepository
97 6
            ->getPreviousTweetId($firstTweetId);
98 6
        $vars['next'] = $this->tweetRepository
99 6
            ->getNextTweetId($firstTweetId);
100
101
        // Only update the cookie if the last Tweet Id is bigger than
102
        //  the one in the cookie
103 6
        if ($firstTweetId > $vars['cookieId']) {
104 6
            $vars['cookie'] = $this->createCookie($firstTweetId);
105 6
            $vars['cookieId'] = $firstTweetId;
106
        }
107
108 6
        $vars['number'] = $this->tweetRepository
109 6
            ->countPendingTweets($vars['cookieId']);
110
111 6
        $vars['first'] = $firstTweetId;
112
113 6
        return $vars;
114
    }
115
116
    /**
117
     * @param Request $request
118
     *
119
     * @return int|null
120
     */
121 7
    private function getLastTweetIdFromCookie(Request $request)
122
    {
123 7
        if ($request->cookies->has('lastTweetId')) {
124
            return $request->cookies->get('lastTweetId');
125
        }
126
        // else
127 7
    }
128
129
    /**
130
     * @param string $firstTweetId
131
     *
132
     * @return Cookie $cookie
133
     */
134 6
    private function createCookie($firstTweetId)
135
    {
136 6
        $nextYear = new \DateTime('now');
137 6
        $nextYear->add(new \DateInterval('P1Y'));
138
139
        // Set last Tweet Id
140 6
        $cookie = new Cookie(
141 6
            'lastTweetId',
142 6
            $firstTweetId,
143 6
            $nextYear->format('U')
144
        );
145
146 6
        return $cookie;
147
    }
148
149
    /**
150
     * @return RedirectResponse $response
151
     */
152
    public function resetCookieAction()
153
    {
154
        /* @see http://www.craftitonline.com/2011/07/symfony2-how-to-set-a-cookie/ */
155
        $response = new RedirectResponse(
156
            $this->generateUrl('asynctweets_homepage')
157
        );
158
159
        // Reset last Tweet Id
160
        $cookie = new Cookie('lastTweetId', null);
161
        $response->headers->setCookie($cookie);
162
163
        return $response;
164
    }
165
166
    /**
167
     * @param Request $request
168
     *
169
     * @return RedirectResponse $response
170
     */
171
    public function deleteLessThanAction(Request $request)
172
    {
173
        $lastTweetId = $this->getLastTweetIdFromCookie($request);
174
175
        if (!is_null($lastTweetId)) {
176
            /** @var TweetRepository $tweetRepository */
177
            $tweetRepository = $this->getDoctrine()
178
                ->getRepository('AsyncTweetsBundle:Tweet');
179
180
            $count = $tweetRepository
181
                ->deleteAndHideTweetsLessThanId($lastTweetId);
182
183
            /** @var Session $session */
184
            $session = $this->get('session');
185
186
            $session->getFlashBag()->add(
187
                'message',
188
                sprintf('%s tweets deleted.', $count)
189
            );
190
        }
191
192
        return $this->redirect($this->generateUrl('asynctweets_homepage'));
193
    }
194
}
195