ContentSyncSettings.php in CMS Content Sync 8
File
src/Controller/ContentSyncSettings.php
View source
<?php
namespace Drupal\cms_content_sync\Controller;
use Drupal\cms_content_sync\Entity\Pool;
use Drupal\cms_content_sync\SyncCoreInterface\SyncCoreFactory;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Site\Settings;
use EdgeBox\SyncCore\Exception\NotFoundException;
use EdgeBox\SyncCore\Interfaces\IApplicationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentSyncSettings {
protected $configFactory;
protected $embedEntities;
protected $isPreviewEnabled;
protected $authenticationType;
protected $siteBaseUrl;
protected $siteMachineName;
protected $siteId;
protected $siteName;
public function __construct(ConfigFactoryInterface $configFactory) {
$this->configFactory = $configFactory;
}
public static function getInstance() {
static $instance = null;
if ($instance) {
return $instance;
}
return $instance = self::createInstance(\Drupal::getContainer());
}
public static function createInstance(ContainerInterface $container) {
return new static($container
->get('config.factory'));
}
public function getEmbedEntities() {
if (null !== $this->embedEntities) {
return $this->embedEntities;
}
$value = $this->configFactory
->get('cms_content_sync.settings')
->get('cms_content_sync_embed_entities');
if (!$value) {
$value = [];
}
return $this->embedEntities = $value;
}
public function setEmbedEntities($set) {
$this->configFactory
->getEditable('cms_content_sync.settings')
->set('cms_content_sync_embed_entities', $set)
->save();
$this->embedEntities = $set;
}
public function isPreviewEnabled() {
if (null !== $this->isPreviewEnabled) {
return $this->isPreviewEnabled;
}
return $this->isPreviewEnabled = boolval($this->configFactory
->get('cms_content_sync.settings')
->get('cms_content_sync_enable_preview'));
}
public function setPreviewEnabled($set) {
$this->configFactory
->getEditable('cms_content_sync.settings')
->set('cms_content_sync_enable_preview', $set)
->save();
$this->isPreviewEnabled = $set;
}
public function getAuthenticationType() {
if (null !== $this->authenticationType) {
return $this->authenticationType;
}
$this->authenticationType = $this->configFactory
->get('cms_content_sync.settings')
->get('cms_content_sync_authentication_type');
if (!$this->authenticationType) {
if (\Drupal::service('module_handler')
->moduleExists('basic_auth')) {
$this->authenticationType = IApplicationInterface::AUTHENTICATION_TYPE_BASIC_AUTH;
}
else {
$this->authenticationType = IApplicationInterface::AUTHENTICATION_TYPE_COOKIE;
}
}
return $this->authenticationType;
}
public function setAuthenticationType($set) {
$this->configFactory
->getEditable('cms_content_sync.settings')
->set('cms_content_sync_authentication_type', $set)
->save();
$this->authenticationType = $set;
}
public function getSiteBaseUrl($config_only = false) {
if (null !== $this->siteBaseUrl) {
return $this->siteBaseUrl;
}
$setting = Settings::get('cms_content_sync_base_url', \Drupal::state()
->get('cms_content_sync.base_url'));
if ($config_only) {
return $setting;
}
if ($setting) {
if (UrlHelper::isValid($setting, true) && '/' !== mb_substr($setting, -1)) {
return $this->siteBaseUrl = $setting;
}
throw new \Exception(t('The configured base URL is not a valid URL. Ensure that it does not contain a trailing slash.'));
}
global $base_url;
return $this->siteBaseUrl = $base_url;
}
public function setSiteBaseUrl($set) {
\Drupal::state()
->set('cms_content_sync.base_url', $set);
$this->siteBaseUrl = null;
}
public function isDirectSyncCoreAccessEnabled($default = true) {
static $cache = null;
if (null !== $cache) {
return $cache;
}
$checked = [];
foreach (Pool::getAll() as $pool) {
$url = $pool
->getSyncCoreUrl();
if (in_array($url, $checked)) {
continue;
}
$checked[] = $url;
try {
$setting = $pool
->getClient()
->isDirectUserAccessEnabled();
if ($setting) {
return $cache = true;
}
if (false === $setting && null === $cache) {
$cache = false;
}
} catch (NotFoundException $e) {
continue;
}
}
if (null === $cache) {
return $cache = $default;
}
return $cache = false;
}
public function setDirectSyncCoreAccessEnabled($set) {
$checked = [];
foreach (Pool::getAll() as $pool) {
$url = $pool
->getSyncCoreUrl();
if (in_array($url, $checked)) {
continue;
}
$checked[] = $url;
try {
$pool
->getClient()
->isDirectUserAccessEnabled($set);
} catch (NotFoundException $e) {
continue;
}
}
}
public function getSiteMachineName() {
if (null !== $this->siteMachineName) {
return $this->siteMachineName;
}
return $this->siteMachineName = \Drupal::state()
->get('cms_content_sync.site_machine_name');
}
public function setSiteMachineName($set) {
\Drupal::state()
->set('cms_content_sync.site_machine_name', $set);
$this->siteMachineName = null;
}
public function getSiteId() {
if (null !== $this->siteId) {
return $this->siteId;
}
return $this->siteId = Settings::get('cms_content_sync_site_id', \Drupal::state()
->get('cms_content_sync.site_id'));
}
public function setSiteId($set) {
\Drupal::state()
->set('cms_content_sync.site_id', $set);
$this->siteId = null;
}
public function resetSiteId() {
\Drupal::state()
->delete('cms_content_sync.site_id');
$this->siteId = null;
\Drupal::state()
->delete('cms_content_sync.site_machine_name');
$this->siteMachineName = null;
}
public function getSiteName() {
if (null !== $this->siteName) {
return $this->siteName;
}
if (Settings::get('cms_content_sync_site_name')) {
return $this->siteName = Settings::get('cms_content_sync_site_name');
}
if ($this
->getSiteId()) {
$core = SyncCoreFactory::getAnySyncCore();
if ($core) {
$stored = $core
->getSiteName();
if ($stored) {
return $this->siteName = $stored;
}
}
}
$this->siteName = $this->configFactory
->get('system.site')
->get('name');
return $this->siteName;
}
public function setSiteName($set) {
foreach (SyncCoreFactory::getAllSyncCores() as $core) {
$core
->setSiteName($set);
}
}
public function getExtendedEntityExportLogging() {
return \Drupal::service('keyvalue.database')
->get('cms_content_sync_debug')
->get('extended_entity_export_logging');
}
public function setExtendedEntityExportLogging($value) {
$key_value_db = \Drupal::service('keyvalue.database');
$key_value_db
->get('cms_content_sync_debug')
->set('extended_entity_export_logging', $value);
}
public function getExtendedEntityImportLogging() {
return \Drupal::service('keyvalue.database')
->get('cms_content_sync_debug')
->get('extended_entity_import_logging');
}
public function setExtendedEntityImportLogging($value) {
$key_value_db = \Drupal::service('keyvalue.database');
$key_value_db
->get('cms_content_sync_debug')
->set('extended_entity_import_logging', $value);
}
}