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'
1 service uses AchVersionAttribute
File
- src/
EventSubscriber/ CdfAttributes/ AchVersionAttribute.php, line 16
Namespace
Drupal\acquia_contenthub\EventSubscriber\CdfAttributesView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AchVersionAttribute:: |
protected | property | Module handler. | |
AchVersionAttribute:: |
protected | property | Project Version Client to fetch D.O latest releases. | |
AchVersionAttribute:: |
protected | function | Fetch ACH version. | |
AchVersionAttribute:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AchVersionAttribute:: |
public | function | Method called on the BUILD_CLIENT_CDF event. | |
AchVersionAttribute:: |
public | function | AchVersionAttribute constructor. |