You are here

class AchVersionAttribute in Acquia Content Hub 8.2

Adds ACH module version attribute to client CDF.

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\AchVersionAttribute implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AchVersionAttribute

1 file declares its use of AchVersionAttribute
AchVersionAttributeTest.php in tests/src/Kernel/EventSubscriber/CdfAttributes/AchVersionAttributeTest.php
1 string reference to 'AchVersionAttribute'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses AchVersionAttribute
ach_version.client_cdf.attribute in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\AchVersionAttribute

File

src/EventSubscriber/CdfAttributes/AchVersionAttribute.php, line 16

Namespace

Drupal\acquia_contenthub\EventSubscriber\CdfAttributes
View source
class AchVersionAttribute implements EventSubscriberInterface {

  /**
   * Project Version Client to fetch D.O latest releases.
   *
   * @var \Drupal\acquia_contenthub\Client\ProjectVersionClient
   */
  protected $pvClient;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * AchVersionAttribute constructor.
   *
   * @param \Drupal\acquia_contenthub\Client\ProjectVersionClient $pv_client
   *   Project version client.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module Handler Service.
   */
  public function __construct(ProjectVersionClient $pv_client, ModuleHandlerInterface $module_handler) {
    $this->pvClient = $pv_client;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::BUILD_CLIENT_CDF][] = [
      'onBuildClientCdf',
    ];
    return $events;
  }

  /**
   * Method called on the BUILD_CLIENT_CDF event.
   *
   * Adds ACH module version attribute to the cdf.
   *
   * @param \Drupal\acquia_contenthub\Event\BuildClientCdfEvent $event
   *   The event being dispatched.
   *
   * @throws \Exception
   */
  public function onBuildClientCdf(BuildClientCdfEvent $event) {
    $cdf = $event
      ->getCdf();
    $ach_attributes['current'] = $this
      ->getAchVersion();
    $ch_latest_versions = $this->pvClient
      ->getContentHubReleases();
    if (!empty($ch_latest_versions)) {
      $ch_latest_version = $ch_latest_versions['latest'];
      $ch_latest_version = substr($ch_latest_version, strpos($ch_latest_version, '-') + 1);
      $ach_attributes['latest'] = $ch_latest_version;
    }
    $cdf
      ->addAttribute('ch_version', CDFAttribute::TYPE_ARRAY_STRING);
    $attribute = $cdf
      ->getAttribute('ch_version');
    $attribute
      ->setValue($ach_attributes);
  }

  /**
   * Fetch ACH version.
   *
   * @return mixed|string
   *   Acquia Content Hub version.
   *
   * @throws \Exception
   */
  protected function getAchVersion() {
    $module_path = $this->moduleHandler
      ->getModule('acquia_contenthub')
      ->getPath();
    $version_file_path = $module_path . '/acquia_contenthub_versions.yml';
    if (!file_exists($version_file_path)) {
      throw new \Exception('ACH YAML version file doesn\'t exist.');
    }
    $versions = Yaml::decode(file_get_contents($version_file_path));
    return $versions['acquia_contenthub'] ?? '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AchVersionAttribute::$moduleHandler protected property Module handler.
AchVersionAttribute::$pvClient protected property Project Version Client to fetch D.O latest releases.
AchVersionAttribute::getAchVersion protected function Fetch ACH version.
AchVersionAttribute::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AchVersionAttribute::onBuildClientCdf public function Method called on the BUILD_CLIENT_CDF event.
AchVersionAttribute::__construct public function AchVersionAttribute constructor.