acquia_vwo-1.0.x-dev/modules/acquia_vwo_content/src/Service/Export/ExportContent.php

modules/acquia_vwo_content/src/Service/Export/ExportContent.php
<?php

namespace Drupal\acquia_vwo_content\Service\Export;

use Drupal\acquia_vwo_content\Service\ApiClientService;
use Drupal\acquia_vwo_content\Service\Helper\EntityHelper;
use Drupal\acquia_vwo_content\Service\ThemeManager;
use Drupal\acquia_vwo_content\Storage\AcquiaVwoStorage;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;

/**
 * Contains helper methods for managing Content exports.
 *
 * @package Drupal\acquia_vwo_content
 */
class ExportContent {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity helper.
   *
   * @var \Drupal\acquia_vwo_content\Service\Helper\EntityHelper
   */
  protected $entityHelper;

  /**
   * The Theme Manager Service.
   *
   * @var \Drupal\acquia_vwo_content\Service\ThemeManager
   */
  protected $themeManager;

  /**
   * The VWO Mapping Service.
   *
   * @var \Drupal\acquia_vwo_content\Storage\AcquiaVwoStorage
   */
  protected $vwoMapperStorage;


  /**
   * VWO API Client Service.
   *
   * @var \Drupal\acquia_vwo_content\Service\ApiClientService
   */
  protected $apiClientService;

  /**
   * Logger Channel Factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $loggerChannelFactory;

  /**
   * ExportContent constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity Type Manager.
   * @param \Drupal\acquia_vwo_content\Service\Helper\EntityHelper $entity_helper
   *   The entity helper service.
   * @param \Drupal\acquia_vwo_content\Service\ThemeManager $theme_manager
   *   The theme manager service.
   * @param \Drupal\acquia_vwo_content\Storage\AcquiaVwoStorage $vwo_mapper_storage
   *   The vwo mapping service.
   * @param \Drupal\acquia_vwo_content\Service\ApiClientService $api_client_service
   *   The VWO API client service.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_channel_factory
   *   The Logger Channel Factory service.
   */
  public function __construct(
    EntityTypeManagerInterface $entity_type_manager,
    EntityHelper $entity_helper,
    ThemeManager $theme_manager,
    AcquiaVwoStorage $vwo_mapper_storage,
    ApiClientService $api_client_service,
    LoggerChannelFactoryInterface $logger_channel_factory,
  ) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityHelper = $entity_helper;
    $this->themeManager = $theme_manager;
    $this->vwoMapperStorage = $vwo_mapper_storage;
    $this->apiClientService = $api_client_service;
    $this->loggerChannelFactory = $logger_channel_factory;

  }

  /**
   * Export all entity view modes.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The current entity.
   *
   * @throws \Exception
   */
  public function exportEntity(EntityInterface $entity) {
    if (!$this->entityHelper->isEligibleForExport($entity)) {
      return NULL;
    }

    $activeTheme = $this->themeManager->getActiveTheme();
    $defaultTheme = $this->themeManager->getDefaultTheme();
    $this->themeManager->switchTheme($defaultTheme);

    $entity_payload = $this->getEntityPayload($entity->getEntityTypeId(), $entity->id(), $entity->language()->getId());

    $this->themeManager->switchTheme($activeTheme);

    $vwo_id = $this->vwoMapperStorage->getVwoId($entity->uuid(), $entity_payload['view_mode']);
    if ($vwo_id) {
      $response = $this->apiClientService->updateDataToVwo($entity_payload, $vwo_id);
    }
    else {
      $response = $this->apiClientService->sendDataToVwo($entity_payload);
    }

    if (!empty($response['_data']['id'])) {
      $this->vwoMapperStorage->saveVwoMapping(
        $entity->uuid(),
        $entity->getEntityTypeId(),
        $entity->id(),
        $entity_payload['view_mode'],
        $response['_data']['id'],
        'EXPORTED'
      );
    }
  }

  /**
   * Export all entity view modes.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The current entity.
   *
   * @throws \Exception
   */
  public function deleteEntity(EntityInterface $entity) {
    if (!$this->entityHelper->isEligibleForExport($entity)) {
      return;
    }
    $vwo_id = $this->vwoMapperStorage->getVwoId($entity->uuid(), 'default');
    if ($vwo_id) {
      $this->apiClientService->deleteDataFromVwo($vwo_id);
      $this->vwoMapperStorage->deleteVwoMapping($entity->uuid(), 'default');
    }
  }

  /**
   * Get list of all entity variations (view modes/translations).
   *
   * @param string $entity_type_id
   *   Entity type id of the entity that should be exported.
   * @param int $entity_id
   *   Id of the entity that should be exported.
   * @param string $langcode
   *   Language code of the entity translation that should be exported.
   *   'all' value means that all entity translations should be exported.
   *
   * @return array|null
   *   Returns list of available view modes/translations variations.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getEntityPayload(string $entity_type_id, int $entity_id, string $langcode = 'all'): array|null {
    $payload = [];
    $entity = $this
      ->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($entity_id);
    if (!$entity instanceof ContentEntityInterface) {
      return [];
    }
    if (!$entity_config = $this->entityHelper->getEntityConfigValue($entity)) {
      return [];
    }
    if ($langcode === 'all') {
      foreach ($entity->getTranslationLanguages() as $language) {
        $language_id = $language->getId();
        $translation = $entity->hasTranslation($language_id) ? $entity->getTranslation($language_id) : $entity->getUntranslated();
        $payload = $this->entityHelper->getEntityVariation($translation, $entity_config, $language_id);
      }
    }
    else {
      $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->getUntranslated();
      $payload = $this->entityHelper->getEntityVariation($translation, $entity_config, $langcode);
    }
    return $payload;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc