ChannelAdapter::stdClassToChannel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
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
/**
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