Skip to content

Commit 1213f89

Browse files
authored
Merge pull request #8 from CupOfTea696/master
Follow PSR-2 & other conventions
2 parents 25cf009 + a848f9e commit 1213f89

File tree

2 files changed

+116
-64
lines changed

2 files changed

+116
-64
lines changed

.styleci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
preset: recommended
2+
3+
enabled:
4+
- concat_with_spaces
5+
- no_useless_else
6+
- not_operator_with_successor_space
7+
8+
disabled:
9+
- concat_without_spaces
10+
11+
finder:
12+
not-name:
13+
- "example.php"

src/Subscriber.php

Lines changed: 103 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,198 @@
11
<?php
22
/**
3-
* A PHP client library for pubsubhubbub
3+
* A PHP client library for pubsubhubbub.
44
*
55
* @link http://code.google.com/p/pubsubhubbub/
6+
*
67
* @author Josh Fraser | joshfraser.com | josh@eventvue.com
78
* @license Apache License 2.0
89
*/
9-
1010
namespace Pubsubhubbub\Subscriber;
1111

12-
class Subscriber {
12+
use InvalidArgumentException;
13+
14+
class Subscriber
15+
{
1316
/**
14-
* put your google key here
15-
* required if you want to use the google feed API to lookup RSS feeds
17+
* Put your google key here.
18+
* Required if you want to use the google feed API to lookup RSS feeds.
1619
*
1720
* @var string
1821
*/
19-
protected $google_key = "";
22+
protected $google_key = '';
2023

21-
/** @var string */
24+
/**
25+
* @var string
26+
*/
2227
protected $hub_url;
2328

24-
/** @var string */
29+
/**
30+
* @var string
31+
*/
2532
protected $callback_url;
2633

27-
/** @var string */
34+
/**
35+
* @var string
36+
*/
2837
protected $credentials;
2938

3039
/**
3140
* @var string accepted values are "async" and "sync"
3241
*/
33-
protected $verify = "async";
42+
protected $verify = 'async';
3443

35-
/** @var string */
44+
/**
45+
* @var string
46+
*/
3647
protected $verify_token;
3748

38-
/** @var int */
49+
/**
50+
* @var string
51+
*/
3952
protected $lease_seconds;
4053

4154
/**
42-
* create a new Subscriber (credentials added for SuperFeedr support)
55+
* Create a new Subscriber (credentials added for SuperFeedr support).
4356
*
4457
* @param string $hub_url
4558
* @param string $callback_url
4659
* @param string $credentials
4760
*/
48-
public function __construct($hub_url, $callback_url, $credentials = false) {
49-
50-
if (!isset($hub_url))
51-
throw new Exception('Please specify a hub url');
61+
public function __construct($hub_url, $callback_url, $credentials = false)
62+
{
63+
if (! isset($hub_url)) {
64+
throw new InvalidArgumentException('Please specify a hub url');
65+
}
5266

53-
if (!preg_match("|^https?://|i",$hub_url))
54-
throw new Exception('The specified hub url does not appear to be valid: '.$hub_url);
67+
if (! preg_match('|^https?://|i', $hub_url)) {
68+
throw new InvalidArgumentException('The specified hub url does not appear to be valid: ' . $hub_url);
69+
}
5570

56-
if (!isset($callback_url))
57-
throw new Exception('Please specify a callback');
71+
if (! isset($callback_url)) {
72+
throw new InvalidArgumentException('Please specify a callback');
73+
}
5874

5975
$this->hub_url = $hub_url;
6076
$this->callback_url = $callback_url;
6177
$this->credentials = $credentials;
6278
}
6379

6480
/**
65-
* $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site
81+
* $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site.
82+
*
83+
* @param string $url
84+
* @param callable $http_function
6685
*
67-
* @param string $url
68-
* @param callable $http_function
6986
* @return string
7087
*/
71-
public function find_feed($url, $http_function = false) {
88+
public function find_feed($url, $http_function = false)
89+
{
7290
// using google feed API
73-
$url = "http://ajax.googleapis.com/ajax/services/feed/lookup?key={$this->google_key}&v=1.0&q=".urlencode($url);
91+
$url = "http://ajax.googleapis.com/ajax/services/feed/lookup?key={$this->google_key}&v=1.0&q=" . urlencode($url);
7492
// fetch the content
75-
if ($http_function)
93+
if ($http_function) {
7694
$response = $http_function($url);
77-
else
95+
} else {
7896
$response = $this->http($url);
97+
}
7998

8099
$result = json_decode($response, true);
81100
$rss_url = $result['responseData']['url'];
101+
82102
return $rss_url;
83103
}
84104

85105
/**
86-
* @param string $topic_url
87-
* @param callable $http_function
106+
* Subscribe to a topic.
107+
*
108+
* @param string $topic_url
109+
* @param callable $http_function
110+
*
88111
* @return mixed
89112
*/
90-
public function subscribe($topic_url, $http_function = false) {
91-
return $this->change_subscription("subscribe", $topic_url, $http_function);
113+
public function subscribe($topic_url, $http_function = false)
114+
{
115+
return $this->change_subscription('subscribe', $topic_url, $http_function);
92116
}
93117

94118
/**
95-
* @param string $topic_url
96-
* @param callable $http_function
119+
* Unsubscribe from a topic.
120+
*
121+
* @param string $topic_url
122+
* @param callable $http_function
123+
*
97124
* @return mixed
98125
*/
99-
public function unsubscribe($topic_url, $http_function = false) {
100-
return $this->change_subscription("unsubscribe", $topic_url, $http_function);
126+
public function unsubscribe($topic_url, $http_function = false)
127+
{
128+
return $this->change_subscription('unsubscribe', $topic_url, $http_function);
101129
}
102130

103131
/**
104-
* helper function since sub/unsub are handled the same way
132+
* Helper function since sub/unsub are handled the same way.
133+
*
134+
* @param string $mode
135+
* @param string $topic_url
136+
* @param callable $http_function
105137
*
106-
* @param string $mode
107-
* @param string $topic_url
108-
* @param callable $http_function
109138
* @return mixed
110139
*/
111-
private function change_subscription($mode, $topic_url, $http_function = false) {
112-
if (!isset($topic_url))
113-
throw new Exception('Please specify a topic url');
140+
private function change_subscription($mode, $topic_url, $http_function = false)
141+
{
142+
if (! isset($topic_url)) {
143+
throw new InvalidArgumentException('Please specify a topic url');
144+
}
114145

115146
// lightweight check that we're actually working w/ a valid url
116-
if (!preg_match("|^https?://|i",$topic_url))
117-
throw new Exception('The specified topic url does not appear to be valid: '.$topic_url);
147+
if (! preg_match('|^https?://|i', $topic_url)) {
148+
throw new InvalidArgumentException('The specified topic url does not appear to be valid: ' . $topic_url);
149+
}
118150

119151
// set the mode subscribe/unsubscribe
120-
$post_string = "hub.mode=".$mode;
121-
$post_string .= "&hub.callback=".urlencode($this->callback_url);
122-
$post_string .= "&hub.verify=".$this->verify;
123-
$post_string .= "&hub.verify_token=".$this->verify_token;
124-
$post_string .= "&hub.lease_seconds=".$this->lease_seconds;
152+
$post_string = 'hub.mode=' . $mode;
153+
$post_string .= '&hub.callback=' . urlencode($this->callback_url);
154+
$post_string .= '&hub.verify=' . $this->verify;
155+
$post_string .= '&hub.verify_token=' . $this->verify_token;
156+
$post_string .= '&hub.lease_seconds=' . $this->lease_seconds;
125157

126158
// append the topic url parameters
127-
$post_string .= "&hub.topic=".urlencode($topic_url);
159+
$post_string .= '&hub.topic=' . urlencode($topic_url);
128160

129161
// make the http post request and return true/false
130162
// easy to over-write to use your own http function
131-
if ($http_function)
132-
return call_user_func_array($http_function,array($this->hub_url,$post_string));
133-
else
134-
return $this->http($this->hub_url,$post_string);
163+
if ($http_function) {
164+
return call_user_func_array($http_function, [$this->hub_url, $post_string]);
165+
}
166+
167+
return $this->http($this->hub_url, $post_string);
135168
}
136169

137170
/**
138-
* default http function that uses curl to post to the hub endpoint
171+
* Default http function that uses curl to post to the hub endpoint.
172+
*
173+
* @param string $url
174+
* @param string $post_string
139175
*
140-
* @param string $url
141-
* @param string $post_string
142176
* @return mixed
143177
*/
144-
private function http($url, $post_string) {
178+
private function http($url, $post_string)
179+
{
145180

146181
// add any additional curl options here
147-
$options = array(CURLOPT_URL => $url,
148-
CURLOPT_USERAGENT => "PubSubHubbub-Subscriber-PHP/1.0",
149-
CURLOPT_RETURNTRANSFER => true);
182+
$options = [
183+
CURLOPT_URL => $url,
184+
CURLOPT_USERAGENT => 'PubSubHubbub-Subscriber-PHP/1.0',
185+
CURLOPT_RETURNTRANSFER => true,
186+
];
150187

151188
if ($post_string) {
152189
$options[CURLOPT_POST] = true;
153190
$options[CURLOPT_POSTFIELDS] = $post_string;
154191
}
155192

156-
if ($this->credentials)
193+
if ($this->credentials) {
157194
$options[CURLOPT_USERPWD] = $this->credentials;
195+
}
158196

159197
$ch = curl_init();
160198
curl_setopt_array($ch, $options);
@@ -163,9 +201,10 @@ private function http($url, $post_string) {
163201
$info = curl_getinfo($ch);
164202

165203
// all good -- anything in the 200 range
166-
if (substr($info['http_code'],0,1) == "2") {
204+
if (substr($info['http_code'], 0, 1) == '2') {
167205
return $response;
168206
}
207+
169208
return false;
170209
}
171210
}

0 commit comments

Comments
 (0)