Completed
Branch master (7f0c44)
by Artem
02:24
created

ChannelAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A stdClassToChannel() 0 7 1
A convertJsonArray() 0 13 3
A convertJson() 0 5 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
final class ChannelAdapter implements AdapterInterface
10
{
11
    /**
12
     * @param string $content
13
     * @return Channel
14
     */
15 4
    public function convertJson(string $content): Channel
16
    {
17 4
        $channel = json_decode($content);
18
19 4
        return $this->stdClassToChannel($channel);
20
    }
21
22
    /**
23
     * @param string $content
24
     * @return array|Channel[]
25
     */
26 2
    public function convertJsonArray(string $content): array
27
    {
28 2
        $response = json_decode($content);
29
30 2
        $array = [];
31
32 2
        if (!empty($response->channels)) {
33 2
            foreach ($response->channels as $channel) {
34 2
                $array[] = $this->stdClassToChannel($channel);
35
            }
36
        }
37
38 2
        return $array;
39
    }
40
41
    /**
42
     * @param stdClass $channel
43
     * @return Channel
44
     */
45 6
    private function stdClassToChannel(stdClass $channel): Channel
46
    {
47 6
        return new Channel(
48 6
            $channel->publisher_token,
49 6
            $channel->subscriber_token,
50 6
            $channel->subscriber_message_hook_url,
51 6
            $channel->listeners
52
        );
53
    }
54
}
55