1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Gockets; |
4
|
|
|
|
5
|
|
|
use Gockets\Adapter\ChannelAdapter; |
6
|
|
|
use Gockets\Adapter\ResponseAdapter; |
7
|
|
|
use Gockets\Contract\AdapterInterface; |
8
|
|
|
use Gockets\Contract\GocketsInterface; |
9
|
|
|
use Gockets\Exception\ChannelNotFoundException; |
10
|
|
|
use Gockets\Factory\HttpFactory; |
11
|
|
|
use Gockets\Model\Channel; |
12
|
|
|
use Gockets\Model\Params; |
13
|
|
|
use Gockets\Model\Response; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Gockets Client |
17
|
|
|
* |
18
|
|
|
* Implements available Gockets methods. |
19
|
|
|
* |
20
|
|
|
* @package Gockets |
21
|
|
|
* @author Artem Zakharchenko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Client implements GocketsInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Help to convert json into defined Channel objects. |
27
|
|
|
* |
28
|
|
|
* @var AdapterInterface |
29
|
|
|
*/ |
30
|
|
|
private $channelAdapter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Help to convert json into defined Response objects. |
34
|
|
|
* |
35
|
|
|
* @var AdapterInterface |
36
|
|
|
*/ |
37
|
|
|
private $responseAdapter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* HTTP client to communicate with daemon. |
41
|
|
|
* |
42
|
|
|
* @var \GuzzleHttp\Client |
43
|
|
|
*/ |
44
|
|
|
private static $http; |
45
|
|
|
|
46
|
|
|
public function __construct(Params $params) |
47
|
|
|
{ |
48
|
|
|
$this->channelAdapter = new ChannelAdapter(); |
49
|
|
|
$this->responseAdapter = new ResponseAdapter(); |
50
|
|
|
self::$http = HttpFactory::createHttpClient($params); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function prepare(?string $hookUrl = null): Channel |
54
|
|
|
{ |
55
|
2 |
|
if (is_null($hookUrl)) { |
56
|
1 |
|
$options = []; |
57
|
|
|
} else { |
58
|
|
|
$options = [ |
59
|
1 |
|
\GuzzleHttp\RequestOptions::JSON => [ |
60
|
1 |
|
'subscriber_message_hook_url' => $hookUrl, |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$result = self::$http->post('channel/prepare', $options); |
66
|
|
|
|
67
|
2 |
|
return $this->channelAdapter->convertJson($result->getBody()->getContents()); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function show(string $publisherToken): Channel |
71
|
|
|
{ |
72
|
2 |
|
$result = self::$http->get("channel/{$publisherToken}"); |
73
|
|
|
|
74
|
2 |
|
if ($result->getStatusCode() === 404) { |
75
|
1 |
|
throw new ChannelNotFoundException($result->getBody()->getContents()); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $this->channelAdapter->convertJson($result->getBody()->getContents()); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function showAll(): array |
82
|
|
|
{ |
83
|
1 |
|
$result = self::$http->get('channel'); |
84
|
|
|
|
85
|
1 |
|
return $this->channelAdapter->convertJsonArray($result->getBody()->getContents()); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
public function publish($data, string $publisherToken): Response |
89
|
|
|
{ |
90
|
2 |
|
$result = self::$http->post("channel/publish/{$publisherToken}", [ |
91
|
2 |
|
\GuzzleHttp\RequestOptions::JSON => $data, |
92
|
|
|
]); |
93
|
|
|
|
94
|
2 |
|
if ($result->getStatusCode() === 404) { |
95
|
1 |
|
throw new ChannelNotFoundException($result->getBody()->getContents()); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $this->responseAdapter->convertJson($result->getBody()->getContents()); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
public function close(string $publisherToken): Response |
102
|
|
|
{ |
103
|
2 |
|
$result = self::$http->delete("channel/publish/{$publisherToken}"); |
104
|
|
|
|
105
|
2 |
|
if ($result->getStatusCode() === 404) { |
106
|
1 |
|
throw new ChannelNotFoundException($result->getBody()->getContents()); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $this->responseAdapter->convertJson($result->getBody()->getContents()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|