You are here

class Drupal8Config in Rocket.Chat 8.2

Same name and namespace in other branches
  1. 8 modules/rocket_chat_api/src/RocketChat/Drupal8Config.php \Drupal\rocket_chat_api\RocketChat\Drupal8Config

Class Drupal8Config connects the API with the drupal system.

@package RocketChat

Hierarchy

Expanded class hierarchy of Drupal8Config

7 files declare their use of Drupal8Config
ApiTestForm.php in modules/rocket_chat_api_test/src/Form/ApiTestForm.php
Contains \Drupal\rocket_chat_api_test\Form\ApiTestForm.
Channel.php in modules/rocket_chat_api/src/RocketChat/Element/Channel.php
GroupChannel.php in modules/rocket_chat_group/src/Plugin/Field/FieldType/GroupChannel.php
moduleHelper.php in modules/rocket_chat_group/src/RocketChat/moduleHelper.php
RocketChatChannelBlock.php in modules/rocket_chat_group/src/Plugin/Block/RocketChatChannelBlock.php

... See full list

File

modules/rocket_chat_api/src/RocketChat/Drupal8Config.php, line 25

Namespace

Drupal\rocket_chat_api\RocketChat
View source
class Drupal8Config implements RocketChatConfigInterface, ContainerInjectionInterface {

  /**
   * The config factory.
   *
   * Subclasses should use the self::config() method, which may be overridden
   * to address specific needs when loading config, rather than this property
   * directly.
   * See \Drupal\Core\Form\ConfigFormBase::config() for an example of this.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * @var \Psr\Log\LoggerInterface
   */
  protected $Logger;

  /**
   * Constructs a \Drupal\system\ConfigFormBase object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The ModuleHandler to interact with loaded modules.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state interface to manipulate the States.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $moduleHandler, StateInterface $state, MessengerInterface $messenger) {
    $this->config = $config_factory
      ->get('rocket_chat.settings');
    $this->moduleHandler = $moduleHandler;
    $this->state = $state;
    $this->messenger = $messenger;
    $this->Logger = Drupal::logger("Rocket Chat API: Config");
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var ConfigFactoryInterface $configFactory */
    $configFactory = $container
      ->get('config.factory');

    /** @var ModuleHandlerInterface $modulehandler */
    $modulehandler = $container
      ->get('module_handler');

    /** @var StateInterface $stateInterface */
    $stateInterface = $container
      ->get('state');

    /** @var MessengerInterface $messenger */
    $messenger = $container
      ->get('messenger');
    return new static($configFactory, $modulehandler, $stateInterface, $messenger);
  }

  /**
   * {@inheritdoc}
   */
  public function getElement($elementName, $default = NULL) {
    switch ($elementName) {

      /** @noinspection PhpMissingBreakStatementInspection */
      case 'rocket_chat_url':

        // Fallthrough and modify.
        $elementName = "server";
      default:
        $value = $this->config
          ->get($elementName);
        if (empty($value)) {
          $value = $default;
        }
        return $value;
      case 'rocket_chat_uid':

      // Fallthrough.
      case 'rocket_chat_uit':

        // Fallthrough.
        return $this->state
          ->get($elementName, $default);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setElement($elementName, $newValue) {
    $config = $this->config;
    switch ($elementName) {

      /** @noinspection PhpMissingBreakStatementInspection */
      case 'rocket_chat_url':

        // Fallthrough and modify.
        $elementName = "url";
      default:
        $config
          ->clear($elementName)
          ->set($elementName, $newValue)
          ->save();
        break;
      case 'rocket_chat_uid':

      // Fallthrough.
      case 'rocket_chat_uit':

        // Fallthrough.
        $this->state
          ->delete($elementName);
        if (!empty($newValue)) {
          $this->state
            ->set($elementName, $newValue);
        }
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isDebug() {
    return $this->state
      ->get("rocket.chat.debugMode", FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function getJsonDecoder() {
    return '\\Drupal\\Component\\Serialization\\Json::decode';
  }

  /**
   * {@inheritdoc}
   */
  public function notify($message, $type) {
    return $this->messenger
      ->addMessage("{$message}", $type);
  }

  /**
   * Check if we got everything to connect to a Client.
   *
   * @return bool
   */
  public function isReady() {
    return !empty($this
      ->getElement('rocket_chat_url')) && !empty($this
      ->getElement('rocket_chat_uid')) && !empty($this
      ->getElement('rocket_chat_uit'));
  }

  /**
   * Log a specific action
   *
   * @param $message
   *   Message to log.
   * @param $level
   *   a string value if either "error"|"warning"|"info"|"debug" to indicate the level of this log message.
   *
   * @return void
   */
  public function log($message, $level) {
    $this->Logger
      ->log($level, $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Drupal8Config::$config protected property The config factory.
Drupal8Config::$Logger protected property
Drupal8Config::$messenger protected property The messenger.
Drupal8Config::$moduleHandler protected property
Drupal8Config::$state protected property
Drupal8Config::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Drupal8Config::getElement public function Get a RocketChatConfigInterface Element. Overrides RocketChatConfigInterface::getElement 1
Drupal8Config::getJsonDecoder public function Get a function pointer to the function to use for JsonDecodeing. Overrides RocketChatConfigInterface::getJsonDecoder
Drupal8Config::isDebug public function Is this a Debug / verbose Run. Overrides RocketChatConfigInterface::isDebug 1
Drupal8Config::isReady public function Check if we got everything to connect to a Client. Overrides RocketChatConfigInterface::isReady
Drupal8Config::log public function Log a specific action Overrides RocketChatConfigInterface::log 1
Drupal8Config::notify public function Notify the backend. Overrides RocketChatConfigInterface::notify 1
Drupal8Config::setElement public function Set an Element in the RocketChatConfigInterface. Overrides RocketChatConfigInterface::setElement 1
Drupal8Config::__construct public function Constructs a \Drupal\system\ConfigFormBase object.