Failed Conditions
Pull Request — master (#28)
by Jonathan
02:17
created

ObjectManager   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 86.27%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 166
ccs 44
cts 51
cp 0.8627
rs 10
c 0
b 0
f 0
wmc 18

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A persist() 0 3 1
A remove() 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 getRepository() 0 3 1
A getMetadataFactory() 0 3 1
A getClassMetadata() 0 3 1
A initializeObject() 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\Persistence\Mapping\ClassMetadata;
10
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
11
use Doctrine\Persistence\ObjectRepository;
12
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryFactoryInterface;
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
     * @param mixed $id
66
     */
67 18
    public function find(string $className, $id) : ?object
68
    {
69 18
        return $this->getRepository($className)->find($id);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 10
    public function persist(object $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 $object) : void
88
    {
89
        $this->unitOfWork->update($object);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 3
    public function remove(object $object) : void
96
    {
97 3
        $this->unitOfWork->remove($object);
98 3
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103 1
    public function merge(object $object) : object
104
    {
105 1
        return $this->unitOfWork->merge($object);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 9
    public function clear(?string $objectName = null) : void
112
    {
113 9
        $this->unitOfWork->clear($objectName);
114 9
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 1
    public function detach(object $object) : void
120
    {
121 1
        $this->unitOfWork->detach($object);
122 1
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 1
    public function refresh(object $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 20
    public function getRepository(string $className) : ObjectRepository
141
    {
142 20
        return $this->objectRepositoryFactory->getRepository($className);
143
    }
144
145 22
    public function getClassMetadata(string $className) : ClassMetadata
146
    {
147 22
        return $this->metadataFactory->getMetadataFor($className);
148
    }
149
150
    /**
151
     * Gets the metadata factory used to gather the metadata of classes.
152
     */
153
    public function getMetadataFactory() : ClassMetadataFactory
154
    {
155
        return $this->metadataFactory;
156
    }
157
158
    /**
159
     * Helper method to initialize a lazy loading proxy or persistent collection.
160
     *
161
     * This method is a no-op for other objects.
162
     */
163
    public function initializeObject(object $object) : void
164
    {
165
        throw new BadMethodCallException('Not supported.');
166
    }
167
168
    /**
169
     * Checks if the object is part of the current UnitOfWork and therefore managed.
170
     */
171 1
    public function contains(object $object) : bool
172
    {
173 1
        return $this->unitOfWork->contains($object);
174
    }
175
176
    /**
177
     * @param array<string, mixed> $data
178
     *
179
     * @return object
180
     */
181 18
    public function getOrCreateObject(string $className, array $data)
182
    {
183 18
        return $this->unitOfWork->getOrCreateObject($className, $data);
184
    }
185
}
186