Json::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Mapper\Kind;
6
7
use Mapper\Kind;
8
9
/**
10
 * Class Json
11
 * @package Mapper\Kind
12
 */
13
class Json extends Kind {
14
15
    /**
16
     * Json constructor.
17
     * @param string $data
18
     * @param object $object
19
     */
20 3
    public function __construct(string $data, $object) {
21 3
        $this->data = $data;
22
23 3
        parent::__construct($object);
24 3
    }
25
26
    /**
27
     * @return string
28
     */
29 3
    public function getData(): string {
30 3
        return $this->data;
31
    }
32
33
    /**
34
     * @param string $data
35
     * @return Json
36
     */
37 2
    public function setData(string $data): Json {
38 2
        $this->data = $data;
39
40 2
        $this->getKernel()
41 2
            ->setData($this->prepareDataKernel());
42
43 1
        return $this;
44
    }
45
46
    /**
47
     * @return array
48
     * @throws \Exception
49
     */
50 3
    protected function prepareDataKernel(): array {
51 3
        $data = json_decode($this->getData(), true);
52
53 3
        if (is_null($data)) {
54 1
            throw new \Exception('Invalid json passed');
55
        }
56
57 3
        return $data;
58
    }
59
60
}