You are here

class StorageFactory in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x src/StorageFactory.php \Drupal\radioactivity\StorageFactory

Storage factory service.

Hierarchy

Expanded class hierarchy of StorageFactory

4 files declare their use of StorageFactory
EmitController.php in src/Controller/EmitController.php
EmitControllerTest.php in tests/src/Unit/EmitControllerTest.php
RadioactivityProcessorTest.php in tests/src/Unit/RadioactivityProcessorTest.php
StorageFactoryTest.php in tests/src/Unit/StorageFactoryTest.php
1 string reference to 'StorageFactory'
radioactivity.services.yml in ./radioactivity.services.yml
radioactivity.services.yml
1 service uses StorageFactory
radioactivity.storage in ./radioactivity.services.yml
Drupal\radioactivity\StorageFactory

File

src/StorageFactory.php, line 11

Namespace

Drupal\radioactivity
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StorageFactory::$classResolver protected property The class resolver.
StorageFactory::$config protected property The radioactivity storage configuration.
StorageFactory::get public function Getter for classes which implement IncidentStorageInterface.
StorageFactory::getConfiguredStorage public function Get the configured incident storage.
StorageFactory::__construct public function StorageFactory constructor.