SendTinCanStatement.php in Opigno TinCan API 8        
                          
                  
                        
  
  
  
  
File
  src/Plugin/QueueWorker/SendTinCanStatement.php
  
    View source  
  <?php
namespace Drupal\opigno_tincan_api\Plugin\QueueWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use TinCan\RemoteLRS;
class SendTinCanStatement extends QueueWorkerBase implements ContainerFactoryPluginInterface {
  
  protected $configFactory;
  
  protected $logger;
  
  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;
  }
  
  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'));
  }
  
  public function processItem($data) {
    
    $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;
  }
}