Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
63 / 63 |
| PersistTweet | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
21 | |
100.00% |
63 / 63 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| persistUser | |
100.00% |
1 / 1 |
2 | |
100.00% |
8 / 8 |
|||
| iterateMedias | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
|||
| addMedias | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
|||
| createTweet | |
100.00% |
1 / 1 |
1 | |
100.00% |
7 / 7 |
|||
| persistTweet | |
100.00% |
1 / 1 |
3 | |
100.00% |
11 / 11 |
|||
| persistRetweetedTweet | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| persistMedia | |
100.00% |
1 / 1 |
2 | |
100.00% |
10 / 10 |
|||
| addTweet | |
100.00% |
1 / 1 |
4 | |
100.00% |
8 / 8 |
|||
| 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 | public function __construct(ObjectManager $em, bool $displayTable, ?Table $table) |
| 21 | { |
| 22 | $this->em = $em; |
| 23 | $this->displayTable = $displayTable; |
| 24 | $this->table = $table; |
| 25 | } |
| 26 | |
| 27 | protected function persistUser(\stdClass $userTmp): User |
| 28 | { |
| 29 | $user = $this->em |
| 30 | ->getRepository(User::class) |
| 31 | ->findOneBy(['id' => $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<\stdClass> $medias |
| 48 | */ |
| 49 | public function iterateMedias(array $medias, Tweet $tweet): void |
| 50 | { |
| 51 | foreach ($medias as $mediaTmp) { |
| 52 | if ($mediaTmp->type == 'photo') { |
| 53 | $this->persistMedia($tweet, $mediaTmp); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | protected function addMedias(\stdClass $tweetTmp, Tweet $tweet): void |
| 59 | { |
| 60 | if ((isset($tweetTmp->entities)) |
| 61 | && (isset($tweetTmp->entities->media))) { |
| 62 | $this->iterateMedias($tweetTmp->entities->media, $tweet); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | protected function createTweet(\stdClass $tweetTmp, User $user, bool $inTimeline): Tweet |
| 67 | { |
| 68 | $tweet = new Tweet(); |
| 69 | |
| 70 | $tweet |
| 71 | ->setId($tweetTmp->id) |
| 72 | ->setValues($tweetTmp) |
| 73 | ->setUser($user) |
| 74 | ->setInTimeline($inTimeline); |
| 75 | |
| 76 | $this->addMedias($tweetTmp, $tweet); |
| 77 | |
| 78 | return $tweet; |
| 79 | } |
| 80 | |
| 81 | protected function persistTweet( |
| 82 | \stdClass $tweetTmp, |
| 83 | User $user, |
| 84 | bool $inTimeline |
| 85 | ): Tweet { |
| 86 | /** @var Tweet|null $tweet */ |
| 87 | $tweet = $this->em |
| 88 | ->getRepository(Tweet::class) |
| 89 | ->findOneBy(['id' => $tweetTmp->id]); |
| 90 | |
| 91 | if (!$tweet) { |
| 92 | $tweet = $this->createTweet($tweetTmp, $user, $inTimeline); |
| 93 | } |
| 94 | |
| 95 | if (isset($tweetTmp->retweeted_status)) { |
| 96 | $retweet = $this->persistRetweetedTweet($tweetTmp); |
| 97 | $tweet->setRetweetedStatus($retweet); |
| 98 | } |
| 99 | |
| 100 | $this->em->persist($tweet); |
| 101 | $this->em->flush(); |
| 102 | |
| 103 | return $tweet; |
| 104 | } |
| 105 | |
| 106 | protected function persistRetweetedTweet(\stdClass $tweetTmp): Tweet |
| 107 | { |
| 108 | $retweet = $this->em |
| 109 | ->getRepository(Tweet::class) |
| 110 | ->findOneBy(['id' => $tweetTmp->retweeted_status->id]); |
| 111 | |
| 112 | if (!$retweet) { |
| 113 | $retweet = $this->addTweet( |
| 114 | $tweetTmp->retweeted_status |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | return $retweet; |
| 119 | } |
| 120 | |
| 121 | protected function persistMedia(Tweet $tweet, \stdClass $mediaTmp): void |
| 122 | { |
| 123 | $media = $this->em |
| 124 | ->getRepository(Media::class) |
| 125 | ->findOneBy(['id' => $mediaTmp->id]); |
| 126 | |
| 127 | if (!$media) { |
| 128 | // Only set the id and values when adding the Media |
| 129 | $media = new Media($mediaTmp->id); |
| 130 | $media->setValues($mediaTmp); |
| 131 | $this->em->persist($media); |
| 132 | $this->em->flush(); |
| 133 | } |
| 134 | |
| 135 | $tweet->addMedia($media); |
| 136 | } |
| 137 | |
| 138 | public function addTweet(\stdClass $tweetTmp, bool $inTimeline = false): Tweet |
| 139 | { |
| 140 | $user = $this->persistUser($tweetTmp->user); |
| 141 | |
| 142 | $tweet = $this->persistTweet($tweetTmp, $user, $inTimeline); |
| 143 | |
| 144 | // Ignore retweeted tweets |
| 145 | if ($this->displayTable && $inTimeline && $this->table instanceof Table) { |
| 146 | $this->table->addRow([ |
| 147 | $tweet->getCreatedAt()->format('Y-m-d H:i:s'), |
| 148 | mb_substr($tweet->getText(), 0, 40), |
| 149 | $user->getName(), |
| 150 | ]); |
| 151 | } |
| 152 | |
| 153 | return $tweet; |
| 154 | } |
| 155 | } |