Completed
Push — master ( a1b189...a1cb6a )
by Kristof
04:46
created

JsonDocument   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 48
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getBody() 0 4 1
A getRawBody() 0 4 1
A withBody() 0 4 1
A __construct() 0 5 1
A apply() 0 5 1
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\ReadModel;
7
8
use Broadway\ReadModel\ReadModelInterface;
9
10
class JsonDocument implements ReadModelInterface
11
{
12
    protected $id;
13
    protected $body;
14
15
    public function __construct($id, $rawBody = '{}')
16
    {
17
        $this->id = $id;
18
        $this->body = $rawBody;
19
    }
20
21
    /**
22
     * @return string
23
     */
24
    public function getId()
25
    {
26
        return $this->id;
27
    }
28
29
    public function getBody()
30
    {
31
        return json_decode($this->body);
32
    }
33
34
    public function getRawBody()
35
    {
36
        return $this->body;
37
    }
38
39
    /**
40
     * @param \stdClass $body
41
     * @return static
42
     */
43
    public function withBody($body)
44
    {
45
        return new self($this->id, json_encode($body));
46
    }
47
48
    /**
49
     * @param callable $fn
50
     * @return
51
     */
52
    public function apply(callable $fn)
53
    {
54
        $body = $fn($this->getBody());
55
        return $this->withBody($body);
56
    }
57
}
58