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

TweetRepository::getNextTweetId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * TweetRepository
9
 *
10
 * This class was generated by the Doctrine ORM. Add your own custom
11
 * repository methods below.
12
 */
13
class TweetRepository extends EntityRepository
14
{
15
    private $nbTweets = 5;
16
    
17 3
    public function getWithUsers($page = 1)
18
    {
19 3
        $firstResult = (($page - 1) * $this->nbTweets);
20
                
21 3
        $qb = $this->createQueryBuilder('t');
22
        
23
        $query = $qb
24 3
            ->select('t, user, rt, rt_user')
25 3
            ->innerJoin('t.user', 'user')
26 3
            ->leftJoin('t.retweeted_status', 'rt')
27 3
            ->leftJoin('rt.user', 'rt_user')
28
            
29
            // Ignore tweets that were only retweeted
30 3
            ->where($qb->expr()->eq('t.in_timeline', 'true'))
31
            
32 3
            ->orderBy('t.id', 'DESC')
33
            
34 3
            ->setFirstResult($firstResult)
35 3
            ->setMaxResults($this->nbTweets)
36 3
        ;
37
        
38 3
        return $query->getQuery()->getResult();
39
    }
40
    
41
    /**
42
     * @param \Doctrine\ORM\QueryBuilder $qb
43
     */
44 8
    private function getWithUsersAndMediasQuery($qb)
45
    {
46
        $query = $qb
47 8
            ->select('t, user, medias, rt, rt_user')
48 8
            ->innerJoin('t.user', 'user')
49 8
            ->leftJoin('t.medias', 'medias')
50 8
            ->leftJoin('t.retweeted_status', 'rt')
51 8
            ->leftJoin('rt.user', 'rt_user')
52
            
53
            // Ignore tweets that were only retweeted
54 8
            ->where($qb->expr()->eq('t.in_timeline', 'true'))
55
            
56 8
            ->orderBy('t.id', 'ASC')
57
            
58 8
            ->setFirstResult(0)
59 8
            ->setMaxResults($this->nbTweets)
60 8
        ;
61
        
62 8
        return $query;
63
    }
64
    
65 8
    public function getWithUsersAndMedias($firstTweetId = null)
66
    {
67 8
        $qb = $this->createQueryBuilder('t');
68
        
69 8
        $query = $this->getWithUsersAndMediasQuery($qb);
70
        
71 8
        if (! is_null($firstTweetId))
72 8
        {
73 5
            $query = $query->andWhere(
74 5
                $qb->expr()->gte('t.id', $firstTweetId)
75 5
            );
76 5
        }
77
        
78 8
        return $query->getQuery()->getResult();
79
    }
80
    
81
    /**
82
     * @param string $condition
83
     * @param string $order
84
     */
85 6
    private function getTweetId($condition, $order, $tweetId)
86
    {
87 6
        $qb = $this->createQueryBuilder('t')
88 6
            ->select('t.id')
89
            
90 6
            ->where('t.id '.$condition.' :tweetId')
91 6
            ->setParameter(':tweetId', $tweetId)
92
            
93 6
            ->andWhere('t.in_timeline = true')
94
            
95 6
            ->orderBy('t.id', $order)
96
            
97 6
            ->setFirstResult($this->nbTweets - 1)
98 6
            ->setMaxResults(1)
99 6
        ;
100
        
101 6
        $result = $qb->getQuery()->getOneOrNullResult();
102
        
103 6
        return(is_array($result) ? $result['id'] : null);
104
    }
105
    
106 6
    public function getPreviousTweetId($tweetId)
107
    {
108 6
        return($this->getTweetId('<', 'DESC', $tweetId));
109
    }
110
    
111 6
    public function getNextTweetId($tweetId)
112
    {
113 6
        return($this->getTweetId('>', 'ASC', $tweetId));
114
    }
115
    
116 7
    public function countPendingTweets($lastTweetId = null)
117
    {
118 7
        $qb = $this->createQueryBuilder('t');
119
        
120
        $query = $qb
121 7
            ->add('select', $qb->expr()->count('t.id'))
0 ignored issues
show
Documentation introduced by
$qb->expr()->count('t.id') is of type object<Doctrine\ORM\Query\Expr\Func>, but the function expects a object<Doctrine\ORM\Query\Expr\Base>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
            // Ignore tweets that were only retweeted
123 7
            ->where(
124 7
                $qb->expr()->eq('t.in_timeline', 'true')
125 7
            )
126 7
        ;
127
        
128 7
        if (! is_null($lastTweetId))
129 7
        {
130 7
            $query = $query->andWhere(
131 7
                $qb->expr()->gte('t.id', $lastTweetId)
132 7
            );
133 7
        }
134
        
135
        # return result of "COUNT()" query
136 7
        return $query->getQuery()->getSingleScalarResult();
137
    }
138
    
139 7
    public function getLastTweet()
140
    {
141 7
        $qb = $this->createQueryBuilder('t')
142 7
            ->addOrderBy('t.id', 'DESC')
143 7
            ->setFirstResult(0)
144 7
            ->setMaxResults(1)
145 7
        ;
146
        
147 7
        return $qb->getQuery()->getOneOrNullResult();
148
    }
149
    
150
    /**
151
     * @param integer $tweetId
152
     */
153 2
    private function getTweetsLessThanId($tweetId)
154
    {
155 2
        $qb = $this->createQueryBuilder('t')
156 2
            ->select('t, m')
157 2
            ->leftJoin('t.medias', 'm')
158 2
            ->where('t.id < :tweetId')
159 2
            ->setParameter(':tweetId', $tweetId)
160
            
161
            // Get retweeted tweets (it would break foreign keys)
162
            //  http://stackoverflow.com/questions/15087933/how-to-do-left-join-in-doctrine/15088250#15088250
163 2
            ->leftJoin(
164 2
                'AsyncTweetsBundle:Tweet',
165 2
                't2',
166 2
                'WITH',
167
                't.id = t2.retweeted_status'
168 2
            )
169
            
170 2
            ->orderBy('t.id', 'DESC')
171 2
        ;
172
        
173 2
        return($qb->getQuery()->getResult());
174
    }
175
    
176
    /**
177
     * Remove Media not associated to any Tweet
178
     */
179 1
    private function removeOrphanMedias(Media $media)
180
    {
181 1
        if (count($media->getTweets()) == 0)
182 1
        {
183 1
            $this->_em->remove($media);
184 1
        }
185 1
    }
186
    
187
    /**
188
     * Remove the tweet and return 1 is the deleted tweet is not a
189
     *  retweet
190
     * 
191
     * @param Tweet $tweet
192
     * 
193
     * @return integer
194
     */
195 2
    protected function removeTweet($tweet)
196
    {
197 2
        $count = 0;
198
        
199 2
        foreach ($tweet->getMedias() as $media) {
200 1
            $tweet->removeMedia($media);
201 1
            $this->removeOrphanMedias($media);
202 2
        }
203
        
204
        // Don't count tweets that were only retweeted
205 2
        if ($tweet->isInTimeline()) {
206 2
            $count = 1;
207 2
        }
208
        
209 2
        $this->_em->remove($tweet);
210
        
211 2
        return $count;
212
    }
213
    
214
    /**
215
     * Delete tweets and return the number of deleted tweets (excluding
216
     *  retweeted-only tweets)
217
     * 
218
     * @param integer $tweetId
219
     * 
220
     * @return integer
221
     */
222 2
    public function deleteAndHideTweetsLessThanId($tweetId)
223
    {
224 2
        $count = 0;
225
        
226 2
        $tweets = $this->getTweetsLessThanId($tweetId);
227
        
228 2
        foreach ($tweets as $tweet) {
229 2
            if ($tweet->mustBeKept($tweetId)) {
230
                // The Tweet is still in the timeline, it can only be hidden
231 1
                $tweet->setInTimeline(false);
232 1
                $this->_em->persist($tweet);
233 1
             }
234
             else {
235
                 // The Tweet has not been retweeted, it can be removed
236 2
                $count += $this->removeTweet($tweet);
237
            }
238 2
        }
239
        
240 2
        $this->_em->flush();
241
        
242 2
        return($count);
243
    }
244
}
245