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

ChannelAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 44
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertJsonArray() 0 13 3
A convertJson() 0 5 1
A stdClassToChannel() 0 8 1
1
<?php declare(strict_types=1);
2
3
namespace Gockets\Adapter;
4
5
use Gockets\Contract\AdapterInterface;
6
use Gockets\Model\Channel;
7
use stdClass;
8
9
/**
10
 * Channel Adapter
11
 *
12
 * Converts raw json into Channel object.
13
 */
14
final class ChannelAdapter implements AdapterInterface
15
{
16
    /**
17
     * @param string $content
18
     * @return Channel
19
     */
20
    public function convertJson(string $content): Channel
21
    {
22
        $channel = json_decode($content);
23
24
        return $this->stdClassToChannel($channel);
25
    }
26
27
    /**
28
     * @param string $content
29
     * @return array|Channel[]
30
     */
31
    public function convertJsonArray(string $content): array
32
    {
33
        $response = json_decode($content);
34
35
        $array = [];
36
37
        if (!empty($response->channels)) {
38
            foreach ($response->channels as $channel) {
39
                $array[] = $this->stdClassToChannel($channel);
40
            }
41
        }
42
43
        return $array;
44
    }
45
46
    /**
47
     * @param stdClass $channel
48
     * @return Channel
49
     */
50
    private function stdClassToChannel(stdClass $channel): Channel
51
    {
52
        return new Channel(
53
            $channel->publisher_token,
54
            $channel->subscriber_token,
55
            $channel->subscriber_message_hook_url,
56
            $channel->tag,
57
            $channel->listeners
58
        );
59
    }
60
}
61