Completed
Push — master ( 7ca08e...5a5cfd )
by Matt
02:19
created

ClientResult::__construct()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

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