You are here

class SiteClient in Lockr 7

Same name and namespace in other branches
  1. 7.2 vendor/lockr/lockr-client/src/SiteClient.php \Lockr\SiteClient

API for site management operations.

Hierarchy

Expanded class hierarchy of SiteClient

File

src/Lockr/SiteClient.php, line 12

Namespace

Lockr
View source
class SiteClient {

  /**
   * @var Lockr The external interface.
   */
  protected $client;

  /**
   * Constructs a SiteClient.
   *
   * @param Lockr $client The external interface.
   */
  public function __construct(Lockr $client) {
    $this->client = $client;
  }

  /**
   * Checks if the current site/env is registered and/or available.
   *
   * @return bool[] Returns a two-value array of booleans:
   *
   * - True if the site is registered with Lockr.
   * - True if the current env is available.
   *
   * @throws ServerException
   * if the server is unavailable or returns an error.
   * @throws ClientException if there was an unexpected client error.
   */
  public function exists() {
    list($status, $body) = $this->client
      ->get('/v1/site/exists');
    $body = json_decode($body, true);
    if (json_last_error() !== JSON_ERROR_NONE || $status >= 500) {
      throw new ServerException();
    }
    if ($status >= 400) {
      throw new ClientException();
    }
    return array(
      isset($body['exists']) ? (bool) $body['exists'] : false,
      isset($body['available']) ? (bool) $body['available'] : false,
    );
  }

  /**
   * Registers the site with Lockr.
   *
   * @param string $email The email to register with.
   * @param string $pass  (optional) The password for authentication.
   * @param string $name  (optional) The site name.
   *
   * @throws ServerException
   * if the server is unavailable or returns an error.
   * @throws ClientException if there was an unexpected client error.
   */
  public function register($email, $pass = null, $name = null) {
    $data = array(
      'email' => $email,
      'name' => $name,
    );
    if (null !== $pass) {
      $auth = "{$email}:{$pass}";
    }
    else {
      $auth = null;
    }
    list($status, $_) = $this->client
      ->post('/v1/site/register', $data, $auth);
    if ($status >= 500) {
      throw new ServerException();
    }
    if ($status >= 400) {
      throw new ClientException();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SiteClient::$client protected property
SiteClient::exists public function Checks if the current site/env is registered and/or available.
SiteClient::register public function Registers the site with Lockr.
SiteClient::__construct public function Constructs a SiteClient.