Completed
Push — master ( 58ab73...98607e )
by Alexis
05:27
created

DefaultController::getVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4286
cc 2
eloc 11
nc 2
nop 2
crap 2
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 string|null $firstTweetId
16
     * 
17
     * @return \Symfony\Component\HttpFoundation\Response $response $response
18
     */
19 7
    public function indexAction($firstTweetId = null)
20
    {
21 7
        $this->tweetRepository = $this->getDoctrine()
22 7
            ->getRepository('AsyncTweetsBundle:Tweet');
23
        
24 7
        $tweets = $this->tweetRepository
25 7
            ->getWithUsersAndMedias($firstTweetId);
26
        
27 7
        $variables = $this->getVariables($tweets, $firstTweetId);
28
        
29 7
        $response = $this->render(
30 7
            'AsyncTweetsBundle:Default:index.html.twig',
31
            array(
32 7
                'tweets' => $tweets,
33 7
                'vars' => $variables,
34
            )
35 7
        );
36
        
37 7
        if (! is_null($variables['cookie'])) {
38 6
            $response->headers->setCookie($variables['cookie']);
39 6
        }
40
        
41 7
        return $response;
42
    }
43
    
44
    /**
45
     * @param Tweets[] $tweets
46
     * @param integer $firstTweetId
47
     * 
48
     * @return array $vars
49
     */
50 7
    private function getVariables($tweets, $firstTweetId)
51
    {
52
        $vars = array(
53 7
            'first' => $firstTweetId,
54 7
            'previous' => null,
55 7
            'next' => null,
56 7
            'number' => 0,
57
            # No cookie by default
58 7
            'cookieId' => null,
59 7
            'cookie' => null,
60 7
        );
61
        
62 7
        if (count($tweets) > 0) {
63 6
            $vars = $this->getTweetsVars($tweets, $vars);
64 6
        }
65
        
66 7
        return($vars);
67
    }
68
    
69
    /**
70
     * If a Tweet is displayed, fetch data from repository
71
     * 
72
     * @param Tweets[] $tweets
73
     * @param array $vars
74
     * 
75
     * @return array $vars
76
     */
77 6
    private function getTweetsVars($tweets, $vars)
78
    {
79 6
        $firstTweetId = $tweets[0]->getId();
80
        
81 6
        $vars['previous'] = $this->tweetRepository
82 6
            ->getPreviousTweetId($firstTweetId);
83 6
        $vars['next'] = $this->tweetRepository
84 6
            ->getNextTweetId($firstTweetId);
85
        
86
        # Only update the cookie if the last Tweet Id is bigger than
87
        #  the one in the cookie
88 6
        if ($firstTweetId > $vars['cookieId']) {
89 6
            $vars['cookie'] = $this->getCookie($firstTweetId);
90 6
            $vars['cookieId'] = $firstTweetId;
91 6
        }    
92
        
93 6
        $vars['number'] = $this->tweetRepository
94 6
            ->countPendingTweets($vars['cookieId']);
95
        
96 6
        $vars['first'] = $firstTweetId;
97
        
98 6
        return($vars);
99
    }
100
    
101
    /**
102
     * @param Request $request
103
     * @return integer|null
104
     */
105 2
    private function getLastTweetIdFromCookie(Request $request)
106
    {
107 2
        if ($request->cookies->has('lastTweetId')) {
108 2
            return($request->cookies->get('lastTweetId'));
109
        }
110
        // else
111
        return(null);
112
    }
113
    
114
    /**
115
     * @param string $firstTweetId
116
     * @return Cookie $cookie
117
     */
118 6
    private function getCookie($firstTweetId)
119
    {
120 6
        $nextYear = new \Datetime('now');
121 6
        $nextYear->add(new \DateInterval('P1Y'));
122
        
123
        # Set last Tweet Id
124 6
        $cookie = new Cookie('lastTweetId', $firstTweetId,
125 6
            $nextYear->format('U'));
126
        
127 6
        return($cookie);
128
    }
129
    
130
    /**
131
     * @return RedirectResponse $response
132
     */
133 1
    public function resetCookieAction()
134
    {
135
        /** @see http://www.craftitonline.com/2011/07/symfony2-how-to-set-a-cookie/ */
136 1
        $response = new RedirectResponse(
137 1
            $this->generateUrl('asynctweets_homepage')
138 1
        );
139
        
140
        # Reset last Tweet Id
141 1
        $cookie = new Cookie('lastTweetId', null);
142 1
        $response->headers->setCookie($cookie);
143
        
144 1
        return $response;
145
    }
146
    
147
    /**
148
     * @param Request $request
149
     * @return RedirectResponse $response
150
     */
151 2
    public function deleteLessThanAction(Request $request)
152
    {
153 2
        $lastTweetId = $this->getLastTweetIdFromCookie($request);
154
        
155 2
        if ($lastTweetId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lastTweetId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
156 2
            $count = $this->getDoctrine()
157 2
                ->getRepository('AsyncTweetsBundle:Tweet')
158 2
                ->deleteAndHideTweetsLessThanId($lastTweetId);
159
            
160 2
            $this->get('session')->getFlashBag()->add('message',
161 2
                sprintf('%s tweets deleted.', $count)
162 2
            );
163 2
        }
164
        
165 2
        return $this->redirect($this->generateUrl('asynctweets_homepage'));
166
    }
167
}
168