You are here

class Subscription in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-feed/src/PubSubHubbub/Model/Subscription.php \Zend\Feed\PubSubHubbub\Model\Subscription

Hierarchy

Expanded class hierarchy of Subscription

File

vendor/zendframework/zend-feed/src/PubSubHubbub/Model/Subscription.php, line 16

Namespace

Zend\Feed\PubSubHubbub\Model
View source
class Subscription extends AbstractModel implements SubscriptionPersistenceInterface {

  /**
   * Common DateTime object to assist with unit testing
   *
   * @var DateTime
   */
  protected $now;

  /**
   * Save subscription to RDMBS
   *
   * @param array $data
   * @return bool
   * @throws PubSubHubbub\Exception\InvalidArgumentException
   */
  public function setSubscription(array $data) {
    if (!isset($data['id'])) {
      throw new PubSubHubbub\Exception\InvalidArgumentException('ID must be set before attempting a save');
    }
    $result = $this->db
      ->select([
      'id' => $data['id'],
    ]);
    if ($result && 0 < count($result)) {
      $data['created_time'] = $result
        ->current()->created_time;
      $now = $this
        ->getNow();
      if (array_key_exists('lease_seconds', $data) && $data['lease_seconds']) {
        $data['expiration_time'] = $now
          ->add(new DateInterval('PT' . $data['lease_seconds'] . 'S'))
          ->format('Y-m-d H:i:s');
      }
      $this->db
        ->update($data, [
        'id' => $data['id'],
      ]);
      return false;
    }
    $this->db
      ->insert($data);
    return true;
  }

  /**
   * Get subscription by ID/key
   *
   * @param  string $key
   * @return array
   * @throws PubSubHubbub\Exception\InvalidArgumentException
   */
  public function getSubscription($key) {
    if (empty($key) || !is_string($key)) {
      throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"' . ' of "' . $key . '" must be a non-empty string');
    }
    $result = $this->db
      ->select([
      'id' => $key,
    ]);
    if (count($result)) {
      return $result
        ->current()
        ->getArrayCopy();
    }
    return false;
  }

  /**
   * Determine if a subscription matching the key exists
   *
   * @param  string $key
   * @return bool
   * @throws PubSubHubbub\Exception\InvalidArgumentException
   */
  public function hasSubscription($key) {
    if (empty($key) || !is_string($key)) {
      throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"' . ' of "' . $key . '" must be a non-empty string');
    }
    $result = $this->db
      ->select([
      'id' => $key,
    ]);
    if (count($result)) {
      return true;
    }
    return false;
  }

  /**
   * Delete a subscription
   *
   * @param string $key
   * @return bool
   */
  public function deleteSubscription($key) {
    $result = $this->db
      ->select([
      'id' => $key,
    ]);
    if (count($result)) {
      $this->db
        ->delete([
        'id' => $key,
      ]);
      return true;
    }
    return false;
  }

  /**
   * Get a new DateTime or the one injected for testing
   *
   * @return DateTime
   */
  public function getNow() {
    if (null === $this->now) {
      return new DateTime();
    }
    return $this->now;
  }

  /**
   * Set a DateTime instance for assisting with unit testing
   *
   * @param DateTime $now
   * @return Subscription
   */
  public function setNow(DateTime $now) {
    $this->now = $now;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractModel::$db protected property Zend\Db\TableGateway\TableGatewayInterface instance to host database methods
AbstractModel::__construct public function Constructor
Subscription::$now protected property Common DateTime object to assist with unit testing
Subscription::deleteSubscription public function Delete a subscription Overrides SubscriptionPersistenceInterface::deleteSubscription
Subscription::getNow public function Get a new DateTime or the one injected for testing
Subscription::getSubscription public function Get subscription by ID/key Overrides SubscriptionPersistenceInterface::getSubscription
Subscription::hasSubscription public function Determine if a subscription matching the key exists Overrides SubscriptionPersistenceInterface::hasSubscription
Subscription::setNow public function Set a DateTime instance for assisting with unit testing
Subscription::setSubscription public function Save subscription to RDMBS Overrides SubscriptionPersistenceInterface::setSubscription