AcsfExtraCommands.php in Acquia Cloud Site Factory Connector 8.2
File
src/Commands/AcsfExtraCommands.php
View source
<?php
namespace Drush\Commands;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Drupal\acsf\AcsfConfigDefault;
use Drupal\acsf\AcsfException;
use Drupal\acsf\AcsfMessageRest;
use Drupal\Core\Extension\InfoParser;
class AcsfExtraCommands extends DrushCommands {
public function getFactoryCreds() {
if (!class_exists('\\Drupal\\acsf\\AcsfConfigDefault')) {
$include_path = realpath(dirname(__FILE__));
require_once $include_path . '/src/AcsfConfig.php';
require_once $include_path . '/src/AcsfConfigDefault.php';
require_once $include_path . '/src/AcsfConfigIncompleteException.php';
require_once $include_path . '/src/AcsfConfigMissingCredsException.php';
}
try {
$config = new AcsfConfigDefault();
} catch (\Exception $e) {
throw new AcsfException('Failed to get config: ' . $e
->getMessage());
}
$creds = [
'url' => $config
->getUrl(),
'username' => $config
->getUsername(),
'password' => $config
->getPassword(),
'url_suffix' => $config
->getUrlSuffix(),
];
return new PropertyList($creds);
}
public function offline() {
$lock = \Drupal::lock();
$acsf_settings = \Drupal::configFactory()
->getEditable('acsf.settings');
$maintenance_mode = \Drupal::state()
->get('system.maintenance_mode');
if ($maintenance_mode) {
$acsf_settings
->set('site_owner_maintenance_mode', TRUE)
->save();
}
$expected = time() + 10 * 60;
\Drupal::state()
->set('system.maintenance_mode', TRUE);
$acsf_settings
->set('maintenance_time', $expected)
->save();
$lock
->acquire('cron', 1200.0);
}
public function postOffline() {
$offline = \Drupal::state()
->get('system.maintenance_mode');
if ($offline) {
$this
->logger()
->success(dt('Site has been placed offline.'));
}
else {
$this
->logger()
->error(dt('Site has not been placed offline.'));
}
}
public function online() {
$lock = \Drupal::lock();
$content = \Drupal::config('acsf.settings')
->get('site_owner_maintenance_mode');
\Drupal::state()
->set('system.maintenance_mode', FALSE);
\Drupal::configFactory()
->getEditable('acsf.settings')
->set('maintenance_time', 0)
->save();
if (!empty($content)) {
\Drupal::state()
->set('system.maintenance_mode', TRUE);
}
$lock
->release('cron');
}
public function postOnline() {
$content = \Drupal::state()
->get('system.maintenance_mode');
if (empty($content)) {
$this
->logger()
->success(dt('Site has been placed online.'));
}
else {
$content = \Drupal::config('acsf.settings')
->get('site_owner_maintenance_mode');
if (empty($content)) {
$this
->logger()
->error(dt('Site has not been placed online.'));
}
else {
$this
->logger()
->success(dt('Site has been left offline as set by the site owner.'));
\Drupal::configFactory()
->getEditable('acsf.settings')
->set('site_owner_maintenance_mode', FALSE)
->save();
}
}
}
public function versionGet($path) {
if (empty($path)) {
$path = __DIR__;
}
$version = '0.0';
$acsf_file_path = rtrim($path, '/') . '/acsf.info.yml';
if (file_exists($acsf_file_path)) {
$info_parser = new InfoParser();
$info = $info_parser
->parse($acsf_file_path);
$version = isset($info['acsf_version']) ? $info['acsf_version'] : '0.1';
}
$this
->output()
->writeln($version);
}
public function completeAsyncProcess(array $options = [
'data' => NULL,
]) {
$data = unserialize($options['data']);
if (empty($data->callback) || empty($data->object_id) || empty($data->acsf_path)) {
throw new AcsfException(dt('Requires serialized object in --data argument with $data->callback and $data->object_id populated.'));
}
require_once $data->acsf_path . '/src/AcsfConfig.php';
require_once $data->acsf_path . '/src/AcsfConfigDefault.php';
require_once $data->acsf_path . '/src/AcsfConfigIncompleteException.php';
require_once $data->acsf_path . '/src/AcsfConfigMissingCredsException.php';
require_once $data->acsf_path . '/src/AcsfMessage.php';
require_once $data->acsf_path . '/src/AcsfMessageEmptyResponseException.php';
require_once $data->acsf_path . '/src/AcsfMessageFailedResponseException.php';
require_once $data->acsf_path . '/src/AcsfMessageFailureException.php';
require_once $data->acsf_path . '/src/AcsfMessageMalformedResponseException.php';
require_once $data->acsf_path . '/src/AcsfMessageRest.php';
require_once $data->acsf_path . '/src/AcsfMessageResponse.php';
require_once $data->acsf_path . '/src/AcsfMessageResponseRest.php';
$arguments = [
'wid' => $data->object_id,
'signal' => 1,
'state' => isset($data->state) ? $data->state : NULL,
'data' => $data,
];
try {
$config = new AcsfConfigDefault();
$message = new AcsfMessageRest('POST', $data->callback, $arguments, $config);
$message
->send();
} catch (\Exception $e) {
syslog(LOG_ERR, dt('Unable to contact the factory via AcsfMessage.'));
}
}
}