Completed
Push — master ( 7f0c44...602b55 )
by Artem
02:09
created

Client   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 87
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

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