DocumentManager   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 339
Duplicated Lines 9.73 %

Coupling/Cohesion

Components 2
Dependencies 13

Importance

Changes 0
Metric Value
wmc 40
lcom 2
cbo 13
dl 33
loc 339
rs 9.2
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getEventManager() 0 4 1
A getCouchDBClient() 0 4 1
A create() 0 12 3
A getMetadataFactory() 0 4 1
A getProxyFactory() 0 4 1
A getHttpClient() 0 4 1
A getDatabase() 0 4 1
A getConfiguration() 0 4 1
A getClassMetadataFactory() 0 4 1
A getClassMetadata() 0 4 1
A find() 0 4 1
A getRepository() 0 14 3
A createQuery() 10 10 2
A createNativeQuery() 9 9 2
A createLuceneQuery() 14 14 2
A persist() 0 4 1
A remove() 0 4 1
A refresh() 0 4 1
A merge() 0 4 1
A detach() 0 4 1
A getReference() 0 13 2
A flush() 0 4 1
A contains() 0 4 1
A getUnitOfWork() 0 4 1
A clear() 0 10 2
A initializeObject() 0 8 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DocumentManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DocumentManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Doctrine\ODM\CouchDB;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\CouchDB\CouchDBClient;
8
use Doctrine\CouchDB\View;
9
use Doctrine\CouchDB\View\Query;
10
use Doctrine\ODM\CouchDB\Mapping\ClassMetadataFactory;
11
use Doctrine\ODM\CouchDB\View\ODMQuery;
12
use Doctrine\ODM\CouchDB\View\ODMLuceneQuery;
13
use Doctrine\ODM\CouchDB\CouchDBException;
14
15
class DocumentManager implements ObjectManager
16
{
17
    /**
18
     * @var Configuration
19
     */
20
    private $config;
21
22
    /**
23
     * @var Mapping\ClassMetadataFactory
24
     */
25
    private $metadataFactory;
26
27
    /**
28
     * @var UnitOfWork
29
     */
30
    private $unitOfWork = null;
31
32
    /**
33
     * @var \Doctrine\ODM\CouchDB\Proxy\ProxyFactory
34
     */
35
    private $proxyFactory = null;
36
37
    /**
38
     * @var array
39
     */
40
    private $repositories = array();
41
42
    /**
43
     * @var CouchDBClient
44
     */
45
    private $couchDBClient = null;
46
47
    /**
48
     * @var EventManager
49
     */
50
    private $evm;
51
52
    /**
53
     * @param CouchDBClient $couchClient
54
     * @param Configuration $config
55
     * @param EventManager $evm
56
     */
57
    public function __construct(CouchDBClient $couchClient, Configuration $config = null, EventManager $evm = null)
58
    {
59
        $this->couchDBClient = $couchClient;
60
        $this->config = $config ?: new Configuration();
61
        $this->evm = $evm ?: new EventManager();
62
        $this->metadataFactory = new ClassMetadataFactory($this);
63
        $this->unitOfWork = new UnitOfWork($this);
64
        $this->proxyFactory = new Proxy\ProxyFactory($this, $this->config->getProxyDir(), $this->config->getProxyNamespace(), $this->config->getAutoGenerateProxyClasses());
65
    }
66
67
    /**
68
     * @return EventManager
69
     */
70
    public function getEventManager()
71
    {
72
        return $this->evm;
73
    }
74
75
    /**
76
     * @return CouchDBClient
77
     */
78
    public function getCouchDBClient()
79
    {
80
        return $this->couchDBClient;
81
    }
82
83
    /**
84
     * Factory method for a Document Manager.
85
     *
86
     * @param array|CouchDBClient $couchParams
87
     * @param Configuration       $config
88
     * @param EventManager        $evm
89
     * @return DocumentManager
90
     * @throws \InvalidArgumentException
91
     */
92
    public static function create($couchParams, Configuration $config = null, EventManager $evm = null)
93
    {
94
        if (is_array($couchParams)) {
95
            $couchClient = CouchDBClient::create($couchParams);
96
        } else if ($couchParams instanceof CouchDBClient) {
97
            $couchClient = $couchParams;
98
        } else {
99
            throw new \InvalidArgumentException("Expecting array of instance of CouchDBClient as first argument to DocumentManager::create().");
100
        }
101
102
        return new DocumentManager($couchClient, $config, $evm);
103
    }
104
105
    /**
106
     * @return ClassMetadataFactory
107
     */
108
    public function getMetadataFactory()
109
    {
110
        return $this->metadataFactory;
111
    }
112
113
    /**
114
     * @return Proxy\ProxyFactory
115
     */
116
    public function getProxyFactory()
117
    {
118
        return $this->proxyFactory;
119
    }
120
121
    public function getHttpClient()
122
    {
123
        return $this->couchDBClient->getHttpClient();
124
    }
125
126
    public function getDatabase()
127
    {
128
        return $this->couchDBClient->getDatabase();
129
    }
130
131
    /**
132
     * @return Configuration
133
     */
134
    public function getConfiguration()
135
    {
136
        return $this->config;
137
    }
138
139
    /**
140
     * @return ClassMetadataFactory
141
     */
142
    public function getClassMetadataFactory()
143
    {
144
        return $this->metadataFactory;
145
    }
146
147
    /**
148
     * @param  string $class
149
     * @return \Doctrine\ODM\CouchDB\Mapping\ClassMetadata
150
     */
151
    public function getClassMetadata($class)
152
    {
153
        return $this->metadataFactory->getMetadataFor($class);
154
    }
155
156
    /**
157
     * Find the Document with the given id.
158
     *
159
     * Will return null if the document wasn't found.
160
     *
161
     * @param string $documentName
162
     * @param string $id
163
     * @return object
164
     */
165
    public function find($documentName, $id)
166
    {
167
        return $this->getRepository($documentName)->find($id);
168
    }
169
170
    /**
171
     * @param  string $documentName
172
     * @return \Doctrine\ODM\CouchDB\DocumentRepository
173
     */
174
    public function getRepository($documentName)
175
    {
176
        $documentName  = ltrim($documentName, '\\');
177
        if (!isset($this->repositories[$documentName])) {
178
            $class = $this->getClassMetadata($documentName);
179
            if ($class->customRepositoryClassName) {
180
                $repositoryClass = $class->customRepositoryClassName;
181
            } else {
182
                $repositoryClass = 'Doctrine\ODM\CouchDB\DocumentRepository';
183
            }
184
            $this->repositories[$documentName] = new $repositoryClass($this, $class);
185
        }
186
        return $this->repositories[$documentName];
187
    }
188
189
    /**
190
     * Create a Query for the view in the specified design document.
191
     *
192
     * @param  string $designDocName
193
     * @param  string $viewName
194
     * @return \Doctrine\ODM\CouchDB\View\ODMQuery
195
     */
196 View Code Duplication
    public function createQuery($designDocName, $viewName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198
        $designDoc = $this->config->getDesignDocument($designDocName);
199
        if ($designDoc) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $designDoc of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
200
            $designDoc = new $designDoc['className']($designDoc['options']);
201
        }
202
        $query = new ODMQuery($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $designDocName, $viewName, $designDoc);
0 ignored issues
show
Documentation introduced by
$designDoc is of type object|array, but the function expects a null|object<Doctrine\CouchDB\View\DesignDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203
        $query->setDocumentManager($this);
204
        return $query;
205
    }
