1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\Http\Client; |
4
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Exception\Http\NetworkException; |
6
|
|
|
use Marek\Toggable\API\Security\TokenInterface; |
7
|
|
|
use Marek\Toggable\Http\Converter\ArgumentsConverterInterface; |
8
|
|
|
use Marek\Toggable\Http\Parser\HttpResponseParserInterface; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class NativeHttpClient |
13
|
|
|
* @package Marek\Toggable\Http\Client |
14
|
|
|
*/ |
15
|
|
|
class NativeHttpClient implements HttpClientInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Marek\Toggable\Http\Converter\ArgumentsConverterInterface |
19
|
|
|
*/ |
20
|
|
|
private $converter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Marek\Toggable\Http\Parser\HttpResponseParserInterface |
24
|
|
|
*/ |
25
|
|
|
private $parser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
private $uri; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Marek\Toggable\API\Security\TokenInterface |
34
|
|
|
*/ |
35
|
|
|
private $token; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $data; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* NativeHttpClient constructor. |
44
|
|
|
* |
45
|
|
|
* @param string $uri |
46
|
|
|
* @param \Marek\Toggable\API\Security\TokenInterface $token |
47
|
|
|
* @param \Marek\Toggable\Http\Converter\ArgumentsConverterInterface $converter |
48
|
|
|
* @param \Marek\Toggable\Http\Parser\HttpResponseParserInterface $parser |
49
|
|
|
* |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
*/ |
52
|
18 |
|
public function __construct($uri, TokenInterface $token, ArgumentsConverterInterface $converter, HttpResponseParserInterface $parser) |
53
|
|
|
{ |
54
|
18 |
|
$this->converter = $converter; |
55
|
18 |
|
$this->parser = $parser; |
56
|
|
|
|
57
|
18 |
|
if (empty($uri) || !is_string($uri) || filter_var($uri, FILTER_VALIDATE_URL) === false) { |
58
|
1 |
|
throw new InvalidArgumentException( |
59
|
1 |
|
sprintf('Please provide valid ur in %s', get_class($this)) |
60
|
1 |
|
); |
61
|
|
|
} |
62
|
17 |
|
$this->uri = $uri; |
63
|
17 |
|
$this->token = $token; |
64
|
17 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function send() |
70
|
|
|
{ |
71
|
|
|
$context = stream_context_create($this->data['options']); |
72
|
|
|
|
73
|
|
|
$stream = @fopen($this->data['uri'], 'r', false, $context); |
74
|
|
|
|
75
|
|
|
if ($stream === false) { |
76
|
|
|
return array(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$data = stream_get_contents($stream); |
80
|
|
|
|
81
|
|
|
if (empty($data)) { |
82
|
|
|
throw new NetworkException('Toggl server is unreachable'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$metadata = stream_get_meta_data($stream); |
86
|
|
|
|
87
|
|
|
fclose($stream); |
88
|
|
|
|
89
|
|
|
return array('data' => $data, 'metadata' => $metadata['wrapper_data']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
1 |
|
public function prepare(\Marek\Toggable\API\Http\Request\RequestInterface $request) |
96
|
|
|
{ |
97
|
1 |
|
$this->data = $this->converter->convert($request, $this->token, $this->uri); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
1 |
|
public function getResponse() |
104
|
|
|
{ |
105
|
1 |
|
$data = $this->send(); |
106
|
|
|
|
107
|
1 |
|
return $this->parser->parse($data); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|