|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Media; |
|
6
|
|
|
use AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Tweet; |
|
7
|
|
|
use AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\User; |
|
8
|
|
|
|
|
9
|
|
|
class PersistTweet |
|
10
|
|
|
{ |
|
11
|
|
|
private $em; |
|
12
|
|
|
private $displayTable; |
|
13
|
|
|
private $table; |
|
14
|
|
|
|
|
15
|
3 |
|
public function __construct($em, $displayTable, $table) |
|
16
|
|
|
{ |
|
17
|
3 |
|
$this->em = $em; |
|
18
|
3 |
|
$this->displayTable = $displayTable; |
|
19
|
3 |
|
$this->table = $table; |
|
20
|
3 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param \stdClass $userTmp |
|
24
|
|
|
* |
|
25
|
|
|
* @return User |
|
26
|
|
|
*/ |
|
27
|
3 |
|
protected function persistUser(\stdClass $userTmp) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$user = $this->em |
|
30
|
3 |
|
->getRepository('AsyncTweetsBundle:User') |
|
31
|
3 |
|
->findOneById($userTmp->id); |
|
32
|
|
|
|
|
33
|
3 |
|
if (!$user) { |
|
34
|
|
|
// Only set the id when adding the User |
|
35
|
3 |
|
$user = new User($userTmp->id); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Update other fields |
|
39
|
3 |
|
$user->setValues($userTmp); |
|
40
|
|
|
|
|
41
|
3 |
|
$this->em->persist($user); |
|
42
|
|
|
|
|
43
|
3 |
|
return $user; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $medias |
|
48
|
|
|
* @param Tweet $tweet |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function iterateMedias($medias, Tweet $tweet) |
|
51
|
|
|
{ |
|
52
|
3 |
|
foreach ($medias as $mediaTmp) { |
|
53
|
3 |
|
if ($mediaTmp->type == 'photo') { |
|
54
|
3 |
|
$this->persistMedia($tweet, $mediaTmp); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
3 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param \stdClass $tweetTmp |
|
61
|
|
|
* @param Tweet $tweet |
|
62
|
|
|
*/ |
|
63
|
3 |
|
protected function addMedias(\stdClass $tweetTmp, Tweet $tweet) |
|
64
|
|
|
{ |
|
65
|
3 |
|
if ((isset($tweetTmp->entities)) |
|
66
|
3 |
|
&& (isset($tweetTmp->entities->media))) { |
|
67
|
3 |
|
$this->iterateMedias($tweetTmp->entities->media, $tweet); |
|
68
|
|
|
} |
|
69
|
3 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Create a Tweet object and return it. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \stdClass $tweetTmp |
|
75
|
|
|
* @param User $user |
|
76
|
|
|
* @param bool $inTimeline |
|
77
|
|
|
* |
|
78
|
|
|
* @return Tweet |
|
79
|
|
|
*/ |
|
80
|
3 |
|
protected function createTweet(\stdClass $tweetTmp, $user, $inTimeline) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$tweet = new Tweet($tweetTmp->id); |
|
83
|
|
|
|
|
84
|
|
|
$tweet |
|
85
|
3 |
|
->setValues($tweetTmp) |
|
86
|
3 |
|
->setUser($user) |
|
87
|
3 |
|
->setInTimeline($inTimeline); |
|
88
|
|
|
|
|
89
|
3 |
|
$this->addMedias($tweetTmp, $tweet); |
|
90
|
|
|
|
|
91
|
3 |
|
return $tweet; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \stdClass $tweetTmp |
|
96
|
|
|
* @param User $user |
|
97
|
|
|
* @param bool $inTimeline |
|
98
|
|
|
* |
|
99
|
|
|
* @return Tweet |
|
100
|
|
|
*/ |
|
101
|
3 |
|
protected function persistTweet(\stdClass $tweetTmp, User $user, |
|
102
|
|
|
$inTimeline) |
|
103
|
|
|
{ |
|
104
|
3 |
|
$tweet = $this->em |
|
105
|
3 |
|
->getRepository('AsyncTweetsBundle:Tweet') |
|
106
|
3 |
|
->findOneById($tweetTmp->id); |
|
107
|
|
|
|
|
108
|
3 |
|
if (!$tweet) { |
|
109
|
3 |
|
$tweet = $this->createTweet($tweetTmp, $user, $inTimeline); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
if (isset($tweetTmp->retweeted_status)) { |
|
113
|
3 |
|
$retweet = $this->persistRetweetedTweet($tweetTmp); |
|
114
|
3 |
|
$tweet->setRetweetedStatus($retweet); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
$this->em->persist($tweet); |
|
118
|
3 |
|
$this->em->flush(); |
|
119
|
|
|
|
|
120
|
3 |
|
return $tweet; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param \stdClass $tweetTmp |
|
125
|
|
|
* |
|
126
|
|
|
* @return Tweet |
|
127
|
|
|
*/ |
|
128
|
3 |
|
protected function persistRetweetedTweet(\stdClass $tweetTmp) |
|
129
|
|
|
{ |
|
130
|
3 |
|
$retweet = $this->em |
|
131
|
3 |
|
->getRepository('AsyncTweetsBundle:Tweet') |
|
132
|
3 |
|
->findOneById($tweetTmp->retweeted_status->id); |
|
133
|
|
|
|
|
134
|
3 |
|
if (!$retweet) { |
|
135
|
3 |
|
$retweet = $this->addTweet( |
|
136
|
3 |
|
$tweetTmp->retweeted_status |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
3 |
|
return $retweet; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param Tweet $tweet |
|
145
|
|
|
* @param \stdClass $mediaTmp |
|
146
|
|
|
*/ |
|
147
|
3 |
|
protected function persistMedia(Tweet $tweet, \stdClass $mediaTmp) |
|
148
|
|
|
{ |
|
149
|
3 |
|
$media = $this->em |
|
150
|
3 |
|
->getRepository('AsyncTweetsBundle:Media') |
|
151
|
3 |
|
->findOneById($mediaTmp->id); |
|
152
|
|
|
|
|
153
|
3 |
|
if (!$media) { |
|
154
|
|
|
// Only set the id and values when adding the Media |
|
155
|
3 |
|
$media = new Media($mediaTmp->id); |
|
156
|
3 |
|
$media->setValues($mediaTmp); |
|
157
|
3 |
|
$this->em->persist($media); |
|
158
|
3 |
|
$this->em->flush(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
$tweet->addMedia($media); |
|
162
|
3 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param \stdClass $tweetTmp |
|
166
|
|
|
* @param bool $inTimeline |
|
167
|
|
|
* |
|
168
|
|
|
* @return Tweet |
|
169
|
|
|
*/ |
|
170
|
3 |
|
public function addTweet(\stdClass $tweetTmp, $inTimeline = false) |
|
171
|
|
|
{ |
|
172
|
3 |
|
$user = $this->persistUser($tweetTmp->user); |
|
173
|
|
|
|
|
174
|
3 |
|
$tweet = $this->persistTweet($tweetTmp, $user, $inTimeline); |
|
175
|
|
|
|
|
176
|
|
|
// Ignore retweeted tweets |
|
177
|
3 |
|
if ($this->displayTable && $inTimeline) { |
|
178
|
2 |
|
$this->table->addRow([ |
|
179
|
2 |
|
$tweet->getCreatedAt()->format('Y-m-d H:i:s'), |
|
180
|
2 |
|
mb_substr($tweet->getText(), 0, 40), |
|
181
|
2 |
|
$user->getName(), |
|
182
|
|
|
]); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
3 |
|
return $tweet; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|