JsonScopeTokenValueSerializer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 43
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D getSerializedValue() 0 32 10
1
<?php
2
3
namespace ConfigToken\TokenResolver\ScopeValueSerializers;
4
5
use ConfigToken\TokenResolver\Exception\ScopeTokenValueSerializationException;
6
use ConfigToken\TokenResolver\ScopeTokenValueSerializerInterface;
7
8
9
class JsonScopeTokenValueSerializer implements ScopeTokenValueSerializerInterface
10
{
11
    /**
12
     * Serialize the given value from the scope to a string representation.
13
     *
14
     * @param array|string $scopeValue
15
     * @param boolean $escape
16
     * @return string
17
     * @throws ScopeTokenValueSerializationException
18
     */
19 2
    public function getSerializedValue($scopeValue, $escape = False)
20
    {
21 2
        $valueType = gettype($scopeValue);
22
23
        switch ($valueType) {
24 2
            case 'string':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
25 1
                if ($escape) {
26
                    $result = json_encode($scopeValue);
27
                    return substr($result, 1, -1); // eliminate quotes
28
                } else {
29 1
                    return '' . $scopeValue;
30
                }
31 2
            case 'boolean':
32 1
                return ($scopeValue ? 'true' : 'false');
33 2
            case 'integer':
34 1
                return '' . $scopeValue;
35 2
            case 'double':
36 1
                return sprintf('%.5f', $scopeValue);
37 2
            case 'array':
38 2
            case 'object':
39 1
                return json_encode($scopeValue);
40 2
            case 'NULL':
41 1
                return 'null';
42 1
            default:
43 1
                throw new ScopeTokenValueSerializationException(
44 1
                    sprintf(
45 1
                        'Unable to serialize value of type "%s".',
46
                        $valueType
47 1
                    )
48 1
                );
49 1
        }
50
    }
51
}