Completed
Push — master ( 1be44d...449b6f )
by Artem
02:21
created

Client   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 109
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A showAll() 0 5 1
A edit() 0 12 1
A show() 0 9 2
A prepare() 0 23 4
A publish() 0 11 2
A close() 0 9 2
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\ChannelOptions;
13
use Gockets\Model\Params;
14
use Gockets\Model\Response;
15
16
/**
17
 * Gockets Client
18
 *
19
 * Implements available Gockets methods.
20
 *
21
 * @package Gockets
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
    public function prepare(?ChannelOptions $channelOptions = null): Channel
54
    {
55
        if (is_null($channelOptions)) {
56
            $options = [];
57
        } else {
58
            $body = [];
59
60
            if (!empty($channelOptions->getHookUrl())) {
61
                $body['subscriber_message_hook_url'] = $channelOptions->getHookUrl();
62
            }
63
64
            if (!empty($channelOptions->getTag())) {
65
                $body['tag'] = $channelOptions->getTag();
66
            }
67
68
            $options = [
69
                \GuzzleHttp\RequestOptions::JSON => $body,
70
            ];
71
        }
72
73
        $result = self::$http->post('channel/prepare', $options);
74
75
        return $this->channelAdapter->convertJson($result->getBody()->getContents());
76
    }
77
78
    public function show(string $publisherToken): Channel
79
    {
80
        $result = self::$http->get("channel/show/{$publisherToken}");
81
82
        if ($result->getStatusCode() === 404) {
83
            throw new ChannelNotFoundException($result->getBody()->getContents());
84
        }
85
86
        return $this->channelAdapter->convertJson($result->getBody()->getContents());
87
    }
88
89
    public function showAll(): array
90
    {
91
        $result = self::$http->get('channel/show');
92
93
        return $this->channelAdapter->convertJsonArray($result->getBody()->getContents());
94
    }
95
96
    public function edit(string $publisherToken, ChannelOptions $channelOptions): Channel
97
    {
98
        $options = [
99
            \GuzzleHttp\RequestOptions::JSON => [
100
                'subscriber_message_hook_url' => $channelOptions->getHookUrl(),
101
                'tag' => $channelOptions->getTag(),
102
            ],
103
        ];
104
105
        $result = self::$http->patch("channel/edit/{$publisherToken}", $options);
106
107
        return $this->channelAdapter->convertJson($result->getBody()->getContents());
108
    }
109
110
    public function publish($data, string $publisherToken): Response
111
    {
112
        $result = self::$http->post("channel/publish/{$publisherToken}", [
113
            \GuzzleHttp\RequestOptions::JSON => $data,
114
        ]);
115
116
        if ($result->getStatusCode() === 404) {
117
            throw new ChannelNotFoundException($result->getBody()->getContents());
118
        }
119
120
        return $this->responseAdapter->convertJson($result->getBody()->getContents());
121
    }
122
123
    public function close(string $publisherToken): Response
124
    {
125
        $result = self::$http->delete("channel/publish/{$publisherToken}");
126
127
        if ($result->getStatusCode() === 404) {
128
            throw new ChannelNotFoundException($result->getBody()->getContents());
129
        }
130
131
        return $this->responseAdapter->convertJson($result->getBody()->getContents());
132
    }
133
}
134