ObjectIdentityMap   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 107
ccs 38
cts 40
cp 0.95
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 6 2
A count() 0 3 1
A addToIdentityMap() 0 11 2
A __construct() 0 3 1
A tryGetById() 0 9 2
A extractIdentifierFromData() 0 5 1
A getObjectIdentifier() 0 5 1
A contains() 0 9 1
A detach() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper;
6
7
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryFactoryInterface;
8
use function count;
9
use function get_class;
10
use function serialize;
11
12
/**
13
 * Class for maintaining an object identity map.
14
 */
15
class ObjectIdentityMap
16
{
17
    /** @var object[][] */
18
    private $identityMap = [];
19
20
    /** @var ObjectRepositoryFactoryInterface */
21
    private $objectRepositoryFactory;
22
23 22
    public function __construct(ObjectRepositoryFactoryInterface $objectRepositoryFactory)
24
    {
25 22
        $this->objectRepositoryFactory = $objectRepositoryFactory;
26 22
    }
27
28
    /**
29
     * @param object $object
30
     */
31 11
    public function contains($object) : bool
32
    {
33 11
        $className = get_class($object);
34
35 11
        $objectIdentifier = $this->getObjectIdentifier($object);
36
37 11
        $serialized = serialize($objectIdentifier);
38
39 11
        return isset($this->identityMap[$className][$serialized]);
40
    }
41
42
    /**
43
     * @param mixed[] $data
44
     *
45
     * @return object|null
46
     */
47 18
    public function tryGetById(string $className, array $data)
48
    {
49 18
        $serialized = serialize($this->extractIdentifierFromData($className, $data));
50
51 18
        if (isset($this->identityMap[$className][$serialized])) {
52 8
            return $this->identityMap[$className][$serialized];
53
        }
54
55 17
        return null;
56
    }
57
58
    /**
59
     * @param object  $object
60
     * @param mixed[] $data
61
     */
62 20
    public function addToIdentityMap($object, array $data) : void
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function addToIdentityMap($object, /** @scrutinizer ignore-unused */ array $data) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64 20
        $className = get_class($object);
65
66 20
        if (! isset($this->identityMap[$className])) {
67 20
            $this->identityMap[get_class($object)] = [];
68
        }
69
70 20
        $serialized = serialize($this->getObjectIdentifier($object));
71
72 20
        $this->identityMap[get_class($object)][$serialized] = $object;
73 20
    }
74
75 9
    public function clear(?string $objectName = null) : void
76
    {
77 9
        if ($objectName !== null) {
78 1
            unset($this->identityMap[$objectName]);
79
        } else {
80 9
            $this->identityMap = [];
81
        }
82 9
    }
83
84
    /**
85
     * @param object $object
86
     */
87 4
    public function detach($object) : void
88
    {
89 4
        $objectIdentifier = $this->getObjectIdentifier($object);
90
91 4
        $serialized = serialize($objectIdentifier);
92 4
        unset($this->identityMap[get_class($object)][$serialized]);
93 4
    }
94
95
    public function count() : int
96
    {
97
        return count($this->identityMap);
98
    }
99
100
    /**
101
     * @param object $object
102
     *
103
     * @return mixed[] $identifier
104
     */
105 20
    private function getObjectIdentifier($object) : array
106
    {
107 20
        return $this->objectRepositoryFactory
108 20
            ->getRepository(get_class($object))
109 20
            ->getObjectIdentifier($object);
110
    }
111
112
    /**
113
     * @param mixed[] $data
114
     *
115
     * @return mixed[] $identifier
116
     */
117 18
    private function extractIdentifierFromData(string $className, array $data) : array
118
    {
119 18
        return $this->objectRepositoryFactory
120 18
            ->getRepository($className)
121 18
            ->getObjectIdentifierFromData($data);
122
    }
123
}
124