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