You are here

PartnerFactory.php in Lockr 8.2

Namespace

Drupal\lockr

File

src/PartnerFactory.php
View source
<?php

namespace Drupal\lockr;

use Drupal\Core\State\StateInterface;
use Lockr\NullPartner;
use Lockr\Partner;

/**
 * Creates partner objects for Lockr clients as well as information
 * about the currently detected partner.
 */
class PartnerFactory {

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

  /**
   * Construct a new partner factory.
   *
   * @param \Drupal\Core\State\StateInterface
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * Get a partner object for use with a Lockr client.
   *
   * @return \Lockr\PartnerInterface
   */
  public function getPartner() {
    $region = $this->state
      ->get('lockr.region', 'us');
    if ($this->state
      ->get('lockr.custom', FALSE)) {
      $cert_path = $this->state
        ->get('lockr.cert');
      if ($cert_path) {
        return new Partner($cert_path, 'custom', $region);
      }
      return new NullPartner($region);
    }
    $detected_partner = $this
      ->detectPartner();
    if (!$detected_partner) {
      return new NullPartner($region);
    }
    return new Partner($detected_partner['cert'], $detected_partner['name'], $region);
  }

  /**
   * Return information about the detected partner.
   *
   * @return array|null
   */
  public function detectPartner() {
    if (defined('PANTHEON_BINDING')) {
      return [
        'name' => 'pantheon',
        'title' => 'Pantheon',
        'description' => "The Pantheor is strong with this one.\n          We're detecting you 're on Pantheon and a friend of theirs is a friend of ours.\n          Welcome to Lockr.",
        'cert' => '/srv/bindings/' . PANTHEON_BINDING . '/certs/binding.pem',
      ];
    }
    return NULL;
  }

}

Classes

Namesort descending Description
PartnerFactory Creates partner objects for Lockr clients as well as information about the currently detected partner.