Passed
Push — master ( a290cb...57bf28 )
by Dawid
02:40
created

Embed::extract()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 3
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Mapping\Strategy;
4
5
use Igni\Storage\Manager;
6
use Igni\Storage\Exception\HydratorException;
7
use Igni\Storage\Mapping\MappingStrategy;
8
9
final class Embed implements MappingStrategy, DefaultAttributesProvider
10
{
11 4
    public static function hydrate(&$value, array $attributes = [], Manager $manager = null): void
12
    {
13 4
        if (!empty($value)) {
14 3
            $value = self::deserializeValue($value, $attributes['storeAs']);
15 3
            if (!empty($value)) {
16 3
                $value = $manager->hydrate($attributes['class'], $value);
0 ignored issues
show
Bug introduced by
The method hydrate() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
                /** @scrutinizer ignore-call */ 
17
                $value = $manager->hydrate($attributes['class'], $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
17
            } else {
18 3
                $value = null;
19
            }
20
        } else {
21 1
            $value = null;
22
        }
23 4
    }
24
25 3
    public static function extract(&$value, array $attributes = [], Manager $manager = null): void
26
    {
27
28 3
        if ($value instanceof $attributes['class']) {
29 2
            $value = $manager->extract($value);
30 2
            $value = self::serializeValue($value, $attributes['storeAs']);
31
        } else {
32 1
            $value = null;
33
        }
34 3
    }
35
36 5
    public static function getDefaultAttributes(): array
37
    {
38
        return [
39 5
            'storeAs' => 'json',
40
        ];
41
    }
42
43 3
    private static function deserializeValue($value, string $strategy)
44
    {
45
        switch ($strategy) {
46 3
            case 'json':
47 1
                $value = json_decode($value, true);
48 1
                break;
49 2
            case 'serialized':
50
                $value = unserialize($value);
51
                break;
52 2
            case 'plain':
53 2
                break;
54
            default:
55
                throw new HydratorException("Cannot persist embed entity, invalid storeAs attribute (${strategy})");
56
        }
57
58
        return $value;
59
    }
60
61
    private static function serializeValue($value, string $strategy)
62
    {
63
        switch ($strategy) {
64 2
            case 'json':
65 1
                $value = json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
66 1
                break;
67 1
            case 'serialized':
68
                $value = serialize($value);
69
                break;
70 1
            case 'plain':
71 1
                break;
72
            default:
73
                throw new HydratorException("Cannot hydrate embed entity, invalid storeAs attribute (${strategy})");
74
        }
75
76 2
        return $value;
77
    }
78
}
79