comparison vendor/zendframework/zend-feed/src/PubSubHubbub/Model/Subscription.php @ 0:4c8ae668cc8c

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