|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pin; |
|
4
|
|
|
|
|
5
|
|
|
use Pin\RequestInterface; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
8
|
|
|
use Buzz\Browser; |
|
9
|
|
|
use Buzz\Client\Curl; |
|
10
|
|
|
|
|
11
|
|
|
class Handler |
|
12
|
|
|
{ |
|
13
|
|
|
protected $options = array(); |
|
14
|
|
|
protected $browser; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $options |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(array $options = array()) |
|
22
|
|
|
{ |
|
23
|
|
|
$resolver = new OptionsResolver(); |
|
24
|
|
|
$this->setDefaultOptions($resolver); |
|
25
|
|
|
$this->options = $resolver->resolve($options); |
|
26
|
|
|
|
|
27
|
|
|
$this->init(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set our default options. |
|
32
|
|
|
* |
|
33
|
|
|
* @param OptionsResolver $resolver |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function setDefaultOptions(OptionsResolver $resolver) |
|
36
|
|
|
{ |
|
37
|
|
|
$resolver |
|
38
|
|
|
->setDefaults(array( |
|
39
|
|
|
'host' => function(Options $options) { |
|
40
|
|
|
if (isset($options['test']) && $options['test']) { |
|
41
|
|
|
return 'https://test-api.pin.net.au'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return 'https://api.pin.net.au'; |
|
45
|
|
|
} |
|
46
|
|
|
)) |
|
47
|
|
|
->setRequired(array( |
|
48
|
|
|
'key' |
|
49
|
|
|
)) |
|
50
|
|
|
->setDefined(array( |
|
51
|
|
|
'host', |
|
52
|
|
|
'test', |
|
53
|
|
|
'timeout', |
|
54
|
|
|
)) |
|
55
|
|
|
->setAllowedTypes('host', 'string') |
|
56
|
|
|
->setAllowedTypes('key', 'string') |
|
57
|
|
|
->setAllowedTypes('test', 'bool') |
|
58
|
|
|
->setAllowedTypes('timeout', 'int') |
|
59
|
|
|
; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Initialise |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function init() |
|
66
|
|
|
{ |
|
67
|
|
|
$client = new Curl; |
|
68
|
|
|
$client->setVerifyPeer(false); |
|
69
|
|
|
$client->setOption(CURLOPT_USERPWD, $this->options['key'] . ':'); |
|
70
|
|
|
if (isset($this->options['timeout'])) { |
|
71
|
|
|
$client->setTimeout($this->options['timeout']); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->browser = new Browser($client); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Submit a request to the API. |
|
79
|
|
|
* |
|
80
|
|
|
* @param RequestInterface $request |
|
81
|
|
|
* @return object |
|
82
|
|
|
*/ |
|
83
|
|
|
public function submit(RequestInterface $request) |
|
84
|
|
|
{ |
|
85
|
|
|
$response = $this->browser->submit( |
|
86
|
|
|
$this->options['host'] . $request->getPath(), |
|
87
|
|
|
$request->getData(), |
|
88
|
|
|
$request->getMethod()); |
|
89
|
|
|
|
|
90
|
|
|
return json_decode($response->getContent()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the transport handler, currently only Buzz. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Buzz\Browser |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getTransport() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->browser; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|