3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Feed\PubSubHubbub;
14 use Zend\Http\Request as HttpRequest;
15 use Zend\Stdlib\ArrayUtils;
20 * An array of URLs for all Hub Servers used by the Publisher, and to
21 * which all topic update notifications will be sent.
25 protected $hubUrls = [];
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.
33 protected $updatedTopicUrls = [];
36 * An array of any errors including keys for 'response', 'hubUrl'.
37 * The response is the actual Zend\Http\Response object.
41 protected $errors = [];
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.
49 protected $parameters = [];
52 * Constructor; accepts an array or Zend\Config\Config instance to preset
53 * options for the Publisher without calling all supported setter
56 * @param array|Traversable $options
58 public function __construct($options = null)
60 if ($options !== null) {
61 $this->setOptions($options);
66 * Process any injected configuration options
68 * @param array|Traversable $options Options array or Traversable object
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']);
95 * Add a Hub Server URL supported by 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'
108 $this->hubUrls[] = $url;
113 * Add an array of Hub Server URLs supported by Publisher
118 public function addHubUrls(array $urls)
120 foreach ($urls as $url) {
121 $this->addHubUrl($url);
127 * Remove a Hub Server URL
132 public function removeHubUrl($url)
134 if (! in_array($url, $this->getHubUrls())) {
137 $key = array_search($url, $this->hubUrls);
138 unset($this->hubUrls[$key]);
143 * Return an array of unique Hub Server URLs currently available
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
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'
167 $this->updatedTopicUrls[] = $url;
172 * Add an array of Topic URLs which have been updated
177 public function addUpdatedTopicUrls(array $urls)
179 foreach ($urls as $url) {
180 $this->addUpdatedTopicUrl($url);
186 * Remove an updated topic URL
191 public function removeUpdatedTopicUrl($url)
193 if (! in_array($url, $this->getUpdatedTopicUrls())) {
196 $key = array_search($url, $this->updatedTopicUrls);
197 unset($this->updatedTopicUrls[$key]);
202 * Return an array of unique updated topic URLs currently available
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
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'
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.
247 * @throws Exception\RuntimeException
249 public function notifyAll()
251 $client = $this->_getHttpClient();
252 $hubs = $this->getHubUrls();
254 throw new Exception\RuntimeException('No Hub Server URLs'
255 . ' have been set so no notifications can be sent');
258 foreach ($hubs as $url) {
259 $client->setUri($url);
260 $response = $client->getResponse();
261 if ($response->getStatusCode() !== 204) {
263 'response' => $response,
271 * Add an optional parameter to the update notification requests
273 * @param string $name
274 * @param string|null $value
276 * @throws Exception\InvalidArgumentException
278 public function setParameter($name, $value = null)
280 if (is_array($name)) {
281 $this->setParameters($name);
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);
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;
301 * Add an optional parameter to the update notification requests
303 * @param array $parameters
306 public function setParameters(array $parameters)
308 foreach ($parameters as $name => $value) {
309 $this->setParameter($name, $value);
315 * Remove an optional parameter for the notification requests
317 * @param string $name
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]);
334 * Return an array of optional parameters for notification requests
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.
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
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 // @codingStandardsIgnoreStart
373 protected function _getHttpClient()
375 // @codingStandardsIgnoreEnd
376 $client = PubSubHubbub::getHttpClient();
377 $client->setMethod(HttpRequest::METHOD_POST);
378 $client->setOptions([
379 'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Version::VERSION,
382 $params[] = 'hub.mode=publish';
383 $topics = $this->getUpdatedTopicUrls();
384 if (empty($topics)) {
385 throw new Exception\RuntimeException('No updated topic URLs'
388 foreach ($topics as $topicUrl) {
389 $params[] = 'hub.url=' . urlencode($topicUrl);
391 $optParams = $this->getParameters();
392 foreach ($optParams as $name => $value) {
393 $params[] = urlencode($name) . '=' . urlencode($value);
395 $paramString = implode('&', $params);
396 $client->setRawBody($paramString);