Chris@0: setOptions($options); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Process any injected configuration options Chris@0: * Chris@0: * @param array|Traversable $options Options array or Traversable object Chris@0: * @return Publisher Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function setOptions($options) Chris@0: { Chris@0: if ($options instanceof Traversable) { Chris@0: $options = ArrayUtils::iteratorToArray($options); Chris@0: } Chris@0: Chris@12: if (! is_array($options)) { Chris@0: throw new Exception\InvalidArgumentException('Array or Traversable object' Chris@0: . 'expected, got ' . gettype($options)); Chris@0: } Chris@0: if (array_key_exists('hubUrls', $options)) { Chris@0: $this->addHubUrls($options['hubUrls']); Chris@0: } Chris@0: if (array_key_exists('updatedTopicUrls', $options)) { Chris@0: $this->addUpdatedTopicUrls($options['updatedTopicUrls']); Chris@0: } Chris@0: if (array_key_exists('parameters', $options)) { Chris@0: $this->setParameters($options['parameters']); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add a Hub Server URL supported by Publisher Chris@0: * Chris@0: * @param string $url Chris@0: * @return Publisher Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function addHubUrl($url) Chris@0: { Chris@12: if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { Chris@0: throw new Exception\InvalidArgumentException('Invalid parameter "url"' Chris@0: . ' of "' . $url . '" must be a non-empty string and a valid' Chris@0: . 'URL'); Chris@0: } Chris@0: $this->hubUrls[] = $url; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add an array of Hub Server URLs supported by Publisher Chris@0: * Chris@0: * @param array $urls Chris@0: * @return Publisher Chris@0: */ Chris@0: public function addHubUrls(array $urls) Chris@0: { Chris@0: foreach ($urls as $url) { Chris@0: $this->addHubUrl($url); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Remove a Hub Server URL Chris@0: * Chris@0: * @param string $url Chris@0: * @return Publisher Chris@0: */ Chris@0: public function removeHubUrl($url) Chris@0: { Chris@12: if (! in_array($url, $this->getHubUrls())) { Chris@0: return $this; Chris@0: } Chris@0: $key = array_search($url, $this->hubUrls); Chris@0: unset($this->hubUrls[$key]); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return an array of unique Hub Server URLs currently available Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getHubUrls() Chris@0: { Chris@0: $this->hubUrls = array_unique($this->hubUrls); Chris@0: return $this->hubUrls; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add a URL to a topic (Atom or RSS feed) which has been updated Chris@0: * Chris@0: * @param string $url Chris@0: * @return Publisher Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function addUpdatedTopicUrl($url) Chris@0: { Chris@12: if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { Chris@0: throw new Exception\InvalidArgumentException('Invalid parameter "url"' Chris@0: . ' of "' . $url . '" must be a non-empty string and a valid' Chris@0: . 'URL'); Chris@0: } Chris@0: $this->updatedTopicUrls[] = $url; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add an array of Topic URLs which have been updated Chris@0: * Chris@0: * @param array $urls Chris@0: * @return Publisher Chris@0: */ Chris@0: public function addUpdatedTopicUrls(array $urls) Chris@0: { Chris@0: foreach ($urls as $url) { Chris@0: $this->addUpdatedTopicUrl($url); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Remove an updated topic URL Chris@0: * Chris@0: * @param string $url Chris@0: * @return Publisher Chris@0: */ Chris@0: public function removeUpdatedTopicUrl($url) Chris@0: { Chris@12: if (! in_array($url, $this->getUpdatedTopicUrls())) { Chris@0: return $this; Chris@0: } Chris@0: $key = array_search($url, $this->updatedTopicUrls); Chris@0: unset($this->updatedTopicUrls[$key]); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return an array of unique updated topic URLs currently available Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getUpdatedTopicUrls() Chris@0: { Chris@0: $this->updatedTopicUrls = array_unique($this->updatedTopicUrls); Chris@0: return $this->updatedTopicUrls; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Notifies a single Hub Server URL of changes Chris@0: * Chris@0: * @param string $url The Hub Server's URL Chris@0: * @return void Chris@0: * @throws Exception\InvalidArgumentException Chris@0: * @throws Exception\RuntimeException Chris@0: */ Chris@0: public function notifyHub($url) Chris@0: { Chris@12: if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { Chris@0: throw new Exception\InvalidArgumentException('Invalid parameter "url"' Chris@0: . ' of "' . $url . '" must be a non-empty string and a valid' Chris@0: . 'URL'); Chris@0: } Chris@0: $client = $this->_getHttpClient(); Chris@0: $client->setUri($url); Chris@0: $response = $client->getResponse(); Chris@0: if ($response->getStatusCode() !== 204) { Chris@0: throw new Exception\RuntimeException('Notification to Hub Server ' Chris@0: . 'at "' . $url . '" appears to have failed with a status code of "' Chris@0: . $response->getStatusCode() . '" and message "' Chris@0: . $response->getContent() . '"'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Notifies all Hub Server URLs of changes Chris@0: * Chris@0: * If a Hub notification fails, certain data will be retained in an Chris@0: * an array retrieved using getErrors(), if a failure occurs for any Hubs Chris@0: * the isSuccess() check will return FALSE. This method is designed not Chris@0: * to needlessly fail with an Exception/Error unless from Zend\Http\Client. Chris@0: * Chris@0: * @return void Chris@0: * @throws Exception\RuntimeException Chris@0: */ Chris@0: public function notifyAll() Chris@0: { Chris@0: $client = $this->_getHttpClient(); Chris@0: $hubs = $this->getHubUrls(); Chris@0: if (empty($hubs)) { Chris@0: throw new Exception\RuntimeException('No Hub Server URLs' Chris@0: . ' have been set so no notifications can be sent'); Chris@0: } Chris@0: $this->errors = []; Chris@0: foreach ($hubs as $url) { Chris@0: $client->setUri($url); Chris@0: $response = $client->getResponse(); Chris@0: if ($response->getStatusCode() !== 204) { Chris@0: $this->errors[] = [ Chris@0: 'response' => $response, Chris@0: 'hubUrl' => $url Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add an optional parameter to the update notification requests Chris@0: * Chris@0: * @param string $name Chris@0: * @param string|null $value Chris@0: * @return Publisher Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function setParameter($name, $value = null) Chris@0: { Chris@0: if (is_array($name)) { Chris@0: $this->setParameters($name); Chris@0: return $this; Chris@0: } Chris@12: if (empty($name) || ! is_string($name)) { Chris@0: throw new Exception\InvalidArgumentException('Invalid parameter "name"' Chris@0: . ' of "' . $name . '" must be a non-empty string'); Chris@0: } Chris@0: if ($value === null) { Chris@0: $this->removeParameter($name); Chris@0: return $this; Chris@0: } Chris@12: if (empty($value) || (! is_string($value) && $value !== null)) { Chris@0: throw new Exception\InvalidArgumentException('Invalid parameter "value"' Chris@0: . ' of "' . $value . '" must be a non-empty string'); Chris@0: } Chris@0: $this->parameters[$name] = $value; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add an optional parameter to the update notification requests Chris@0: * Chris@0: * @param array $parameters Chris@0: * @return Publisher Chris@0: */ Chris@0: public function setParameters(array $parameters) Chris@0: { Chris@0: foreach ($parameters as $name => $value) { Chris@0: $this->setParameter($name, $value); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Remove an optional parameter for the notification requests Chris@0: * Chris@0: * @param string $name Chris@0: * @return Publisher Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function removeParameter($name) Chris@0: { Chris@12: if (empty($name) || ! is_string($name)) { Chris@0: throw new Exception\InvalidArgumentException('Invalid parameter "name"' Chris@0: . ' of "' . $name . '" must be a non-empty string'); Chris@0: } Chris@0: if (array_key_exists($name, $this->parameters)) { Chris@0: unset($this->parameters[$name]); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return an array of optional parameters for notification requests Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getParameters() Chris@0: { Chris@0: return $this->parameters; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a boolean indicator of whether the notifications to Hub Chris@0: * Servers were ALL successful. If even one failed, FALSE is returned. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isSuccess() Chris@0: { Chris@17: return ! $this->errors; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return an array of errors met from any failures, including keys: Chris@0: * 'response' => the Zend\Http\Response object from the failure Chris@0: * 'hubUrl' => the URL of the Hub Server whose notification failed Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getErrors() Chris@0: { Chris@0: return $this->errors; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a basic prepared HTTP client for use Chris@0: * Chris@0: * @return \Zend\Http\Client Chris@0: * @throws Exception\RuntimeException Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _getHttpClient() Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $client = PubSubHubbub::getHttpClient(); Chris@0: $client->setMethod(HttpRequest::METHOD_POST); Chris@0: $client->setOptions([ Chris@0: 'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Version::VERSION, Chris@0: ]); Chris@0: $params = []; Chris@0: $params[] = 'hub.mode=publish'; Chris@0: $topics = $this->getUpdatedTopicUrls(); Chris@0: if (empty($topics)) { Chris@0: throw new Exception\RuntimeException('No updated topic URLs' Chris@0: . ' have been set'); Chris@0: } Chris@0: foreach ($topics as $topicUrl) { Chris@0: $params[] = 'hub.url=' . urlencode($topicUrl); Chris@0: } Chris@0: $optParams = $this->getParameters(); Chris@0: foreach ($optParams as $name => $value) { Chris@0: $params[] = urlencode($name) . '=' . urlencode($value); Chris@0: } Chris@0: $paramString = implode('&', $params); Chris@0: $client->setRawBody($paramString); Chris@0: return $client; Chris@0: } Chris@0: }