NodeValueFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createValue() 0 11 2
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Data\Value\EncodedJson;
6
7
use Remorhaz\JSON\Data\Path\PathInterface;
8
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactory as DecodedJsonNodeValueFactory;
9
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactoryInterface as DecodedJsonNodeValueFactoryInterface;
10
use Remorhaz\JSON\Data\Value\NodeValueInterface;
11
use Throwable;
12
13
use function json_decode;
14
15
use const JSON_THROW_ON_ERROR;
16
17
final class NodeValueFactory implements NodeValueFactoryInterface
18
{
19
20
    private $decodedJsonNodeValueFactory;
21
22
    public static function create(): NodeValueFactoryInterface
23
    {
24
        return new self(DecodedJsonNodeValueFactory::create());
25
    }
26
27
    public function __construct(DecodedJsonNodeValueFactoryInterface $decodedJsonNodeValueFactory)
28
    {
29
        $this->decodedJsonNodeValueFactory = $decodedJsonNodeValueFactory;
30
    }
31
32
    public function createValue(string $json, ?PathInterface $path = null): NodeValueInterface
33
    {
34
        try {
35
            $decodedData = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
36
        } catch (Throwable $e) {
37
            throw new Exception\JsonNotDecodedException($json, $e);
38
        }
39
40
        return $this
41
            ->decodedJsonNodeValueFactory
42
            ->createValue($decodedData, $path);
43
    }
44
}
45