DeleteAssets.php in Acquia Content Hub 8.2        
                          
                  
                        
  
  
  
  
  
File
  modules/acquia_contenthub_subscriber/src/EventSubscriber/HandleWebhook/DeleteAssets.php
  
    View source  
  <?php
namespace Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\acquia_contenthub_subscriber\SubscriberTracker;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DeleteAssets implements EventSubscriberInterface {
  
  protected $tracker;
  
  protected $config;
  
  public function __construct(SubscriberTracker $tracker, ConfigFactoryInterface $config_factory) {
    $this->tracker = $tracker;
    $this->config = $config_factory
      ->getEditable('acquia_contenthub.admin_settings');
  }
  
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = 'onHandleWebhook';
    return $events;
  }
  
  public function onHandleWebhook(HandleWebhookEvent $event) : void {
    $payload = $event
      ->getPayload();
    $assets = $payload['assets'] ?? [];
    $client = $event
      ->getClient();
    $settings = $client
      ->getSettings();
    $client_uuid = $settings
      ->getUuid();
    if ('successful' !== $payload['status'] || 'delete' !== $payload['crud'] || $payload['initiator'] === $client_uuid || empty($assets)) {
      return;
    }
    $send_update = $this->config
      ->get('send_contenthub_updates') ?? TRUE;
    foreach ($assets as $asset) {
      if (!$this
        ->isSupportedType($asset['type'])) {
        continue;
      }
      $entity = $this->tracker
        ->getEntityByRemoteIdAndHash($asset['uuid']);
      if (!$entity) {
        
        $this->tracker
          ->delete($asset['uuid']);
        
        if ($settings && $send_update) {
          $webhook_uuid = $settings
            ->getWebhook('uuid');
          $client
            ->deleteInterest($asset['uuid'], $webhook_uuid);
        }
        continue;
      }
      $status = $this->tracker
        ->getStatusByUuid($asset['uuid']);
      
      if ($status === SubscriberTracker::AUTO_UPDATE_DISABLED) {
        $this->tracker
          ->delete($asset['uuid']);
        continue;
      }
      $entity
        ->delete();
    }
  }
  
  protected function isSupportedType(string $type) : bool {
    $supported_types = [
      'drupal8_content_entity',
      'drupal8_config_entity',
    ];
    return in_array($type, $supported_types, TRUE);
  }
}