Client::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 2
    public function prepare(?ChannelOptions $channelOptions = null): Channel
54
    {
55 2
        if (is_null($channelOptions)) {
56 1
            $options = [];
57
        } else {
58 1
            $body = [];
59
60 1
            if (!empty($channelOptions->getHookUrl())) {
61 1
                $body['subscriber_message_hook_url'] = $channelOptions->getHookUrl();
62
            }
63
64 1
            if (!empty($channelOptions->getTag())) {
65 1
                $body['tag'] = $channelOptions->getTag();
66
            }
67
68
            $options = [
69 1
                \GuzzleHttp\RequestOptions::JSON => $body,
70
            ];
71
        }
72
73 2
        $result = self::$http->post('channel/prepare', $options);
74
75 2
        return $this->channelAdapter->convertJson($result->getBody()->getContents());
76
    }
77
78 2
    public function show(string $publisherToken): Channel
79
    {
80 2
        $result = self::$http->get("channel/show/{$publisherToken}");
81
82 2
        if ($result->getStatusCode() === 404) {
83 1
            throw new ChannelNotFoundException($result->getBody()->getContents());
84
        }
85
86 1
        return $this->channelAdapter->convertJson($result->getBody()->getContents());
87
    }
88
89 1
    public function showAll(): array
90
    {
91 1
        $result = self::$http->get('channel/show');
92
93 1
        return $this->channelAdapter->convertJsonArray($result->getBody()->getContents());
94
    }
95
96 1
    public function edit(string $publisherToken, ChannelOptions $channelOptions): Channel
97
    {
98
        $options = [
99 1
            \GuzzleHttp\RequestOptions::JSON => [
100 1
                'subscriber_message_hook_url' => $channelOptions->getHookUrl(),
101 1
                'tag' => $channelOptions->getTag(),
102
            ],
103
        ];
104
105 1
        $result = self::$http->patch("channel/edit/{$publisherToken}", $options);
106
107 1
        return $this->channelAdapter->convertJson($result->getBody()->getContents());
108
    }
109
110 2
    public function publish($data, string $publisherToken): Response
111
    {
112 2
        $result = self::$http->post("channel/publish/{$publisherToken}", [
113 2
            \GuzzleHttp\RequestOptions::JSON => $data,
114
        ]);
115
116 2
        if ($result->getStatusCode() === 404) {
117 1
            throw new ChannelNotFoundException($result->getBody()->getContents());
118
        }
119
120 1
        return $this->responseAdapter->convertJson($result->getBody()->getContents());
121
    }
122
123 2
    public function close(string $publisherToken): Response
124
    {
125 2
        $result = self::$http->delete("channel/publish/{$publisherToken}");
126
127 2
        if ($result->getStatusCode() === 404) {
128 1
            throw new ChannelNotFoundException($result->getBody()->getContents());
129
        }
130
131 1
        return $this->responseAdapter->convertJson($result->getBody()->getContents());
132
    }
133
}
134