Permalink
Browse files

Uses exception codes (Bug #19267)

  • Loading branch information...
1 parent 47b5707 commit 7a9200b7e46e493826b9c2e62193be5695930a1c @hm2k hm2k committed Feb 1, 2012
Showing with 13 additions and 30 deletions.
  1. +9 −29 Services/Mailman.php
  2. +4 −1 Services/Mailman/Exception.php
View
@@ -140,7 +140,7 @@ public function setAdminUrl($string)
}
$string = filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
if (!$string) {
- throw new Services_Mailman_Exception('Invalid URL');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::INVALID_URL);
}
$this->adminUrl = trim($string, '/');
return $this;
@@ -193,7 +193,7 @@ protected function fetch($url)
{
$url = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
if (!$url) {
- throw new Services_Mailman_Exception('Invalid URL');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::INVALID_URL);
}
try {
$this->request->setUrl($url);
@@ -205,7 +205,7 @@ protected function fetch($url)
if (strlen($html)>5) {
return $html;
}
- throw new Services_Mailman_Exception('Could not fetch HTML.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_FETCH);
}
/**
@@ -232,7 +232,7 @@ public function lists($assoc = true)
$descs = $xpath->query('/html/body/table[1]/tr/td[2]');
$count = $names->length;
if (!$count) {
- throw new Services_Mailman_Exception('Failed to parse HTML.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
}
$a = array();
for ($i=0;$i < $count;$i++) {
@@ -336,10 +336,6 @@ public function unsubscribe($email)
$query = http_build_query($query, '', '&');
$url = $this->adminUrl . $path . '?' . $query;
$html = $this->fetch($url);
- if (!$html) {
- throw new Services_Mailman_Exception('Unable to fetch HTML.');
- }
-
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
@@ -348,14 +344,13 @@ public function unsubscribe($email)
$h5 = $xpath->query('/html/body/h5');
$h3 = $xpath->query('/html/body/h3');
libxml_clear_errors();
-
if ($h5->item(0) && $h5->item(0)->nodeValue == 'Successfully Unsubscribed:') {
return $this;
}
if ($h3) {
throw new Services_Mailman_Exception(trim($h3->item(0)->nodeValue, ':'));
}
- throw new Services_Mailman_Exception('Failed to parse HTML.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
}
/**
@@ -384,24 +379,19 @@ public function subscribe($email, $invite = false)
$query = http_build_query($query, '', '&');
$url = $this->adminUrl . $path . '?' . $query;
$html = $this->fetch($url);
- if (!$html) {
- throw new Services_Mailman_Exception('Unable to fetch HTML.');
- }
-
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$h5 = $xpath->query('/html/body/h5');
libxml_clear_errors();
-
if ($h5 && $h5->item(0)->nodeValue == 'Successfully subscribed:') {
return $this;
} elseif ($h5) {
throw new Services_Mailman_Exception(trim($h5->item(0)->nodeValue, ':'));
}
- throw new Services_Mailman_Exception('Failed to parse HTML.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
}
/**
@@ -500,7 +490,7 @@ public function setOption($email, $option, $value)
$query['options-submit'] = 'Submit+My+Changes';
$xp = "//input[@name='$option' and @checked]/@value";
} else {
- throw new Services_Mailman_Exception('Invalid option.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::INVALID_OPTION);
}
$query = http_build_query($query, '', '&');
$url = dirname($this->adminUrl) . $path . '?' . $query;
@@ -515,7 +505,7 @@ public function setOption($email, $option, $value)
if ($query->item(0)) {
return $query->item(0)->nodeValue;
}
- throw new Services_Mailman_Exception('Failed to parse HTML.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
}
/**
@@ -532,17 +522,13 @@ public function members()
$query = http_build_query($query, '', '&');
$url = $this->adminUrl . $path . '?' . $query;
$html = $this->fetch($url);
- if (!$html) {
- throw new Services_Mailman_Exception('Unable to fetch HTML.');
- }
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$letters = $xpath->query('/html/body/form/center[1]/table/tr[2]/td/center/a');
libxml_clear_errors();
-
if ($letters->length>0) {
$letters = range('a', 'z');
} else {
@@ -557,9 +543,6 @@ public function members()
$url = $this->adminUrl . $path . '?' . $query;
$html = $this->fetch($url);
}
- if (!$html) {
- throw new Services_Mailman_Exception('Unable to fetch HTML.');
- }
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
@@ -594,9 +577,6 @@ public function version()
$query = http_build_query($query, '', '&');
$url = $this->adminUrl . $path . '?' . $query;
$html = $this->fetch($url);
- if (!$html) {
- throw new Services_Mailman_Exception('Unable to fetch HTML.');
- }
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
@@ -607,7 +587,7 @@ public function version()
if (preg_match('#version ([\d-.]+)#is', $content, $m)) {
return array_pop($m);
}
- throw new Services_Mailman_Exception('Failed to parse HTML.');
+ throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
}
} //end
//eof
@@ -52,5 +52,8 @@
*/
class Services_Mailman_Exception extends Exception
{
-
+ define('INVALID_URL', 'Invalid URL');
+ define('HTML_FETCH', 'Could not fetch HTML');
+ define('HTML_PARSE', 'Failed to parse HTML');
+ define('INVALID_OPTION', 'Invalid Option');
}

0 comments on commit 7a9200b

Please sign in to comment.