206
207
    /**
208
     * Create a Native query for the view of the specified design document.
209
     *
210
     * A native query will return an array of data from the &include_docs=true parameter.
211
     *
212
     * @param  string $designDocName
213
     * @param  string $viewName
214
     * @return \Doctrine\CouchDB\View\Query
215
     */
216 View Code Duplication
    public function createNativeQuery($designDocName, $viewName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
        $designDoc = $this->config->getDesignDocument($designDocName);
219
        if ($designDoc) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $designDoc of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
220
            $designDoc = new $designDoc['className']($designDoc['options']);
221
        }
222
        $query = new Query($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $designDocName, $viewName, $designDoc);
0 ignored issues
show
Documentation introduced by
$designDoc is of type object|array, but the function expects a null|object<Doctrine\CouchDB\View\DesignDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
223
        return $query;
224
    }
225
226
    /**
227
     * Create a CouchDB-Lucene Query.
228
     *
229
     * @param string $designDocName
230
     * @param string $viewName
231
     * @return \Doctrine\ODM\CouchDB\View\ODMLuceneQuery
232
     */
233 View Code Duplication
    public function createLuceneQuery($designDocName, $viewName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        $luceneHandlerName = $this->config->getLuceneHandlerName();
236
        $designDoc = $this->config->getDesignDocument($designDocName);
237
        if ($designDoc) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $designDoc of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
238
            $designDoc = new $designDoc['className']($designDoc['options']);
239
        }
