You are here

class LockrAdminController in Lockr 8.4

Same name and namespace in other branches
  1. 8.2 src/Controller/LockrAdminController.php \Drupal\lockr\Controller\LockrAdminController
  2. 8.3 src/Controller/LockrAdminController.php \Drupal\lockr\Controller\LockrAdminController
  3. 4.x src/Controller/LockrAdminController.php \Drupal\lockr\Controller\LockrAdminController

Controller for the Lockr admin status and configuration page.

Hierarchy

Expanded class hierarchy of LockrAdminController

File

src/Controller/LockrAdminController.php, line 25

Namespace

Drupal\lockr\Controller
View source
class LockrAdminController implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * Lockr library client.
   *
   * @var Lockr
   */
  protected $lockr;

  /**
   * Generates Lockr settings.
   *
   * @var SettingsFactory
   */
  protected $settingsFactory;

  /**
   * Drupal simple config factory.
   *
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Drupal form builder.
   *
   * @var FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * Drupal file system interface.
   *
   * @var FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Drupal messenger.
   *
   * @var MessengerInterface
   */
  protected $messenger;

  /**
   * Root directory of the Drupal installation.
   *
   * @var string
   */
  protected $drupalRoot;

  /**
   * Constructs a new LockrAdminForm.
   *
   * @param Lockr $lockr
   *   The Lockr library client.
   * @param SettingsFactory $settings_factory
   *   The Lockr settings factory.
   * @param ConfigFactoryInterface $config_factory
   *   The simple config factory.
   * @param FormBuilderInterface $form_builder
   *   The Drupal form builder.
   * @param FileSystemInterface $file_system
   *   The Drupal file system interface.
   * @param MessengerInterface $messenger
   *   The Drupal messenger.
   * @param TranslationInterface $translation
   *   The Drupal translator.
   * @param string $drupal_root
   *   The Drupal site root.
   */
  public function __construct(Lockr $lockr, SettingsFactory $settings_factory, ConfigFactoryInterface $config_factory, FormBuilderInterface $form_builder, FileSystemInterface $file_system, MessengerInterface $messenger, TranslationInterface $translation, $drupal_root) {
    $this->lockr = $lockr;
    $this->settingsFactory = $settings_factory;
    $this->configFactory = $config_factory;
    $this->formBuilder = $form_builder;
    $this->fileSystem = $file_system;
    $this->messenger = $messenger;
    $this->drupalRoot = $drupal_root;
    $this
      ->setStringTranslation($translation);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('lockr.lockr'), $container
      ->get('lockr.settings_factory'), $container
      ->get('config.factory'), $container
      ->get('form_builder'), $container
      ->get('file_system'), $container
      ->get('messenger'), $container
      ->get('string_translation'), $container
      ->get('app.root'));
  }

  /**
   * Renders the Lockr status page.
   */
  public function overview() {
    try {
      $info = $this->lockr
        ->getInfo();
    } catch (LockrApiException $e) {
      if ($e
        ->getCode() >= 500) {
        watchdog_exception('lockr', $e);
        $this->messenger
          ->addMessage($this
          ->t('The Lockr service has returned an error. Please try again.'), 'error');
        return [];
      }
      $info = [];
    }
    $text_config = $this->configFactory
      ->get('lockr.ui_text');
    $ra['header'] = [
      '#prefix' => '<p>',
      '#markup' => $info ? $text_config
        ->get('admin_page.header.registered') : $text_config
        ->get('admin_page.header.not_registered'),
      '#suffix' => '</p>',
    ];
    $ra['status'] = $this
      ->getStatus($info);
    $partner = $this->settingsFactory
      ->getPartner();
    if ($partner) {
      $ra['description'] = [
        '#prefix' => '<p>',
        '#markup' => $partner['description'],
        '#suffix' => '</p>',
      ];
    }
    elseif ($info && $info['env'] === 'dev') {
      $ra['migrate'] = $this->formBuilder
        ->getForm(LockrMigrateForm::class, $info);
    }

    // If register is going to attempt writing certs to the private
    // directory (when there is no partner), then only allow registration
    // if the private directory is set.
    if (!$info && ($partner || $this
      ->privateValid())) {
      $ra['register'] = $this->formBuilder
        ->getForm(LockrRegisterForm::class);
    }
    $ra['advanced'] = $this->formBuilder
      ->getForm(LockrAdvancedForm::class);
    return $ra;
  }

  /**
   * Renders the Lockr status table.
   */
  public function getStatus(array $info) {
    require_once "{$this->drupalRoot}/core/includes/install.inc";
    $text_config = $this->configFactory
      ->get('lockr.ui_text');
    $reqs = [];
    if ($info) {
      $reqs[] = [
        'title' => $this
          ->t('Certificate Valid'),
        'value' => $this
          ->t('Yes'),
        'description' => $text_config
          ->get('admin_page.status.registered'),
        'severity' => REQUIREMENT_OK,
      ];
      $reqs[] = [
        'title' => $this
          ->t('Environment'),
        'value' => ucfirst($info['env']),
        'severity' => REQUIREMENT_INFO,
      ];
    }
    else {
      $private_valid = $this
        ->privateValid();
      $reqs[] = [
        'title' => $this
          ->t('Private Directory'),
        'value' => $private_valid ? $this->fileSystem
          ->realpath('private://') : $this
          ->t('Unknown'),
        'description' => $private_valid ? $text_config
          ->get('admin_page.status.path.exists') : $text_config
          ->get('admin_page.status.path.invalid'),
        'severity' => $private_valid ? REQUIREMENT_OK : REQUIREMENT_ERROR,
      ];
      $reqs[] = [
        'title' => $this
          ->t('Certificate Valid'),
        'value' => $this
          ->t('No'),
        'description' => $text_config
          ->get('admin_page.status.not_registered'),
      ];
    }
    if ($info) {
      $reqs[] = [
        'title' => $this
          ->t('Connected KeyRing'),
        'value' => $this
          ->t('Yes'),
        'description' => $this
          ->t("You are currently connected to the @label KeyRing.", [
          '@label' => $info['keyring']['label'],
        ]),
        'severity' => REQUIREMENT_OK,
      ];
      $has_cc = $info['keyring']['hasCreditCard'];
      if (isset($info['keyring']['trialEnd'])) {
        $trial_end = \DateTime::createFromFormat(\DateTime::RFC3339, $info['keyring']['trialEnd']);
        if ($trial_end > new \DateTime()) {
          $reqs[] = [
            'title' => $this
              ->t('Trial Expiration Date'),
            'value' => $trial_end
              ->format('M jS, Y'),
            'severity' => REQUIREMENT_INFO,
          ];
        }
        elseif (!$has_cc) {
          $reqs[] = [
            'title' => $this
              ->t('Trial Expiration Date'),
            'value' => $trial_end
              ->format('M jS, Y'),
            'severity' => REQUIREMENT_ERROR,
          ];
        }
      }
      $reqs[] = [
        'title' => $this
          ->t('Credit Card on File'),
        'value' => $has_cc ? 'Yes' : 'No',
        'description' => $has_cc ? $text_config
          ->get('admin_page.status.cc.has') : $text_config
          ->get('admin_page.status.cc.missing.required'),
        'severity' => $has_cc ? REQUIREMENT_OK : REQUIREMENT_ERROR,
      ];
    }
    lockr_preprocess_status_report($reqs);
    return [
      '#theme' => 'status_report',
      '#requirements' => $reqs,
    ];
  }

  /**
   * Returns TRUE if the private stream is available.
   */
  protected function privateValid() {
    return $this->fileSystem
      ->validScheme('private');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrAdminController::$configFactory protected property Drupal simple config factory.
LockrAdminController::$drupalRoot protected property Root directory of the Drupal installation.
LockrAdminController::$fileSystem protected property Drupal file system interface.
LockrAdminController::$formBuilder protected property Drupal form builder.
LockrAdminController::$lockr protected property Lockr library client.
LockrAdminController::$messenger protected property Drupal messenger.
LockrAdminController::$settingsFactory protected property Generates Lockr settings.
LockrAdminController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LockrAdminController::getStatus public function Renders the Lockr status table.
LockrAdminController::overview public function Renders the Lockr status page.
LockrAdminController::privateValid protected function Returns TRUE if the private stream is available.
LockrAdminController::__construct public function Constructs a new LockrAdminForm.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.