You are here

class SendTinCanStatement in Opigno TinCan API 8

Same name and namespace in other branches
  1. 3.x src/Plugin/QueueWorker/SendTinCanStatement.php \Drupal\opigno_tincan_api\Plugin\QueueWorker\SendTinCanStatement

Send TinCan Statement.

Plugin annotation


@QueueWorker(
  id = "opigno_tincan_send_tincan_statement",
  title = @Translation("Send TinCan Statement via queue"),
  cron = {"time" = 20}
)

Hierarchy

Expanded class hierarchy of SendTinCanStatement

File

src/Plugin/QueueWorker/SendTinCanStatement.php, line 19

Namespace

Drupal\opigno_tincan_api\Plugin\QueueWorker
View source
class SendTinCanStatement extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * Config factory service.
   */
  protected $configFactory;

  /**
   * Logger system service.
   */
  protected $logger;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, $config_factory, $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('logger.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {

    // _opigno_tincan_api_send_statement.
    // The variables 'opigno_tincan_api_*'
    // will be used to send the statement to the LRS.
    $config = $this->configFactory
      ->get('opigno_tincan_api.settings');
    $endpoint = $config
      ->get('opigno_tincan_api_endpoint');
    $username = $config
      ->get('opigno_tincan_api_username');
    $password = $config
      ->get('opigno_tincan_api_password');
    if (empty($endpoint) || empty($username) || empty($password)) {
      return FALSE;
    }
    $lrs = new RemoteLRS($endpoint, '1.0.1', $username, $password);
    $response = $lrs
      ->saveStatement($data->statement);
    if ($response->success === FALSE) {
      $this->logger
        ->get('Opigno Tincan API')
        ->error('The following statement could not be sent :<br /><pre>' . print_r($statement
        ->asVersion('1.0.1'), TRUE) . '</pre><br/>', []);
      return FALSE;
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SendTinCanStatement::$configFactory protected property Config factory service.
SendTinCanStatement::$logger protected property Logger system service.
SendTinCanStatement::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SendTinCanStatement::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
SendTinCanStatement::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct