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
|
|
|
use Doctrine\Persistence\ObjectManager; |
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
10
|
|
|
|
11
|
|
|
class PersistTweet |
12
|
|
|
{ |
13
|
|
|
/** @var ObjectManager */ |
14
|
|
|
private $em; |
15
|
|
|
/** @var bool */ |
16
|
|
|
private $displayTable; |
17
|
|
|
/** @var Table|null */ |
18
|
|
|
private $table; |
19
|
|
|
|
20
|
3 |
|
public function __construct(ObjectManager $em, bool $displayTable, ?Table $table) |
21
|
|
|
{ |
22
|
3 |
|
$this->em = $em; |
23
|
3 |
|
$this->displayTable = $displayTable; |
24
|
3 |
|
$this->table = $table; |
25
|
3 |
|
} |
26
|
|
|
|
27
|
3 |
|
protected function persistUser(\stdClass $userTmp): User |
28
|
|
|
{ |
29
|
3 |
|
$user = $this->em |
30
|
3 |
|
->getRepository(User::class) |
31
|
3 |
|
->findOneBy(['id' => $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<\stdClass> $medias |
48
|
|
|
*/ |
49
|
3 |
|
public function iterateMedias(array $medias, Tweet $tweet): void |
50
|
|
|
{ |
51
|
3 |
|
foreach ($medias as $mediaTmp) { |
52
|
3 |
|
if ($mediaTmp->type == 'photo') { |
53
|
3 |
|
$this->persistMedia($tweet, $mediaTmp); |
54
|
|
|
} |
55
|
|
|
} |
56
|
3 |
|
} |
57
|
|
|
|
58
|
3 |
|
protected function addMedias(\stdClass $tweetTmp, Tweet $tweet): void |
59
|
|
|
{ |
60
|
3 |
|
if ((isset($tweetTmp->entities)) |
61
|
3 |
|
&& (isset($tweetTmp->entities->media))) { |
62
|
3 |
|
$this->iterateMedias($tweetTmp->entities->media, $tweet); |
63
|
|
|
} |
64
|
3 |
|
} |
65
|
|
|
|
66
|
3 |
|
protected function createTweet(\stdClass $tweetTmp, User $user, bool $inTimeline): Tweet |
67
|
|
|
{ |
68
|
3 |
|
$tweet = new Tweet(); |
69
|
|
|
|
70
|
|
|
$tweet |
71
|
3 |
|
->setId($tweetTmp->id) |
72
|
3 |
|
->setValues($tweetTmp) |
73
|
3 |
|
->setUser($user) |
74
|
3 |
|
->setInTimeline($inTimeline); |
75
|
|
|
|
76
|
3 |
|
$this->addMedias($tweetTmp, $tweet); |
77
|
|
|
|
78
|
3 |
|
return $tweet; |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
protected function persistTweet( |
82
|
|
|
\stdClass $tweetTmp, |
83
|
|
|
User $user, |
84
|
|
|
bool $inTimeline |
85
|
|
|
): Tweet { |
86
|
|
|
/** @var Tweet|null $tweet */ |
87
|
3 |
|
$tweet = $this->em |
88
|
3 |
|
->getRepository(Tweet::class) |
89
|
3 |
|
->findOneBy(['id' => $tweetTmp->id]); |
90
|
|
|
|
91
|
3 |
|
if (!$tweet) { |
92
|
3 |
|
$tweet = $this->createTweet($tweetTmp, $user, $inTimeline); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
if (isset($tweetTmp->retweeted_status)) { |
96
|
3 |
|
$retweet = $this->persistRetweetedTweet($tweetTmp); |
97
|
3 |
|
$tweet->setRetweetedStatus($retweet); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
$this->em->persist($tweet); |
101
|
3 |
|
$this->em->flush(); |
102
|
|
|
|
103
|
3 |
|
return $tweet; |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
protected function persistRetweetedTweet(\stdClass $tweetTmp): Tweet |
107
|
|
|
{ |
108
|
3 |
|
$retweet = $this->em |
109
|
3 |
|
->getRepository(Tweet::class) |
110
|
3 |
|
->findOneBy(['id' => $tweetTmp->retweeted_status->id]); |
111
|
|
|
|
112
|
3 |
|
if (!$retweet) { |
113
|
3 |
|
$retweet = $this->addTweet( |
114
|
3 |
|
$tweetTmp->retweeted_status |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
return $retweet; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
protected function persistMedia(Tweet $tweet, \stdClass $mediaTmp): void |
122
|
|
|
{ |
123
|
3 |
|
$media = $this->em |
124
|
3 |
|
->getRepository(Media::class) |
125
|
3 |
|
->findOneBy(['id' => $mediaTmp->id]); |
126
|
|
|
|
127
|
3 |
|
if (!$media) { |
128
|
|
|
// Only set the id and values when adding the Media |
129
|
3 |
|
$media = new Media($mediaTmp->id); |
130
|
3 |
|
$media->setValues($mediaTmp); |
131
|
3 |
|
$this->em->persist($media); |
132
|
3 |
|
$this->em->flush(); |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
$tweet->addMedia($media); |
136
|
3 |
|
} |
137
|
|
|
|
138
|
3 |
|
public function addTweet(\stdClass $tweetTmp, bool $inTimeline = false): Tweet |
139
|
|
|
{ |
140
|
3 |
|
$user = $this->persistUser($tweetTmp->user); |
141
|
|
|
|
142
|
3 |
|
$tweet = $this->persistTweet($tweetTmp, $user, $inTimeline); |
143
|
|
|
|
144
|
|
|
// Ignore retweeted tweets |
145
|
3 |
|
if ($this->displayTable && $inTimeline && $this->table instanceof Table) { |
146
|
2 |
|
$this->table->addRow([ |
147
|
2 |
|
$tweet->getCreatedAt()->format('Y-m-d H:i:s'), |
148
|
2 |
|
mb_substr($tweet->getText(), 0, 40), |
149
|
2 |
|
$user->getName(), |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
3 |
|
return $tweet; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: