Skip to content

Instantly share code, notes, and snippets.

@btmash
Last active March 18, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btmash/747a8a3732172c968671da7c8084e682 to your computer and use it in GitHub Desktop.
Save btmash/747a8a3732172c968671da7c8084e682 to your computer and use it in GitHub Desktop.
SocialFeedCommands.php ported
<?php
namespace Drupal\social_feed\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drush\Commands\DrushCommands;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\Queue\QueueWorkerInterface;
use Drupal\Core\Queue\SuspendQueueException;
/**
* A Drush commandfile.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class SocialFeedCommands extends DrushCommands
{
/**
* Import items from configured social feeds.
*
* @param array $options
* An associative array of options whose values come from cli, aliases, config, etc.
* @option $feed The name of the feed
* @usage social-feed-import
* Imports all feeds
* @usage social-feed-import --feed=facebook
* Imports facebook feed
* @usage social-feed-import --feed=twitter
* Imports twitter feed
* @usage social-feed-import --feed=instagram
* Imports instagram feed
*
* @command social-feed-import
* @aliases sfi
*/
public function socialFeedImport($options = ['feed' => ''])
{
$feeds = [
'facebook' => 'facebook',
'twitter' => 'twitter',
'instagram_noauth' => 'instagram'
];
if (!empty($options['feed'])) {
if (!in_array($options['feed'], $feeds)) {
return drush_set_error('SOCIAL_FEED', dt('No matching feed found.'));
}
$feeds = array_intersect($feeds, array($options['feed']));
}
foreach ($feeds as $feed => $qid) {
call_user_func("social_feed_get_{$feed}");
// Call workers manually.
$q_id = "{$qid}_posts";
/** @var QueueInterface $queue */
$queue = \Drupal::queue($q_id);
if (!$queue) {
throw new \Exception(dt('No matching queue found.'));
}
/** @var QueueWorkerInterface $queue_worker */
$queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($q_id);
while ($item = $queue->claimItem()) {
try {
$queue_worker->processItem($item->data);
$queue->deleteItem($item);
} catch (SuspendQueueException $e) {
$queue->releaseItem($item);
break;
} catch (\Exception $e) {
watchdog_exception('npq', $e);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment