You are here

StorageFactory.php in Radioactivity 8.3

Same filename and directory in other branches
  1. 4.0.x src/StorageFactory.php

File

src/StorageFactory.php
View source
<?php

namespace Drupal\radioactivity;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;

/**
 * Storage factory service.
 */
class StorageFactory {

  /**
   * The radioactivity storage configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * StorageFactory constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The configuration factory.
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $classResolver
   *   The class resolver.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ClassResolverInterface $classResolver) {
    $this->config = $configFactory
      ->get('radioactivity.storage');
    $this->classResolver = $classResolver;
  }

  /**
   * Getter for classes which implement IncidentStorageInterface.
   *
   * @param string $type
   *   The type of storage to get.
   *
   * @return \Drupal\radioactivity\IncidentStorageInterface
   *   Instance of the requested storage.
   */
  public function get($type) {
    switch ($type) {
      case 'rest_local':
        $instance = $this->classResolver
          ->getInstanceFromDefinition('radioactivity.rest_incident_storage');
        $instance
          ->setEndpoint(NULL);
        break;
      case 'rest_remote':
        $instance = $this->classResolver
          ->getInstanceFromDefinition('radioactivity.rest_incident_storage');
        $instance
          ->setEndpoint($this->config
          ->get('endpoint'));
        break;
      case 'default':
      default:
        $instance = $this->classResolver
          ->getInstanceFromDefinition('radioactivity.default_incident_storage');
    }

    /** @var \Drupal\radioactivity\IncidentStorageInterface $instance */
    return $instance;
  }

  /**
   * Get the configured incident storage.
   *
   * @return \Drupal\radioactivity\IncidentStorageInterface
   *   The configured storage instance.
   */
  public function getConfiguredStorage() {
    $type = $this->config
      ->get('type') ?: 'default';
    return $this
      ->get($type);
  }

}

Classes

Namesort descending Description
StorageFactory Storage factory service.