Completed
Push — master ( 449b6f...296164 )
by Artem
02:13
created

ChannelAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 44
ccs 17
cts 17
cp 1
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 5
    public function convertJson(string $content): Channel
21
    {
22 5
        $channel = json_decode($content);
23
24 5
        return $this->stdClassToChannel($channel);
25
    }
26
27
    /**
28
     * @param string $content
29
     * @return array|Channel[]
30
     */
31 2
    public function convertJsonArray(string $content): array
32
    {
33 2
        $response = json_decode($content);
34
35 2
        $array = [];
36
37 2
        if (!empty($response->channels)) {
38 2
            foreach ($response->channels as $channel) {
39 2
                $array[] = $this->stdClassToChannel($channel);
40
            }
41
        }
42
43 2
        return $array;
44
    }
45
46
    /**
47
     * @param stdClass $channel
48
     * @return Channel
49
     */
50 7
    private function stdClassToChannel(stdClass $channel): Channel
51
    {
52 7
        return new Channel(
53 7
            $channel->publisher_token,
54 7
            $channel->subscriber_token,
55 7
            $channel->subscriber_message_hook_url,
56 7
            $channel->tag,
57 7
            $channel->listeners
58
        );
59
    }
60
}
61