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