Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Feed / PubSubHubbub / Model / Subscription.php
bloba7a4596b36b213e8fee403d4590d091e62a5e1b6
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Feed\PubSubHubbub\Model;
12 use DateInterval;
13 use DateTime;
14 use Zend\Feed\PubSubHubbub;
16 class Subscription extends AbstractModel implements SubscriptionPersistenceInterface
18 /**
19 * Common DateTime object to assist with unit testing
21 * @var DateTime
23 protected $now;
25 /**
26 * Save subscription to RDMBS
28 * @param array $data
29 * @return bool
30 * @throws PubSubHubbub\Exception\InvalidArgumentException
32 public function setSubscription(array $data)
34 if (!isset($data['id'])) {
35 throw new PubSubHubbub\Exception\InvalidArgumentException(
36 'ID must be set before attempting a save'
39 $result = $this->db->select(array('id' => $data['id']));
40 if ($result && (0 < count($result))) {
41 $data['created_time'] = $result->current()->created_time;
42 $now = $this->getNow();
43 if (array_key_exists('lease_seconds', $data)
44 && $data['lease_seconds']
45 ) {
46 $data['expiration_time'] = $now->add(new DateInterval('PT' . $data['lease_seconds'] . 'S'))
47 ->format('Y-m-d H:i:s');
49 $this->db->update(
50 $data,
51 array('id' => $data['id'])
53 return false;
56 $this->db->insert($data);
57 return true;
60 /**
61 * Get subscription by ID/key
63 * @param string $key
64 * @return array
65 * @throws PubSubHubbub\Exception\InvalidArgumentException
67 public function getSubscription($key)
69 if (empty($key) || !is_string($key)) {
70 throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"'
71 .' of "' . $key . '" must be a non-empty string');
73 $result = $this->db->select(array('id' => $key));
74 if (count($result)) {
75 return $result->current()->getArrayCopy();
77 return false;
80 /**
81 * Determine if a subscription matching the key exists
83 * @param string $key
84 * @return bool
85 * @throws PubSubHubbub\Exception\InvalidArgumentException
87 public function hasSubscription($key)
89 if (empty($key) || !is_string($key)) {
90 throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"'
91 .' of "' . $key . '" must be a non-empty string');
93 $result = $this->db->select(array('id' => $key));
94 if (count($result)) {
95 return true;
97 return false;
101 * Delete a subscription
103 * @param string $key
104 * @return bool
106 public function deleteSubscription($key)
108 $result = $this->db->select(array('id' => $key));
109 if (count($result)) {
110 $this->db->delete(
111 array('id' => $key)
113 return true;
115 return false;
119 * Get a new DateTime or the one injected for testing
121 * @return DateTime
123 public function getNow()
125 if (null === $this->now) {
126 return new DateTime();
128 return $this->now;
132 * Set a DateTime instance for assisting with unit testing
134 * @param DateTime $now
135 * @return Subscription
137 public function setNow(DateTime $now)
139 $this->now = $now;
140 return $this;