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
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
private $response; |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* NativeHttpClient constructor. |
49
|
|
|
* |
50
|
|
|
* @param string $uri |
51
|
|
|
* @param \Marek\Toggable\API\Security\TokenInterface $token |
52
|
|
|
* @param \Marek\Toggable\Http\Converter\ArgumentsConverterInterface $converter |
53
|
|
|
* @param \Marek\Toggable\Http\Parser\HttpResponseParserInterface $parser |
54
|
|
|
* |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
*/ |
57
|
18 |
|
public function __construct($uri, TokenInterface $token, ArgumentsConverterInterface $converter, HttpResponseParserInterface $parser) |
58
|
|
|
{ |
59
|
18 |
|
$this->converter = $converter; |
60
|
18 |
|
$this->parser = $parser; |
61
|
|
|
|
62
|
18 |
|
if (empty($uri) || !is_string($uri) || filter_var($uri, FILTER_VALIDATE_URL) === false) { |
63
|
1 |
|
throw new InvalidArgumentException( |
64
|
1 |
|
sprintf('Please provide valid ur in %s', get_class($this)) |
65
|
|
|
); |
66
|
|
|
} |
67
|
17 |
|
$this->uri = $uri; |
68
|
17 |
|
$this->token = $token; |
69
|
17 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function send() |
75
|
|
|
{ |
76
|
|
|
$context = stream_context_create($this->data['options']); |
77
|
|
|
|
78
|
|
|
$stream = @fopen($this->data['uri'], 'r', false, $context); |
79
|
|
|
|
80
|
|
|
if ($stream == false) { |
81
|
|
|
return array(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$data = stream_get_contents($stream); |
85
|
|
|
|
86
|
|
|
if (empty($data)) { |
87
|
|
|
throw new NetworkException('Toggl server is unreachable'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$metadata = stream_get_meta_data($stream); |
91
|
|
|
|
92
|
|
|
fclose($stream); |
93
|
|
|
|
94
|
|
|
return array('data' => $data, 'metadata' => $metadata['wrapper_data']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
1 |
|
public function prepare(\Marek\Toggable\API\Http\Request\RequestInterface $request) |
101
|
|
|
{ |
102
|
1 |
|
$this->data = $this->converter->convert($request, $this->token, $this->uri); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
1 |
|
public function getResponse() |
109
|
|
|
{ |
110
|
1 |
|
$data = $this->send(); |
111
|
|
|
|
112
|
1 |
|
return $this->parser->parse($data); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.