Passed
Push — master ( 1bce37...29d889 )
by Alexis
01:34
created

Tweet::getRetweetedStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 int
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 int
29
     */
30
    private $retweet_count = 0;
31
32
    /**
33
     * @var int
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
        }
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
     *
77
     * @return Tweet
78
     */
79 7
    public function setId($id)
80
    {
81 7
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id of type object<AlexisLefebvre\Bu...tsBundle\Entity\bigint> is incompatible with the declared type integer of property $id.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
83 7
        return $this;
84
    }
85
86
    /**
87
     * Get id.
88
     *
89
     * @return int
90
     */
91 9
    public function getId()
92
    {
93 9
        return $this->id;
94
    }
95
96
    /**
97
     * Set created_at.
98
     *
99
     * @param \DateTime $createdAt
100
     *
101
     * @return Tweet
102
     */
103 7
    public function setCreatedAt(\Datetime $createdAt)
104
    {
105 7
        $this->created_at = $createdAt;
106
107 7
        return $this;
108
    }
109
110
    /**
111
     * Get created_at.
112
     *
113
     * @return \DateTime
114
     */
115 10
    public function getCreatedAt()
116
    {
117 10
        return $this->created_at;
118
    }
119
120
    /**
121
     * Set text.
122
     *
123
     * @param string $text
124
     *
125
     * @return Tweet
126
     */
127 7
    public function setText($text)
128
    {
129 7
        $this->text = $text;
130
131 7
        return $this;
132
    }
133
134
    /**
135
     * Get text.
136
     *
137
     * @return string
138
     */
139 11
    public function getText()
140
    {
141 11
        return $this->text;
142
    }
143
144 6
    public function getTextLinkified()
145
    {
146
        /* @see http://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/507459#507459 */
147 6
        return preg_replace(
148 6
            '~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~',
149 6
            '<a href="\\0">\\0</a>',
150 6
            $this->getText()
151
        );
152
    }
153
154
    /**
155
     * Set retweet_count.
156
     *
157
     * @param int $retweetCount
158
     *
159
     * @return Tweet
160
     */
161 7
    public function setRetweetCount($retweetCount)
162
    {
163 7
        $this->retweet_count = $retweetCount;
164
165 7
        return $this;
166
    }
167
168
    /**
169
     * Get retweet_count.
170
     *
171
     * @return int
172
     */
173 8
    public function getRetweetCount()
174
    {
175 8
        return $this->retweet_count;
176
    }
177
178
    /**
179
     * Set favorite_count.
180
     *
181
     * @param int $favoriteCount
182
     *
183
     * @return Tweet
184
     */
185 7
    public function setFavoriteCount($favoriteCount)
186
    {
187 7
        $this->favorite_count = $favoriteCount;
188
189 7
        return $this;
190
    }
191
192
    /**
193
     * Get favorite_count.
194
     *
195
     * @return int
196
     */
197 8
    public function getFavoriteCount()
198
    {
199 8
        return $this->favorite_count;
200
    }
201
202
    /**
203
     * Set user.
204
     *
205
     * @param User $user
206
     *
207
     * @return Tweet
208
     */
209 7
    public function setUser(User $user)
210
    {
211 7
        $this->user = $user;
212 7
        $this->user->addTweet($this);
213
214 7
        return $this;
215
    }
216
217
    /**
218
     * Get User.
219
     *
220
     * @return User
221
     */
222 9
    public function getUser()
223
    {
224 9
        return $this->user;
225
    }
226
227
    /**
228
     * Set in timeline.
229
     *
230
     * @param bool $inTimeline
231
     *
232
     * @return Tweet
233
     */
234 7
    public function setInTimeline($inTimeline)
235
    {
236 7
        $this->in_timeline = $inTimeline;
237
238 7
        return $this;
239
    }
240
241
    /**
242
     * Get in timeline.
243
     *
244
     * @return bool
245
     */
246 3
    public function isInTimeline()
247
    {
248 3
        return $this->in_timeline;
249
    }
250
251
    /**
252
     * Set retweeted
253
     * "This attribute contains a representation of the original Tweet
254
     *  that was retweeted.".
255
     *
256
     * @param self $retweetedStatus
0 ignored issues
show
Documentation introduced by
Should the type for parameter $retweetedStatus not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
257
     *
258
     * @return Tweet
259
     */
260 6
    public function setRetweetedStatus(self $retweetedStatus)
261
    {
262 6
        $this->retweeted_status = $retweetedStatus;
0 ignored issues
show
Documentation Bug introduced by
It seems like $retweetedStatus of type object<self> is incompatible with the declared type object<AlexisLefebvre\Bu...etsBundle\Entity\Tweet> of property $retweeted_status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
263
264 6
        return $this;
265
    }
266
267
    /**
268
     * Get retweeted status.
269
     *
270
     * @return Tweet
271
     */
272 7
    public function getRetweetedStatus()
273
    {
274 7
        return $this->retweeted_status;
275
    }
276
277
    /**
278
     * Get medias.
279
     *
280
     * @return ArrayCollection
281
     */
282 6
    public function getMedias()
283
    {
284 6
        return $this->medias;
285
    }
286
287
    /**
288
     * Add a media.
289
     *
290
     * @param Media $media
291
     *
292
     * @return Tweet
293
     */
294 6
    public function addMedia(Media $media)
295
    {
296 6
        $this->medias->add($media);
297 6
        $media->addTweet($this);
298
299 6
        return $this;
300
    }
301
302
    /**
303
     * Remove a media.
304
     *
305
     * @param Media $media
306
     *
307
     * @return Tweet
308
     */
309 1
    public function removeMedia(Media $media)
310
    {
311 1
        $this->medias->removeElement($media);
312 1
        $media->removeTweet($this);
313
314 1
        return $this;
315
    }
316
317
    /**
318
     * Get retweeting status.
319
     *
320
     * @return ArrayCollection
321
     */
322 2
    public function getRetweetingStatuses()
323
    {
324 2
        return $this->retweeting_statuses;
325
    }
326
327
    /**
328
     * Call setter functions.
329
     *
330
     * @param \stdClass $tweetTmp
331
     *
332
     * @return Tweet
333
     */
334 3
    public function setValues(\stdClass $tweetTmp)
335
    {
336
        $this
337 3
            ->setCreatedAt(new \Datetime($tweetTmp->created_at))
338 3
            ->setText($tweetTmp->text)
339 3
            ->setRetweetCount($tweetTmp->retweet_count)
340 3
            ->setFavoriteCount($tweetTmp->favorite_count);
341
342 3
        return $this;
343
    }
344
345
    /**
346
     * Check that tweet can be deleted.
347
     *
348
     * @param int $tweetId
349
     *
350
     * @return bool
351
     */
352 2
    public function mustBeKept($tweetId)
353
    {
354 2
        if (count($this->getRetweetingStatuses()) == 0) {
355
            // This tweet has not been retweeted
356 2
            return false;
357
        }
358
359
        // Check that this tweet has not been retweeted after $tweetId
360 1
        foreach ($this->getRetweetingStatuses() as $retweeting_status) {
361
            // This tweet is retweeted in the timeline, keep it
362 1
            if ($retweeting_status->getId() >= $tweetId) {
363 1
                return true;
364
            }
365
        }
366
367 1
        return false;
368
    }
369
}
370