Skip to content
Snippets Groups Projects

refactor Config class to initialize config in constructor

1 file
+ 9
35
Compare changes
  • Side-by-side
  • Inline
+ 9
35
@@ -47,36 +47,10 @@ class Config {
*/
public function __construct(ConfigFactoryInterface $configFactory) {
$this->configFactory = $configFactory;
}
/**
* Returns the admin paths config in an editable state.
*
* @return \Drupal\Core\Config\Config
* The config in an editable state.
*/
private function getEditableConfig(): CoreConfig {
if (empty($this->configEditable)) {
$this->configEditable = $this->configFactory->getEditable(
self::CONFIG_KEY
);
}
return $this->configEditable;
}
/**
* Returns the admin paths config in a non-editable state.
*
* @return \Drupal\Core\Config\ImmutableConfig
* The config in a non-editable state.
*/
private function getImmutableConfig(): ImmutableConfig {
if (empty($this->configImmutable)) {
$this->configImmutable = $this->configFactory->get(self::CONFIG_KEY);
}
return $this->configImmutable;
$this->configEditable = $this->configFactory->getEditable(
self::CONFIG_KEY
);
$this->configImmutable = $this->configFactory->get(self::CONFIG_KEY);
}
/**
@@ -89,7 +63,7 @@ class Config {
* TRUE if the path is enabled.
*/
public function isPathEnabled(string $path): bool {
return !empty($this->getImmutableConfig()->get(sprintf('%s_path', $path)));
return !empty($this->configImmutable->get(sprintf('%s_path', $path)));
}
/**
@@ -102,7 +76,7 @@ class Config {
* The value of the requested path.
*/
public function getPathValue(string $path): string {
return $this->getImmutableConfig()->get(sprintf('%s_path_value', $path));
return $this->configImmutable->get(sprintf('%s_path_value', $path));
}
/**
@@ -114,7 +88,7 @@ class Config {
* Zero if disabled, one if enabled.
*/
public function setPathEnabled(string $path, int $enabled): void {
$this->getEditableConfig()->set(sprintf('%s_path', $path), $enabled);
$this->configEditable->set(sprintf('%s_path', $path), $enabled);
}
/**
@@ -126,14 +100,14 @@ class Config {
* The value to set.
*/
public function setPathValue(string $path, string $value): void {
$this->getEditableConfig()->set(sprintf('%s_path_value', $path), $value);
$this->configEditable->set(sprintf('%s_path_value', $path), $value);
}
/**
* Saves the config.
*/
public function save(): void {
$this->getEditableConfig()->save();
$this->configEditable->save();
}
}
Loading