ObjectMapper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 13
dl 0
loc 65
c 0
b 0
f 0
rs 10
ccs 16
cts 16
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 3 1
A __construct() 0 3 1
A awake() 0 9 4
A slumber() 0 19 3
1
<?php
2
/**
3
 * File was created 30.09.2015 07:46
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property;
7
8
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsObject;
9
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
10
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
11
use PeekAndPoke\Types\ValueHolder;
12
13
/**
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class ObjectMapper extends AbstractPropertyMapper
17
{
18
    /** @var AsObject */
19
    private $options;
20
21
    /**
22
     * C'tor.
23
     *
24
     * @param AsObject $options
25
     */
26 24
    public function __construct(AsObject $options)
27
    {
28 24
        $this->options = $options;
29 24
    }
30
31
    /**
32
     * @return AsObject
33
     */
34 4
    public function getOptions()
35
    {
36 4
        return $this->options;
37
    }
38
39
    /**
40
     * @param Slumberer $slumberer
41
     * @param mixed     $value
42
     *
43
     * @return mixed
44
     */
45 15
    public function slumber(Slumberer $slumberer, $value)
46
    {
47
        /**
48
         * unwrap any wrappers like LazyDbReference
49
         * @see LazyDbReference
50
         */
51 15
        if ($value instanceof ValueHolder) {
52
53
            // TODO: Rethink this! Is this really a good idea or should it only happen in the Mongo ObjectMapper and only for LazyDbReference ?
54
            //       This might trigger in cases where it is not really wanted.
55
56 1
            $value = $value->getValue();
57
        }
58
59 15
        if ($value instanceof $this->options->value) {
60 3
            return $slumberer->slumber($value);
61
        }
62
63 12
        return null;
64
    }
65
66
    /**
67
     * @param Awaker $awaker
68
     * @param mixed  $value
69
     *
70
     * @return mixed
71
     */
72 5
    public function awake(Awaker $awaker, $value)
73
    {
74 5
        if ($value === null || (! \is_array($value) && ! $value instanceof \ArrayAccess)) {
75 4
            return null;
76
        }
77
78 1
        $cls = new \ReflectionClass($this->options->value);
79
80 1
        return $awaker->awake($value, $cls);
81
    }
82
}
83