Overview

Namespaces

  • AlexisLefebvre
    • Bundle
      • AsyncTweetsBundle
        • Command
        • Controller
        • Entity
        • Utils

Classes

  • AlexisLefebvre\Bundle\AsyncTweetsBundle\AsyncTweetsBundle
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\BaseCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesHomeTimelineCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesHomeTimelineTestCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesReadCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesShowCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Controller\DefaultController
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Media
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Tweet
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\TweetRepository
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\User
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Utils\PersistTweet
  • Overview
  • Namespace
  • Class
  • Tree
  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:      * @param \stdClass $userTmp
 24:      *
 25:      * @return User
 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:             // Only set the id when adding the User
 35:             $user = new User($userTmp->id);
 36:         }
 37: 
 38:         // Update other fields
 39:         $user->setValues($userTmp);
 40: 
 41:         $this->em->persist($user);
 42: 
 43:         return $user;
 44:     }
 45: 
 46:     /**
 47:      * @param array $medias
 48:      * @param Tweet $tweet
 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:      * @param \stdClass $tweetTmp
 61:      * @param Tweet     $tweet
 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:      * 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:     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:      * @param \stdClass $tweetTmp
 96:      * @param User      $user
 97:      * @param bool      $inTimeline
 98:      *
 99:      * @return Tweet
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:      * @param \stdClass $tweetTmp
125:      *
126:      * @return Tweet
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:      * @param Tweet     $tweet
145:      * @param \stdClass $mediaTmp
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:             // Only set the id and values when adding the Media
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:      * @param \stdClass $tweetTmp
166:      * @param bool      $inTimeline
167:      *
168:      * @return Tweet
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:         // Ignore retweeted tweets
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: 
AsyncTweetsBundle API documentation generated by ApiGen