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

ChannelAdapter::convertJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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