1: <?php
2:
3: namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity;
4:
5: use Doctrine\ORM\EntityRepository;
6:
7: 8: 9: 10: 11: 12:
13: class TweetRepository extends EntityRepository
14: {
15: private $nbTweets = 5;
16:
17: public function getWithUsers($page = 1)
18: {
19: $firstResult = (($page - 1) * $this->nbTweets);
20:
21: $qb = $this->createQueryBuilder('t');
22:
23: $query = $qb
24: ->select('t, user, rt, rt_user')
25: ->innerJoin('t.user', 'user')
26: ->leftJoin('t.retweeted_status', 'rt')
27: ->leftJoin('rt.user', 'rt_user')
28:
29:
30: ->where($qb->expr()->eq('t.in_timeline', 'true'))
31:
32: ->orderBy('t.id', 'DESC')
33:
34: ->setFirstResult($firstResult)
35: ->setMaxResults($this->nbTweets);
36:
37: return $query->getQuery()->getResult();
38: }
39:
40: 41: 42: 43: 44:
45: private function getWithUsersAndMediasQuery($qb)
46: {
47: $query = $qb
48: ->select('t, user, medias, rt, rt_user')
49: ->innerJoin('t.user', 'user')
50: ->leftJoin('t.medias', 'medias')
51: ->leftJoin('t.retweeted_status', 'rt')
52: ->leftJoin('rt.user', 'rt_user')
53:
54:
55: ->where($qb->expr()->eq('t.in_timeline', 'true'))
56:
57: ->orderBy('t.id', 'ASC')
58:
59: ->setFirstResult(0)
60: ->setMaxResults($this->nbTweets);
61:
62: return $query;
63: }
64:
65: public function getWithUsersAndMedias($firstTweetId = null)
66: {
67: $qb = $this->createQueryBuilder('t');
68:
69: $query = $this->getWithUsersAndMediasQuery($qb);
70:
71: if (!is_null($firstTweetId)) {
72: $query = $query->andWhere(
73: $qb->expr()->gte('t.id', $firstTweetId)
74: );
75: }
76:
77: return $query->getQuery()->getResult();
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: private function getTweetId($condition, $order, $tweetId)
88: {
89: $qb = $this->createQueryBuilder('t')
90: ->select('t.id')
91:
92: ->where('t.id '.$condition.' :tweetId')
93: ->setParameter(':tweetId', $tweetId)
94:
95: ->andWhere('t.in_timeline = true')
96:
97: ->orderBy('t.id', $order)
98:
99: ->setFirstResult($this->nbTweets - 1)
100: ->setMaxResults(1);
101:
102: $result = $qb->getQuery()->getOneOrNullResult();
103:
104: return is_array($result) ? $result['id'] : null;
105: }
106:
107: public function getPreviousTweetId($tweetId)
108: {
109: return $this->getTweetId('<', 'DESC', $tweetId);
110: }
111:
112: public function getNextTweetId($tweetId)
113: {
114: return $this->getTweetId('>', 'ASC', $tweetId);
115: }
116:
117: public function countPendingTweets($lastTweetId = null)
118: {
119:
120: $qb = $this->createQueryBuilder('t');
121:
122: $query = $qb
123: ->add('select', $qb->expr()->count('t.id'))
124:
125: ->where(
126: $qb->expr()->eq('t.in_timeline', 'true')
127: );
128:
129: if (!is_null($lastTweetId)) {
130: $query = $query->andWhere(
131: $qb->expr()->gte('t.id', $lastTweetId)
132: );
133: }
134:
135:
136: return $query->getQuery()->getSingleScalarResult();
137: }
138:
139: public function getLastTweet()
140: {
141: $qb = $this->createQueryBuilder('t')
142: ->addOrderBy('t.id', 'DESC')
143: ->setFirstResult(0)
144: ->setMaxResults(1);
145:
146: return $qb->getQuery()->getOneOrNullResult();
147: }
148:
149: 150: 151: 152: 153:
154: private function getTweetsLessThanId($tweetId)
155: {
156: $qb = $this->createQueryBuilder('t')
157: ->select('t, m')
158: ->leftJoin('t.medias', 'm')
159: ->where('t.id < :tweetId')
160: ->setParameter(':tweetId', $tweetId)
161:
162:
163:
164: ->leftJoin(
165: 'AsyncTweetsBundle:Tweet',
166: 't2',
167: 'WITH',
168: 't.id = t2.retweeted_status'
169: )
170:
171: ->orderBy('t.id', 'DESC');
172:
173: return $qb->getQuery()->getResult();
174: }
175:
176: 177: 178: 179: 180:
181: private function removeOrphanMedias(Media $media)
182: {
183: if (count($media->getTweets()) == 0) {
184: $this->_em->remove($media);
185: }
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195:
196: protected function removeTweet($tweet)
197: {
198: $count = 0;
199:
200: foreach ($tweet->getMedias() as $media) {
201: $tweet->removeMedia($media);
202: $this->removeOrphanMedias($media);
203: }
204:
205:
206: if ($tweet->isInTimeline()) {
207: $count = 1;
208: }
209:
210: $this->_em->remove($tweet);
211:
212: return $count;
213: }
214:
215: 216: 217: 218: 219: 220: 221: 222:
223: public function deleteAndHideTweetsLessThanId($tweetId)
224: {
225: $count = 0;
226:
227: $tweets = $this->getTweetsLessThanId($tweetId);
228:
229: foreach ($tweets as $tweet) {
230: if ($tweet->mustBeKept($tweetId)) {
231:
232: $tweet->setInTimeline(false);
233: $this->_em->persist($tweet);
234: } else {
235:
236: $count += $this->removeTweet($tweet);
237: }
238: }
239:
240: $this->_em->flush();
241:
242: return $count;
243: }
244: }
245: