Completed
Push — master ( e75a05...1230d5 )
by Alexis
07:31
created

Tweet::mustBeKept()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 1
crap 4
1
<?php
2
3
namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
/**
8
 * Tweet
9
 */
10
class Tweet
11
{
12
    /**
13
     * @var bigint
14
     */
15
    private $id;
16
    
17
    /**
18
     * @var \DateTime
19
     */
20
    private $created_at;
21
    
22
    /**
23
     * @var string
24
     */
25
    private $text;
26
    
27
    /**
28
     * @var integer
29
     */
30
    private $retweet_count = 0;
31
    
32
    /**
33
     * @var integer
34
     */
35
    private $favorite_count = 0;
36
    
37
    /**
38
     * @var User
39
     */
40
    private $user;
41
    
42
    /**
43
     * In timeline: false for retweeted Tweets
44
     */
45
    private $in_timeline = false;
46
    
47
    /**
48
     * @var Tweet
49
     */
50
    private $retweeted_status = null;
51
    
52
    /**
53
     * @var ArrayCollection
54
     */
55
    private $retweeting_statuses;
56
    
57
    /**
58
     * @var ArrayCollection
59
     */
60
    private $medias;
61
    
62 7
    public function __construct($id = null)
63
    {
64 7
        if (! is_null($id)) {
65 7
            $this->setId($id);
66 7
        }
67
        
68 7
        $this->medias = new ArrayCollection();
69 7
        $this->retweeting_statuses = new ArrayCollection();
70 7
    }
71
    
72
    /**
73
     * Set id
74
     *
75
     * @param bigint $id
76
     * @return Tweet 
77
     */
78 7
    public function setId($id)
79
    {
80 7
        $this->id = $id;
81
        
82 7
        return $this;
83
    }
84
    
85
    /**
86
     * Get id
87
     *
88
     * @return integer 
89
     */
90 9
    public function getId()
91
    {
92 9
        return $this->id;
93
    }
94
    
95
    /**
96
     * Set created_at
97
     *
98
     * @param \DateTime $createdAt
99
     * @return Tweet
100
     */
101 7
    public function setCreatedAt(\Datetime $createdAt)
102
    {
103 7
        $this->created_at = $createdAt;
104
105 7
        return $this;
106
    }
107
    
108
    /**
109
     * Get created_at
110
     *
111
     * @return \DateTime 
112
     */
113 10
    public function getCreatedAt()
114
    {
115 10
        return $this->created_at;
116
    }
117
    
118
    /**
119
     * Set text
120
     *
121
     * @param string $text
122
     * @return Tweet
123
     */
124 7
    public function setText($text)
125
    {
126 7
        $this->text = $text;
127
128 7
        return $this;
129
    }
130
    
131
    /**
132
     * Get text
133
     *
134
     * @return string 
135
     */
136 10
    public function getText()
137
    {
138 10
        return $this->text;
139
    }
140
    
141 6
    public function getTextLinkified()
142
    {
143
        /** @see http://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/507459#507459 */
144 6
        return preg_replace(
145 6
            "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
146 6
            "<a href=\"\\0\">\\0</a>", 
147 6
            $this->getText()
148 6
        );
149
    }
150
    
151
    /**
152
     * Set retweet_count
153
     *
154
     * @param integer $retweetCount
155
     * @return Tweet
156
     */
157 7
    public function setRetweetCount($retweetCount)
158
    {
159 7
        $this->retweet_count = $retweetCount;
160
161 7
        return $this;
162
    }
163
    
164
    /**
165
     * Get retweet_count
166
     *
167
     * @return integer 
168
     */
169 8
    public function getRetweetCount()
170
    {
171 8
        return $this->retweet_count;
172
    }
173
    
174
    /**
175
     * Set favorite_count
176
     *
177
     * @param integer $favoriteCount
178
     * @return Tweet
179
     */
180 7
    public function setFavoriteCount($favoriteCount)
181
    {
182 7
        $this->favorite_count = $favoriteCount;
183
184 7
        return $this;
185
    }
186
    
187
    /**
188
     * Get favorite_count
189
     *
190
     * @return integer
191
     */
192 8
    public function getFavoriteCount()
193
    {
194 8
        return $this->favorite_count;
195
    }
196
    
197
    /**
198
     * Set user
199
     *
200
     * @param User $user
201
     * @return Tweet
202
     */
203 7
    public function setUser(User $user)
204
    {
205 7
        $this->user = $user;
206 7
        $this->user->addTweet($this);
207
208 7
        return $this;
209
    }
210
    
211
    /**
212
     * Get User
213
     *
214
     * @return User
215
     */
216 8
    public function getUser()
217
    {
218 8
        return $this->user;
219
    }
220
    
221
    /**
222
     * Set in timeline
223
     *
224
     * @param boolean $inTimeline
225
     * @return Tweet
226
     */
227 7
    public function setInTimeline($inTimeline)
228
    {
229 7
        $this->in_timeline = $inTimeline;
230
        
231 7
        return $this;
232
    }
233
    
234
    /**
235
     * Get in timeline
236
     *
237
     * @return boolean
238
     */
239 3
    public function isInTimeline()
240
    {
241 3
        return $this->in_timeline;
242
    }
243
    
244
    /**
245
     * Set retweeted
246
     * "This attribute contains a representation of the original Tweet
247
     *  that was retweeted."
248
     *
249
     * @param Tweet $retweetedStatus
250
     * @return Tweet
251
     */
252 6
    public function setRetweetedStatus(Tweet $retweetedStatus)
253
    {
254 6
        $this->retweeted_status = $retweetedStatus;
255
        
256 6
        return $this;
257
    }
258
    
259
    /**
260
     * Get retweeted status
261
     *
262
     * @return Tweet
263
     */
264 7
    public function getRetweetedStatus()
265
    {
266 7
        return $this->retweeted_status;
267
    }
268
    
269
    /**
270
     * Get medias
271
     *
272
     * @return ArrayCollection 
273
     */
274 6
    public function getMedias()
275
    {
276 6
        return $this->medias;
277
    }
278
    
279
    /**
280
     * Add a media
281
     *
282
     * @return Tweet
283
     */
284 6
    public function addMedia(Media $media)
285
    {
286 6
        $media->addTweet($this);
287 6
        $this->medias[] = $media;
288
        
289 6
        return $this;
290
    }
291
    
292
    /**
293
     * Remove a media
294
     *
295
     * @return Tweet
296
     */
297 1
    public function removeMedia(Media $media)
298
    {
299 1
        $this->medias->removeElement($media);
300 1
        $media->removeTweet($this);
301
        
302 1
        return $this;
303
    }
304
    
305
    /**
306
     * Get retweeting status
307
     *
308
     * @return ArrayCollection 
309
     */
310 2
    public function getRetweetingStatuses()
311
    {
312 2
        return $this->retweeting_statuses;
313
    }
314
    
315
    /**
316
     * Call setter functions
317
     * 
318
     * @param \stdClass $tweetTmp
319
     */
320 3
    public function setValues(\stdClass $tweetTmp)
321
    {
322 3
        $this
323 3
            ->setCreatedAt(new \Datetime($tweetTmp->created_at))
324 3
            ->setText($tweetTmp->text)
325 3
            ->setRetweetCount($tweetTmp->retweet_count)
326 3
            ->setFavoriteCount($tweetTmp->favorite_count)
327
        ;
328
        
329 3
        return $this;
330
    }
331
    
332
    /**
333
     * Check that tweet can be deleted
334
     * 
335
     * @param integer $tweetId
336
     * 
337
     * @return boolean
338
     */
339 2
    public function mustBeKept($tweetId)
340
    {
341 2
        if (count($this->getRetweetingStatuses()) == 0) {
342
            // This tweet has not been retweeted
343 2
            return(false);
344
        }
345
        
346
        // Check that this tweet has not been retweeted after $tweetId
347 1
        foreach ($this->getRetweetingStatuses() as $retweeting_status) {
348
            // This tweet is retweeted in the timeline, keep it
349 1
            if ($retweeting_status->getId() >= $tweetId) {
350 1
                return(true);
351
            }
352 1
        }
353
        
354 1
        return(false);
355
    }
356
}
357