Skip to content
Snippets Groups Projects

Issue #3317949: Gracefully catch InstagramBasicDisplayException in InstagramPostCollector::getPosts

3 files
+ 42
15
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -3,6 +3,8 @@
namespace Drupal\socialfeed\Services;
use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay;
use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplayException;
use Psr\Log\LoggerInterface;
/**
* The collector class for Instagram.
@@ -46,6 +48,13 @@ class InstagramPostCollector {
*/
protected $instagram;
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* InstagramPostCollector constructor.
*
@@ -62,13 +71,14 @@ class InstagramPostCollector {
*
* @throws \Exception
*/
public function __construct(string $apiKey, string $apiSecret, string $redirectUri, string $accessToken, InstagramBasicDisplay $instagram = NULL) {
public function __construct(string $apiKey, string $apiSecret, string $redirectUri, string $accessToken, LoggerInterface $logger, InstagramBasicDisplay $instagram = NULL) {
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->redirectUri = $redirectUri;
$this->accessToken = $accessToken;
$this->instagram = $instagram;
$this->setInstagramClient();
$this->logger = $logger;
}
/**
@@ -101,18 +111,23 @@ class InstagramPostCollector {
*/
public function getPosts($numPosts, $user_id = 'me') {
$posts = [];
$response = $this->instagram->getUserMedia($user_id, $numPosts);
if (isset($response->data)) {
$posts = array_map(function ($post) {
return [
'raw' => $post,
'media_url' => $post->media_url,
'type' => $post->media_type,
'children' => ($post->children ?? NULL),
];
}, $response->data);
try {
$response = $this->instagram->getUserMedia($user_id, $numPosts);
if (isset($response->data)) {
$posts = array_map(function ($post) {
return [
'raw' => $post,
'media_url' => $post->media_url,
'type' => $post->media_type,
'children' => ($post->children ?? NULL),
];
}, $response->data);
}
}
catch (InstagramBasicDisplayException $error) {
$this->logger->error($error);
}
return $posts;
}
}
Loading