Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license http://framework.zend.com/license/new-bsd New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Feed\PubSubHubbub\Model;
|
Chris@0
|
11
|
Chris@0
|
12 use DateInterval;
|
Chris@0
|
13 use DateTime;
|
Chris@0
|
14 use Zend\Feed\PubSubHubbub;
|
Chris@0
|
15
|
Chris@0
|
16 class Subscription extends AbstractModel implements SubscriptionPersistenceInterface
|
Chris@0
|
17 {
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Common DateTime object to assist with unit testing
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var DateTime
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $now;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Save subscription to RDMBS
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param array $data
|
Chris@0
|
29 * @return bool
|
Chris@0
|
30 * @throws PubSubHubbub\Exception\InvalidArgumentException
|
Chris@0
|
31 */
|
Chris@0
|
32 public function setSubscription(array $data)
|
Chris@0
|
33 {
|
Chris@12
|
34 if (! isset($data['id'])) {
|
Chris@0
|
35 throw new PubSubHubbub\Exception\InvalidArgumentException(
|
Chris@0
|
36 'ID must be set before attempting a save'
|
Chris@0
|
37 );
|
Chris@0
|
38 }
|
Chris@0
|
39 $result = $this->db->select(['id' => $data['id']]);
|
Chris@0
|
40 if ($result && (0 < count($result))) {
|
Chris@0
|
41 $data['created_time'] = $result->current()->created_time;
|
Chris@0
|
42 $now = $this->getNow();
|
Chris@0
|
43 if (array_key_exists('lease_seconds', $data)
|
Chris@0
|
44 && $data['lease_seconds']
|
Chris@0
|
45 ) {
|
Chris@0
|
46 $data['expiration_time'] = $now->add(new DateInterval('PT' . $data['lease_seconds'] . 'S'))
|
Chris@0
|
47 ->format('Y-m-d H:i:s');
|
Chris@0
|
48 }
|
Chris@0
|
49 $this->db->update(
|
Chris@0
|
50 $data,
|
Chris@0
|
51 ['id' => $data['id']]
|
Chris@0
|
52 );
|
Chris@0
|
53 return false;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 $this->db->insert($data);
|
Chris@0
|
57 return true;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Get subscription by ID/key
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param string $key
|
Chris@0
|
64 * @return array
|
Chris@0
|
65 * @throws PubSubHubbub\Exception\InvalidArgumentException
|
Chris@0
|
66 */
|
Chris@0
|
67 public function getSubscription($key)
|
Chris@0
|
68 {
|
Chris@12
|
69 if (empty($key) || ! is_string($key)) {
|
Chris@0
|
70 throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"'
|
Chris@0
|
71 .' of "' . $key . '" must be a non-empty string');
|
Chris@0
|
72 }
|
Chris@0
|
73 $result = $this->db->select(['id' => $key]);
|
Chris@12
|
74 if ($result && count($result)) {
|
Chris@0
|
75 return $result->current()->getArrayCopy();
|
Chris@0
|
76 }
|
Chris@0
|
77 return false;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Determine if a subscription matching the key exists
|
Chris@0
|
82 *
|
Chris@0
|
83 * @param string $key
|
Chris@0
|
84 * @return bool
|
Chris@0
|
85 * @throws PubSubHubbub\Exception\InvalidArgumentException
|
Chris@0
|
86 */
|
Chris@0
|
87 public function hasSubscription($key)
|
Chris@0
|
88 {
|
Chris@12
|
89 if (empty($key) || ! is_string($key)) {
|
Chris@0
|
90 throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"'
|
Chris@0
|
91 .' of "' . $key . '" must be a non-empty string');
|
Chris@0
|
92 }
|
Chris@0
|
93 $result = $this->db->select(['id' => $key]);
|
Chris@12
|
94 if ($result && count($result)) {
|
Chris@0
|
95 return true;
|
Chris@0
|
96 }
|
Chris@0
|
97 return false;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Delete a subscription
|
Chris@0
|
102 *
|
Chris@0
|
103 * @param string $key
|
Chris@0
|
104 * @return bool
|
Chris@0
|
105 */
|
Chris@0
|
106 public function deleteSubscription($key)
|
Chris@0
|
107 {
|
Chris@0
|
108 $result = $this->db->select(['id' => $key]);
|
Chris@12
|
109 if ($result && count($result)) {
|
Chris@0
|
110 $this->db->delete(
|
Chris@0
|
111 ['id' => $key]
|
Chris@0
|
112 );
|
Chris@0
|
113 return true;
|
Chris@0
|
114 }
|
Chris@0
|
115 return false;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Get a new DateTime or the one injected for testing
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return DateTime
|
Chris@0
|
122 */
|
Chris@0
|
123 public function getNow()
|
Chris@0
|
124 {
|
Chris@0
|
125 if (null === $this->now) {
|
Chris@0
|
126 return new DateTime();
|
Chris@0
|
127 }
|
Chris@0
|
128 return $this->now;
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * Set a DateTime instance for assisting with unit testing
|
Chris@0
|
133 *
|
Chris@0
|
134 * @param DateTime $now
|
Chris@0
|
135 * @return Subscription
|
Chris@0
|
136 */
|
Chris@0
|
137 public function setNow(DateTime $now)
|
Chris@0
|
138 {
|
Chris@0
|
139 $this->now = $now;
|
Chris@0
|
140 return $this;
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|