Skip to content

Commit

Permalink
Add checks to ensure there's enough posts in the cache to satisfy the…
Browse files Browse the repository at this point in the history
… new requests, and correctly round halfs (we then check length anyway)
  • Loading branch information
fryiee committed Sep 29, 2017
1 parent f2f7427 commit f1aefe1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Social.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,15 @@ private function getCachedFeed()
$cache = json_decode(file_get_contents($this->getCache()), true);
}

return isset($cache) ? $cache : $this->makeFeed();
if (isset($cache) && count($cache) >= $this->getCount()) {
if (count($cache) > $this->getCount()) {
return array_slice($cache, 0, $this->getCount());
} else {
return $cache;
}
} else {
return $this->makeFeed();
}
}

/**
Expand All @@ -164,8 +172,9 @@ private function getCachedFeed()
*/
private function makeFeed()
{
$twitter = (new Twitter($this->getCount() / 2))->getFeed();
$instagram = (new Instagram($this->getCount() / 2))->getFeed();
$half = round($this->getCount() / 2, PHP_ROUND_HALF_UP);
$twitter = (new Twitter($half))->getFeed();
$instagram = (new Instagram($half))->getFeed();

$feed = $this->sortFeed(array_merge($twitter, $instagram));

Expand All @@ -178,6 +187,10 @@ private function makeFeed()
}
}

if (count($feed) > $this->getCount()) {
$feed = array_slice($feed, 0, $this->getCount());
}

return $feed;
}

Expand Down

0 comments on commit f1aefe1

Please sign in to comment.