You are here

LockrAdminController.php in Lockr 8.2

File

src/Controller/LockrAdminController.php
View source
<?php

namespace Drupal\lockr\Controller;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\StreamWrapper\PrivateStream;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\lockr\ClientFactory;
use Drupal\lockr\Form\LockrAdvancedForm;
use Drupal\lockr\Form\LockrCSRForm;
use Drupal\lockr\Form\LockrMigrateForm;
use Drupal\lockr\Form\LockrRegisterForm;
use Drupal\lockr\PartnerFactory;
class LockrAdminController implements ContainerInjectionInterface {

  /**
   * Constructs a new LockrAdminForm.
   */
  public function __construct(ClientFactory $client_factory, PartnerFactory $partner_factory, ConfigFactoryInterface $config_factory, FormBuilderInterface $form_builder, $drupalRoot) {
    $this->clientFactory = $client_factory;
    $this->partnerFactory = $partner_factory;
    $this->configFactory = $config_factory;
    $this->formBuilder = $form_builder;
    $this->drupalRoot = $drupalRoot;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('lockr.client_factory'), $container
      ->get('lockr.partner_factory'), $container
      ->get('config.factory'), $container
      ->get('form_builder'), $container
      ->get('app.root'));
  }
  public function overview() {
    $site_client = $this->clientFactory
      ->getSiteClient();
    try {
      $status = $site_client
        ->exists();
    } catch (LockrServerException $e) {
      watchdog_exception('lockr', $e);
      drupal_set_message('The Lockr service has returned an error. Please try again.', 'error');
      return $form;
    }
    $text_config = $this->configFactory
      ->get('lockr.ui_text');
    $exists = $status['exists'];
    $ra['header'] = [
      '#prefix' => '<p>',
      '#markup' => $exists ? $text_config
        ->get('admin_page.header.registered') : $text_config
        ->get('admin_page.header.not_registered'),
      '#suffix' => '</p>',
    ];
    $ra['status'] = $this
      ->getStatus($status);
    $cert_valid = $status['cert_valid'];
    $partner = $this->partnerFactory
      ->detectPartner();
    if ($partner) {
      $ra['description'] = [
        '#prefix' => '<p>',
        '#markup' => $partner['description'],
        '#suffix' => '</p>',
      ];
    }
    elseif (!$cert_valid) {
      $ra['csr'] = $this->formBuilder
        ->getForm(LockrCSRForm::class);
    }
    elseif ($exists && $status['info']['env'] === 'dev') {
      $ra['migrate_info'] = array(
        '#prefix' => '<p>',
        '#markup' => 'Click the button below to deploy this site to production. ' . 'This should only be done in your production environment as it writes a new certificate to the file system. ',
        '#suffix' => '</p>',
      );
      $ra['migrate'] = $this->formBuilder
        ->getForm(LockrMigrateForm::class);
    }
    if (!$exists && $cert_valid) {
      $ra['register'] = $this->formBuilder
        ->getForm(LockrRegisterForm::class);
    }
    $ra['advanced'] = $this->formBuilder
      ->getForm(LockrAdvancedForm::class);
    return $ra;
  }
  public function getStatus($status) {
    require_once "{$this->drupalRoot}/core/includes/install.inc";
    $text_config = $this->configFactory
      ->get('lockr.ui_text');
    $cert_valid = $status['cert_valid'];
    $exists = $status['exists'];
    $created = isset($status['created']) ? $status['created'] : FALSE;
    $reqs[] = [
      'title' => 'Certificate Valid',
      'value' => $cert_valid ? 'Yes' : 'No',
      'description' => $cert_valid ? $text_config
        ->get('admin_page.status.cert.valid') : $text_config
        ->get('admin_page.status.cert.not_valid'),
      'severity' => $cert_valid ? REQUIREMENT_OK : REQUIREMENT_ERROR,
    ];
    if ($cert_valid) {
      $reqs[] = [
        'title' => 'Environment',
        'value' => $status['info']['env'],
        'severity' => REQUIREMENT_INFO,
      ];
    }
    else {
      $private_path = PrivateStream::basepath();
      $default_text = $private_path ? $text_config
        ->get('admin_page.status.path.invalid') : $text_config
        ->get('admin_page.status.path.unknown');
      $is_dir = is_dir($private_path);
      $req = [
        'title' => 'Private Directory',
        'value' => $private_path ?: 'Unknown',
        'description' => $is_dir ? $text_config
          ->get('admin_page.status.path.exists') : $default_text,
        'severity' => $is_dir ? REQUIREMENT_OK : REQUIREMENT_ERROR,
      ];
      array_unshift($reqs, $req);
    }
    $reqs[] = [
      'title' => 'Site Registered',
      'value' => $exists ? 'Yes' : 'No',
      'description' => $exists ? $text_config
        ->get('admin_page.status.registered') : $text_config
        ->get('admin_page.status.not_registered'),
      'severity' => $exists ? REQUIREMENT_OK : REQUIREMENT_ERROR,
    ];
    if ($cert_valid) {
      $has_cc = $status['has_cc'];
      if ($created) {
        $expires = (new \DateTime())
          ->setTimestamp($created)
          ->add(new \DateInterval('P14D'));
        if ($expires > new \DateTime()) {
          $reqs[] = [
            'title' => 'Trial Expiration Date',
            'value' => $expires
              ->format('M jS, Y'),
            'severity' => REQUIREMENT_INFO,
          ];
        }
        elseif (!$has_cc) {
          $reqs[] = [
            'title' => 'Trial Expiration Date',
            'value' => $expires
              ->format('M jS, Y'),
            'severity' => REQUIREMENT_ERROR,
          ];
        }
      }
      $partner = $status['info']['partner'];
      $is_custom = in_array($partner, [
        'custom',
        'lockr',
      ]);
      $default = $is_custom ? REQUIREMENT_ERROR : REQUIREMENT_WARNING;
      $default_text = $is_custom ? $text_config
        ->get('admin_page.status.cc.missing.required') : $text_config
        ->get('admin_page.status.cc.missing.partner');
      $reqs[] = [
        'title' => 'Credit Card on File',
        'value' => $has_cc ? 'Yes' : 'No',
        'description' => $has_cc ? $text_config
          ->get('admin_page.status.cc.has') : $default_text,
        'severity' => $has_cc ? REQUIREMENT_OK : $default,
      ];
    }
    lockr_preprocess_status_report($reqs);
    return [
      '#theme' => 'status_report',
      '#requirements' => $reqs,
    ];
  }

}
function lockr_preprocess_status_report(&$reqs) {
  $severities = array(
    REQUIREMENT_INFO => array(
      'title' => t('Info'),
      'status' => 'info',
    ),
    REQUIREMENT_OK => array(
      'title' => t('OK'),
      'status' => 'ok',
    ),
    REQUIREMENT_WARNING => array(
      'title' => t('Warning'),
      'status' => 'warning',
    ),
    REQUIREMENT_ERROR => array(
      'title' => t('Error'),
      'status' => 'error',
    ),
  );
  foreach ($reqs as $i => $requirement) {

    // Always use the explicit requirement severity, if defined. Otherwise,
    // default to REQUIREMENT_OK in the installer to visually confirm that
    // installation requirements are met. And default to REQUIREMENT_INFO to
    // denote neutral information without special visualization.
    if (isset($requirement['severity'])) {
      $severity = $severities[(int) $requirement['severity']];
    }
    elseif (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install') {
      $severity = $severities[REQUIREMENT_OK];
    }
    else {
      $severity = $severities[REQUIREMENT_INFO];
    }
    $reqs[$i]['severity_title'] = $severity['title'];
    $reqs[$i]['severity_status'] = $severity['status'];
  }
}

Functions

Classes