MongoDbPoolingAwaker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5
ccs 10
cts 18
cp 0.5556
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getStorage() 0 4 1
A awake() 0 24 4
1
<?php
2
/**
3
 * File was created 07.10.2015 06:35
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Data\MongoDb;
7
8
use PeekAndPoke\Component\Slumber\Data\EntityPool;
9
use PeekAndPoke\Component\Slumber\Data\Storage;
10
11
/**
12
 * @author Karsten J. Gerber <[email protected]>
13
 */
14
class MongoDbPoolingAwaker implements MongoDbAwaker
15
{
16
    /** @var MongoDbAwaker */
17
    private $delegate;
18
    /** @var EntityPool */
19
    private $pool;
20
    /** @var MongoDbEntityConfigReader */
21
    private $lookUp;
22
23
    /**
24
     * @param MongoDbAwaker             $delegate
25
     * @param EntityPool                $pool
26
     * @param MongoDbEntityConfigReader $lookUp
27
     */
28
    public function __construct(MongoDbAwaker $delegate, EntityPool $pool, MongoDbEntityConfigReader $lookUp)
29
    {
30
        $this->delegate = $delegate;
31
        $this->pool     = $pool;
32
        $this->lookUp   = $lookUp;
33
    }
34
35
    /**
36
     * @return Storage
37
     */
38
    public function getStorage()
39
    {
40
        return $this->delegate->getStorage();
41
    }
42
43
    /**
44
     * @param mixed            $data
45
     * @param \ReflectionClass $cls
46
     *
47
     * @return mixed|null
48
     */
49 21
    public function awake($data, \ReflectionClass $cls)
50
    {
51 21
        $awoken = $this->delegate->awake($data, $cls);
52
53 21
        if ($awoken === null) {
54
            return null;
55
        }
56
57 21
        $awokenClass = new \ReflectionClass($awoken);
58
59
        // We need to get the idMarker from the awoken class
60 21
        $idAccess  = $this->lookUp->getEntityConfig($awokenClass)->getIdAccess();
61 21
        $primaryId = $idAccess->get($awoken);
62
63
        // Can we find this entity in the pool ?
64 21
        if ($primaryId !== null && $this->pool->has($cls, EntityPool::PRIMARY_ID, $primaryId)) {
65 4
            return $this->pool->get($cls, EntityPool::PRIMARY_ID, $primaryId);
66
        }
67
68
        // otherwise we set it on the pool
69 18
        $this->pool->set($cls, EntityPool::PRIMARY_ID, $primaryId, $awoken);
70
71 18
        return $awoken;
72
    }
73
}
74