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 / Publisher.php
blob0156b638314bb01cd2a304924de7dcd338147b18
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;
12 use Traversable;
13 use Zend\Feed\Uri;
14 use Zend\Http\Request as HttpRequest;
15 use Zend\Stdlib\ArrayUtils;
17 class Publisher
19 /**
20 * An array of URLs for all Hub Servers used by the Publisher, and to
21 * which all topic update notifications will be sent.
23 * @var array
25 protected $hubUrls = array();
27 /**
28 * An array of topic (Atom or RSS feed) URLs which have been updated and
29 * whose updated status will be notified to all Hub Servers.
31 * @var array
33 protected $updatedTopicUrls = array();
35 /**
36 * An array of any errors including keys for 'response', 'hubUrl'.
37 * The response is the actual Zend\Http\Response object.
39 * @var array
41 protected $errors = array();
43 /**
44 * An array of topic (Atom or RSS feed) URLs which have been updated and
45 * whose updated status will be notified to all Hub Servers.
47 * @var array
49 protected $parameters = array();
51 /**
52 * Constructor; accepts an array or Zend\Config\Config instance to preset
53 * options for the Publisher without calling all supported setter
54 * methods in turn.
56 * @param array|Traversable $options
58 public function __construct($options = null)
60 if ($options !== null) {
61 $this->setOptions($options);
65 /**
66 * Process any injected configuration options
68 * @param array|Traversable $options Options array or Traversable object
69 * @return Publisher
70 * @throws Exception\InvalidArgumentException
72 public function setOptions($options)
74 if ($options instanceof Traversable) {
75 $options = ArrayUtils::iteratorToArray($options);
78 if (!is_array($options)) {
79 throw new Exception\InvalidArgumentException('Array or Traversable object'
80 . 'expected, got ' . gettype($options));
82 if (array_key_exists('hubUrls', $options)) {
83 $this->addHubUrls($options['hubUrls']);
85 if (array_key_exists('updatedTopicUrls', $options)) {
86 $this->addUpdatedTopicUrls($options['updatedTopicUrls']);
88 if (array_key_exists('parameters', $options)) {
89 $this->setParameters($options['parameters']);
91 return $this;
94 /**
95 * Add a Hub Server URL supported by Publisher
97 * @param string $url
98 * @return Publisher
99 * @throws Exception\InvalidArgumentException
101 public function addHubUrl($url)
103 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) {
104 throw new Exception\InvalidArgumentException('Invalid parameter "url"'
105 . ' of "' . $url . '" must be a non-empty string and a valid'
106 . 'URL');
108 $this->hubUrls[] = $url;
109 return $this;
113 * Add an array of Hub Server URLs supported by Publisher
115 * @param array $urls
116 * @return Publisher
118 public function addHubUrls(array $urls)
120 foreach ($urls as $url) {
121 $this->addHubUrl($url);
123 return $this;
127 * Remove a Hub Server URL
129 * @param string $url
130 * @return Publisher
132 public function removeHubUrl($url)
134 if (!in_array($url, $this->getHubUrls())) {
135 return $this;
137 $key = array_search($url, $this->hubUrls);
138 unset($this->hubUrls[$key]);
139 return $this;
143 * Return an array of unique Hub Server URLs currently available
145 * @return array
147 public function getHubUrls()
149 $this->hubUrls = array_unique($this->hubUrls);
150 return $this->hubUrls;
154 * Add a URL to a topic (Atom or RSS feed) which has been updated
156 * @param string $url
157 * @return Publisher
158 * @throws Exception\InvalidArgumentException
160 public function addUpdatedTopicUrl($url)
162 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) {
163 throw new Exception\InvalidArgumentException('Invalid parameter "url"'
164 . ' of "' . $url . '" must be a non-empty string and a valid'
165 . 'URL');
167 $this->updatedTopicUrls[] = $url;
168 return $this;
172 * Add an array of Topic URLs which have been updated
174 * @param array $urls
175 * @return Publisher
177 public function addUpdatedTopicUrls(array $urls)
179 foreach ($urls as $url) {
180 $this->addUpdatedTopicUrl($url);
182 return $this;
186 * Remove an updated topic URL
188 * @param string $url
189 * @return Publisher
191 public function removeUpdatedTopicUrl($url)
193 if (!in_array($url, $this->getUpdatedTopicUrls())) {
194 return $this;
196 $key = array_search($url, $this->updatedTopicUrls);
197 unset($this->updatedTopicUrls[$key]);
198 return $this;
202 * Return an array of unique updated topic URLs currently available
204 * @return array
206 public function getUpdatedTopicUrls()
208 $this->updatedTopicUrls = array_unique($this->updatedTopicUrls);
209 return $this->updatedTopicUrls;
213 * Notifies a single Hub Server URL of changes
215 * @param string $url The Hub Server's URL
216 * @return void
217 * @throws Exception\InvalidArgumentException
218 * @throws Exception\RuntimeException
220 public function notifyHub($url)
222 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) {
223 throw new Exception\InvalidArgumentException('Invalid parameter "url"'
224 . ' of "' . $url . '" must be a non-empty string and a valid'
225 . 'URL');
227 $client = $this->_getHttpClient();
228 $client->setUri($url);
229 $response = $client->getResponse();
230 if ($response->getStatusCode() !== 204) {
231 throw new Exception\RuntimeException('Notification to Hub Server '
232 . 'at "' . $url . '" appears to have failed with a status code of "'
233 . $response->getStatusCode() . '" and message "'
234 . $response->getContent() . '"');
239 * Notifies all Hub Server URLs of changes
241 * If a Hub notification fails, certain data will be retained in an
242 * an array retrieved using getErrors(), if a failure occurs for any Hubs
243 * the isSuccess() check will return FALSE. This method is designed not
244 * to needlessly fail with an Exception/Error unless from Zend\Http\Client.
246 * @return void
247 * @throws Exception\RuntimeException
249 public function notifyAll()
251 $client = $this->_getHttpClient();
252 $hubs = $this->getHubUrls();
253 if (empty($hubs)) {
254 throw new Exception\RuntimeException('No Hub Server URLs'
255 . ' have been set so no notifications can be sent');
257 $this->errors = array();
258 foreach ($hubs as $url) {
259 $client->setUri($url);
260 $response = $client->getResponse();
261 if ($response->getStatusCode() !== 204) {
262 $this->errors[] = array(
263 'response' => $response,
264 'hubUrl' => $url
271 * Add an optional parameter to the update notification requests
273 * @param string $name
274 * @param string|null $value
275 * @return Publisher
276 * @throws Exception\InvalidArgumentException
278 public function setParameter($name, $value = null)
280 if (is_array($name)) {
281 $this->setParameters($name);
282 return $this;
284 if (empty($name) || !is_string($name)) {
285 throw new Exception\InvalidArgumentException('Invalid parameter "name"'
286 . ' of "' . $name . '" must be a non-empty string');
288 if ($value === null) {
289 $this->removeParameter($name);
290 return $this;
292 if (empty($value) || (!is_string($value) && $value !== null)) {
293 throw new Exception\InvalidArgumentException('Invalid parameter "value"'
294 . ' of "' . $value . '" must be a non-empty string');
296 $this->parameters[$name] = $value;
297 return $this;
301 * Add an optional parameter to the update notification requests
303 * @param array $parameters
304 * @return Publisher
306 public function setParameters(array $parameters)
308 foreach ($parameters as $name => $value) {
309 $this->setParameter($name, $value);
311 return $this;
315 * Remove an optional parameter for the notification requests
317 * @param string $name
318 * @return Publisher
319 * @throws Exception\InvalidArgumentException
321 public function removeParameter($name)
323 if (empty($name) || !is_string($name)) {
324 throw new Exception\InvalidArgumentException('Invalid parameter "name"'
325 . ' of "' . $name . '" must be a non-empty string');
327 if (array_key_exists($name, $this->parameters)) {
328 unset($this->parameters[$name]);
330 return $this;
334 * Return an array of optional parameters for notification requests
336 * @return array
338 public function getParameters()
340 return $this->parameters;
344 * Returns a boolean indicator of whether the notifications to Hub
345 * Servers were ALL successful. If even one failed, FALSE is returned.
347 * @return bool
349 public function isSuccess()
351 return !(count($this->errors) != 0);
355 * Return an array of errors met from any failures, including keys:
356 * 'response' => the Zend\Http\Response object from the failure
357 * 'hubUrl' => the URL of the Hub Server whose notification failed
359 * @return array
361 public function getErrors()
363 return $this->errors;
367 * Get a basic prepared HTTP client for use
369 * @return \Zend\Http\Client
370 * @throws Exception\RuntimeException
372 protected function _getHttpClient()
374 $client = PubSubHubbub::getHttpClient();
375 $client->setMethod(HttpRequest::METHOD_POST);
376 $client->setOptions(array(
377 'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Version::VERSION,
379 $params = array();
380 $params[] = 'hub.mode=publish';
381 $topics = $this->getUpdatedTopicUrls();
382 if (empty($topics)) {
383 throw new Exception\RuntimeException('No updated topic URLs'
384 . ' have been set');
386 foreach ($topics as $topicUrl) {
387 $params[] = 'hub.url=' . urlencode($topicUrl);
389 $optParams = $this->getParameters();
390 foreach ($optParams as $name => $value) {
391 $params[] = urlencode($name) . '=' . urlencode($value);
393 $paramString = implode('&', $params);
394 $client->setRawBody($paramString);
395 return $client;