|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Marek\Toggable\Http\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Http\Response\Response; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class NativeRequestManager |
|
10
|
|
|
* @package Marek\Toggable\Http\Manager |
|
11
|
|
|
*/ |
|
12
|
|
|
class NativeRequestManager implements RequestManagerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \Marek\Toggable\Http\Client\HttpClientInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $client; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Marek\Toggable\API\Security\TokenInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $token; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $uri; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* NativeRequestManager constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Marek\Toggable\Http\Client\HttpClientInterface $client |
|
33
|
|
|
* @param \Marek\Toggable\API\Security\TokenInterface $token |
|
34
|
|
|
* @param string $uri |
|
35
|
|
|
*/ |
|
36
|
14 |
|
public function __construct( |
|
37
|
|
|
\Marek\Toggable\Http\Client\HttpClientInterface $client, |
|
38
|
|
|
\Marek\Toggable\API\Security\TokenInterface $token, |
|
39
|
|
|
$uri |
|
40
|
|
|
) |
|
41
|
|
|
{ |
|
42
|
14 |
|
$this->client = $client; |
|
43
|
14 |
|
$this->token = $token; |
|
44
|
|
|
|
|
45
|
14 |
|
if (empty($uri) || !is_string($uri)) { |
|
46
|
|
|
throw new InvalidArgumentException( |
|
47
|
|
|
sprintf('Please provide valid ur in %s', get_class($this)) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
14 |
|
$this->uri = $uri; |
|
51
|
14 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function request(\Marek\Toggable\API\Http\Request\Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$opts = array( |
|
59
|
|
|
'http' => array( |
|
60
|
|
|
'method' => $request->getMethod(), |
|
61
|
|
|
'header' => array( |
|
62
|
|
|
'Content-type: application/json', |
|
63
|
|
|
), |
|
64
|
|
|
), |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$auth = $this->token->getAuthentication(); |
|
68
|
|
|
$auth = $auth[0] . ':' . $auth[1]; |
|
69
|
|
|
$auth = base64_encode($auth); |
|
70
|
|
|
$opts['http']['header'][] = 'Authorization: Basic ' . $auth; |
|
71
|
|
|
|
|
72
|
|
|
if (!empty($request->getData())) { |
|
73
|
|
|
$opts['http']['content'] = json_encode($request); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$uri = $this->uri . '/' . $request->getUri(); |
|
77
|
|
|
|
|
78
|
|
|
$data = $this->client->send($uri, $opts); |
|
79
|
|
|
|
|
80
|
|
|
var_dump(json_decode($data['data'], true));die; |
|
|
|
|
|
|
81
|
|
|
if (empty($data)) { |
|
|
|
|
|
|
82
|
|
|
throw new \Exception; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($data['data'] === false) { |
|
86
|
|
|
throw new \Exception; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|