ObjectManager   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 86.54%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 33
c 1
b 1
f 0
dl 0
loc 178
ccs 45
cts 52
cp 0.8654
rs 10
wmc 18

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A getRepository() 0 3 1
A getMetadataFactory() 0 3 1
A persist() 0 3 1
A remove() 0 3 1
A getClassMetadata() 0 3 1
A initializeObject() 0 3 1
A getUnitOfWork() 0 3 1
A flush() 0 3 1
A merge() 0 3 1
A update() 0 3 1
A detach() 0 3 1
A find() 0 3 1
A clear() 0 3 1
A refresh() 0 3 1
A getOrCreateObject() 0 3 1
A contains() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper;
6
7
use BadMethodCallException;
8
use Doctrine\Common\EventManager;
9
use Doctrine\SkeletonMapper\Mapping\ClassMetadataFactory;
10
use Doctrine\SkeletonMapper\Mapping\ClassMetadataInterface;
11
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryFactoryInterface;
12
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryInterface;
13
use Doctrine\SkeletonMapper\Persister\ObjectPersisterFactoryInterface;
14
15
/**
16
 * Class for managing the persistence of objects.
17
 */
18
class ObjectManager implements ObjectManagerInterface
19
{
20
    /** @var ObjectRepositoryFactoryInterface */
21
    private $objectRepositoryFactory;
22
23
    /** @var ObjectPersisterFactoryInterface */
24
    private $objectPersisterFactory;
25
26
    /** @var ObjectIdentityMap */
27
    private $objectIdentityMap;
28
29
    /** @var UnitOfWork */
30
    private $unitOfWork;
31
32
    /** @var ClassMetadataFactory*/
33
    private $metadataFactory;
34
35
    /** @var EventManager */
36
    private $eventManager;
37
38 22
    public function __construct(
39
        ObjectRepositoryFactoryInterface $objectRepositoryFactory,
40
        ObjectPersisterFactoryInterface $objectPersisterFactory,
41
        ObjectIdentityMap $objectIdentityMap,
42
        ClassMetadataFactory $metadataFactory,
43
        ?EventManager $eventManager = null
44
    ) {
45 22
        $this->objectRepositoryFactory = $objectRepositoryFactory;
46 22
        $this->objectPersisterFactory  = $objectPersisterFactory;
47 22
        $this->objectIdentityMap       = $objectIdentityMap;
48 22
        $this->metadataFactory         = $metadataFactory;
49 22
        $this->eventManager            = $eventManager ?: new EventManager();
50
51 22
        $this->unitOfWork = new UnitOfWork(
52 22
            $this,
53 22
            $this->objectPersisterFactory,
54 22
            $this->objectIdentityMap,
55 22
            $this->eventManager
56
        );
57 22
    }
58
59 22
    public function getUnitOfWork() : UnitOfWork
60
    {
61 22
        return $this->unitOfWork;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 18
    public function find($className, $id)
68
    {
69 18
        return $this->getRepository($className)->find($id);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 10
    public function persist($object) : void
76
    {
77 10
        $this->unitOfWork->persist($object);
78 10
    }
79
80
    /**
81
     * Tells the ObjectManager to update the object on flush.
82
     *
83
     * The object will be updated in the database as a result of the flush operation.
84
     *
85
     * @param object $object The instance to update
86
     */
87
    public function update($object) : void
88
    {
89
        $this->unitOfWork->update($object);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 3
    public function remove($object) : void
96
    {
97 3
        $this->unitOfWork->remove($object);
98 3
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103 1
    public function merge($object) : void
104
    {
105 1
        $this->unitOfWork->merge($object);
106 1
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 9
    public function clear($objectName = null) : void
112
    {
113 9
        $this->unitOfWork->clear($objectName);
114 9
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 1
    public function detach($object) : void
120
    {
121 1
        $this->unitOfWork->detach($object);
122 1
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 1
    public function refresh($object) : void
128
    {
129 1
        $this->unitOfWork->refresh($object);
130 1
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 13
    public function flush() : void
136
    {
137 13
        $this->unitOfWork->commit();
138 13
    }
139
140
    /**
141
     * @param string $className
142
     *
143
     * @return ObjectRepositoryInterface
144
     */
145 20
    public function getRepository($className)
146
    {
147 20
        return $this->objectRepositoryFactory->getRepository($className);
148
    }
149
150
    /**
151
     * @param string $className
152
     */
153 22
    public function getClassMetadata($className) : ClassMetadataInterface
154
    {
155 22
        return $this->metadataFactory->getMetadataFor($className);
156
    }
157
158
    /**
159
     * Gets the metadata factory used to gather the metadata of classes.
160
     */
161
    public function getMetadataFactory() : ClassMetadataFactory
162
    {
163
        return $this->metadataFactory;
164
    }
165
166
    /**
167
     * Helper method to initialize a lazy loading proxy or persistent collection.
168
     *
169
     * This method is a no-op for other objects.
170
     *
171
     * @param object $obj
172
     */
173
    public function initializeObject($obj) : void
174
    {
175
        throw new BadMethodCallException('Not supported.');
176
    }
177
178
    /**
179
     * Checks if the object is part of the current UnitOfWork and therefore managed.
180
     *
181
     * @param object $object
182
     */
183 1
    public function contains($object) : bool
184
    {
185 1
        return $this->unitOfWork->contains($object);
186
    }
187
188
    /**
189
     * @param mixed[] $data
190
     *
191
     * @return object
192
     */
193 18
    public function getOrCreateObject(string $className, array $data)
194
    {
195 18
        return $this->unitOfWork->getOrCreateObject($className, $data);
196
    }
197
}
198