JsonFormatter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 9
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 10 1
A decode() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PascalDeVink\CloudEvents\Format;
6
7
use PascalDeVink\CloudEvents\V03\CloudEvent;
8
9
class JsonFormatter
10
{
11 6
    public function encode(CloudEvent $cloudEvent) : string
12
    {
13 6
        return json_encode(
14
            array_filter(
15 6
                $cloudEvent->toArray(),
16 2
                function ($value) {
17 6
                    return null !== $value;
18 6
                }
19
            ),
20 6
            JSON_THROW_ON_ERROR
21
        );
22
    }
23
24 3
    public function decode(string $json) : CloudEvent
25
    {
26 3
        $eventData = json_decode($json, true);
27
28 3
        return CloudEvent::fromArray($eventData);
29
    }
30
}
31