1
|
|
|
<?php |
2
|
|
|
namespace Gothick\AkismetClient; |
3
|
|
|
|
4
|
|
|
abstract class ClientResult |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
const PRO_TIP_HEADER = 'X-akismet-pro-tip'; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var string Raw string we got back from the Akismet API as an answer |
11
|
|
|
*/ |
12
|
|
|
protected $raw_result = ''; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string Akismet's X-akismet-pro-tip header, which sometimes has useful extra information. |
16
|
|
|
*/ |
17
|
|
|
protected $pro_tip = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string If there was an X-akismet-debug-header, this is what it contained. |
21
|
|
|
*/ |
22
|
|
|
protected $debug_help; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Make a result, throwing exceptions on invalid responses and non-200 http status codes. |
26
|
|
|
* |
27
|
|
|
* @param \GuzzleHttp\Psr7\Response $response |
28
|
|
|
* @param array $valid_values |
29
|
|
|
* @throws Exception |
30
|
|
|
*/ |
31
|
11 |
|
public function __construct(\GuzzleHttp\Psr7\Response $response, $valid_values) |
32
|
|
|
{ |
33
|
11 |
|
if ($response->hasHeader('X-akismet-debug-help')) |
34
|
|
|
{ |
35
|
1 |
|
$this->debug_help = $response->getHeaderLine('X-akismet-debug-help'); |
36
|
|
|
} |
37
|
11 |
|
if ($response->hasHeader(self::PRO_TIP_HEADER)) |
38
|
|
|
{ |
39
|
|
|
$this->pro_tip = $response->getHeaderLine(self::PRO_TIP_HEADER); |
40
|
|
|
} |
41
|
|
|
|
42
|
11 |
|
$status_code = $response->getStatusCode(); |
43
|
11 |
|
$this->raw_result = (string) $response->getBody(); |
44
|
|
|
|
45
|
11 |
|
if ($status_code != 200 || !in_array($this->raw_result, $valid_values)) |
46
|
|
|
{ |
47
|
|
|
// Our clients are meant to check first |
48
|
3 |
|
$message = 'Invalid ' . $status_code . ' response :' . $this->raw_result . ' in ' . __METHOD__; |
49
|
3 |
|
if ($this->hasDebugHelp()) |
50
|
|
|
{ |
51
|
1 |
|
$message .= ' (debug help: ' . $this->getDebugHelp() . ')'; |
52
|
|
|
} |
53
|
3 |
|
throw new Exception($message); |
54
|
|
|
} |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
public function hasProTip() |
58
|
|
|
{ |
59
|
|
|
return !empty($this->pro_tip); |
60
|
|
|
} |
61
|
|
|
public function getProTip() |
62
|
|
|
{ |
63
|
|
|
return $this->pro_tip; |
64
|
|
|
} |
65
|
3 |
|
public function hasDebugHelp() |
66
|
|
|
{ |
67
|
3 |
|
return !empty($this->debug_help); |
68
|
|
|
} |
69
|
1 |
|
public function getDebugHelp() |
70
|
|
|
{ |
71
|
1 |
|
return $this->pro_tip; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|