AcquiaContentHubPublisherCommands.php in Acquia Content Hub 8.2        
                          
                  
                        
  
  
  
  
  
File
  modules/acquia_contenthub_publisher/src/Commands/AcquiaContentHubPublisherCommands.php
  
    View source  
  <?php
namespace Drupal\acquia_contenthub_publisher\Commands;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Drush\Commands\DrushCommands;
use Drush\Log\LogLevel;
class AcquiaContentHubPublisherCommands extends DrushCommands {
  
  protected $database;
  
  protected $config;
  
  protected $clientFactory;
  
  protected $logger;
  
  protected $state;
  
  protected $moduleHandler;
  
  public function __construct(Connection $database, ConfigFactoryInterface $config_factory, ClientFactory $client_factory, LoggerChannelFactoryInterface $logger, StateInterface $state, ModuleHandlerInterface $module_handler) {
    $this->database = $database;
    $this->config = $config_factory
      ->get('acquia_contenthub.admin_settings');
    $this->clientFactory = $client_factory;
    $this->logger = $logger
      ->get('acquia_contenthub_publisher');
    $this->state = $state;
    $this->moduleHandler = $module_handler;
  }
  
  public function upgrade() {
    
    if (!$this->database
      ->schema()
      ->tableExists('acquia_contenthub_entities_tracking')) {
      $this->logger
        ->log(LogLevel::CANCEL, dt('Legacy tracking table does not exist.'));
      return;
    }
    
    $settings = $this->clientFactory
      ->getSettings();
    $client = $this->clientFactory
      ->getClient($settings);
    if (!$client
      ->getSettings()
      ->getWebhook()) {
      
      $webhook_url = Url::fromUri('internal:' . '/acquia-contenthub/webhook', [
        'absolute' => TRUE,
      ])
        ->toString();
      $webhook = $client
        ->getWebHook($webhook_url);
      if (empty($webhook)) {
        $connection_manager = \Drupal::service('acquia_contenthub.connection_manager');
        $response = $connection_manager
          ->registerWebhook($webhook_url);
        if (isset($response['success']) && FALSE === $response['success']) {
          $message = dt('Registering webhooks encountered an error (code @code). @reason', [
            '@code' => $response['error']['code'],
            '@reason' => $response['error']['message'],
          ]);
          $this->logger
            ->log(LogLevel::ERROR, $message);
          return;
        }
      }
    }
    
    $path = $this->moduleHandler
      ->getModule('acquia_contenthub_publisher')
      ->getPath();
    $batch = [
      'title' => t('Exporting'),
      'operations' => [
        [
          'acquia_contenthub_publisher_enqueue_exported_entities',
          [],
        ],
      ],
      'finished' => 'acquia_contenthub_publisher_enqueue_exported_entities_finished',
      'file' => $path . '/acquia_contenthub_publisher.migrate.inc',
    ];
    batch_set($batch);
    drush_backend_batch_process();
  }
}