240
        $query = new ODMLuceneQuery($this->couchDBClient->getHttpClient(),
241
            $this->couchDBClient->getDatabase(), $luceneHandlerName, $designDocName,
242
            $viewName, $designDoc
0 ignored issues
show
Documentation introduced by
$designDoc is of type object|array, but the function expects a null|object<Doctrine\CouchDB\View\DesignDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
243
        );
244
        $query->setDocumentManager($this);
245
        return $query;
246
    }
247
248
    public function persist($object)
249
    {
250
        $this->unitOfWork->scheduleInsert($object);
251
    }
252
253
    public function remove($object)
254
    {
255
        $this->unitOfWork->scheduleRemove($object);
256
    }
257
258
    /**
259
     * Refresh the given document by querying the CouchDB to get the current state.
260
     *
261
     * @param object $document
262
     */
263
    public function refresh($document)
264
    {
265
        $this->getUnitOfWork()->refresh($document);
266
    }
267
268
    public function merge($document)
269
    {
270
        return $this->getUnitOfWork()->merge($document);
271
    }
272
273
    public function detach($document)
274
    {
275
        $this->getUnitOfWork()->detach($document);
276
    }
277
278
    /**
279
     * Gets a reference to the entity identified by the given type and identifier
280
     * without actually loading it, if the entity is not yet loaded.
281
     *
282
     * @param string $documentName The name of the entity type.
283
     * @param mixed $identifier The entity identifier.
284
     * @return object The entity reference.
285
     */
286
    public function getReference($documentName, $identifier)
287
    {
288
        $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
289
290
        // Check identity map first, if its already in there just return it.
291
        if ($document = $this->unitOfWork->tryGetById($identifier)) {
292
            return $document;
293
        }
294
        $document = $this->proxyFactory->getProxy($class->name, $identifier);
295
        $this->unitOfWork->registerManaged($document, $identifier, null);
296
297
        return $document;
298
    }
299
300
    public function flush()
301
    {
302
        $this->unitOfWork->flush(); // todo: rename commit
303
    }
304
305
    /**
306
     * @param  object $document
307
     * @return bool
308
     */
309
    public function contains($document)
310
    {
311
        return $this->unitOfWork->contains($document);
312
    }
313
314
    /**
315
     * @return UnitOfWork
316
     */
317
    public function getUnitOfWork()
318
    {
319
        return $this->unitOfWork;
320
    }
321
322
    /**
323
     * Clears the ObjectManager. All objects that are currently managed
324
     * by this ObjectManager become detached.
325
     *
326
     * @param string $objectName if given, only objects of this type will get detached
327
     * @throws CouchDBException
328
     */
329
    public function clear($objectName = null)
330
    {
331
        if ($objectName === null) {
332
            // Todo: Do a real delegated clear?
333
            $this->unitOfWork = new UnitOfWork($this);
334
        } else {
335
            //TODO
336
            throw new CouchDBException("DocumentManager#clear(\$objectName) not yet implemented.");
337
        }
338
    }
339
340
    /**
341
     * Initialize an object that is a lazy load proxy, or do nothing.
342
     *
343
     * @param object $obj
344
     */
345
    public function initializeObject($obj)
346
    {
347
        if ($obj instanceof PersistentCollection) {
348
            $obj->initialize();
349
        } else if ($obj instanceof Proxy\Proxy) {
350
            $obj->__doctrineLoad__();
0 ignored issues
show
Bug introduced by
The method __doctrineLoad__() does not seem to exist on object<Doctrine\ODM\CouchDB\Proxy\Proxy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
351
        }
352
    }
353
}
354