Completed
Push — master ( ecb96f...9f19a4 )
by Andreas
21s queued 13s
created

AbstractRepositoryFactory::createRepository()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 39

Duplication

Lines 9
Ratio 23.08 %

Code Coverage

Tests 6
CRAP Score 10

Importance

Changes 0
Metric Value
dl 9
loc 39
ccs 6
cts 6
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 36
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Repository;
6
7
use Doctrine\ODM\MongoDB\DocumentManager;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
9
use Doctrine\ODM\MongoDB\Mapping\MappingException;
10
use Doctrine\ODM\MongoDB\MongoDBException;
11
use Doctrine\Persistence\ObjectRepository;
12
use function is_a;
13
use function ltrim;
14
use function spl_object_hash;
15
16
/**
17
 * Abstract factory for creating document repositories.
18
 */
19
abstract class AbstractRepositoryFactory implements RepositoryFactory
20
{
21
    /**
22
     * The list of DocumentRepository instances.
23
     *
24
     * @var ObjectRepository[]
25
     */
26
    private $repositoryList = [];
27
28 356
    /**
29
     * {@inheritdoc}
30 356
     */
31 356
    public function getRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
32
    {
33 356
        $metadata = $documentManager->getClassMetadata($documentName);
34 168
        $hashKey  = $metadata->getName() . spl_object_hash($documentManager);
0 ignored issues
show
Bug introduced by
Consider using $metadata->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
35
36
        if (isset($this->repositoryList[$hashKey])) {
37 356
            return $this->repositoryList[$hashKey];
38
        }
39 356
40
        $repository = $this->createRepository($documentManager, ltrim($documentName, '\\'));
41 356
42
        $this->repositoryList[$hashKey] = $repository;
43
44
        return $repository;
45
    }
46
47
    /**
48
     * Create a new repository instance for a document class.
49 356
     *
50
     * @return ObjectRepository|GridFSRepository|ViewRepository
51 356
     */
52
    protected function createRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
53 356
    {
54 13
        $metadata = $documentManager->getClassMetadata($documentName);
55 348
56 12
        $repositoryClassName = $metadata->isFile
57
            ? $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName()
58 337
            : $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName();
59
60
        if ($metadata->customRepositoryClassName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $metadata->customRepositoryClassName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
61 356
            $repositoryClassName = $metadata->customRepositoryClassName;
62
        }
63
64
        switch (true) {
65
            case $metadata->isFile:
66 View Code Duplication
                if (! is_a($repositoryClassName, GridFSRepository::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67
                    throw MappingException::invalidRepositoryClass($documentName, $repositoryClassName, GridFSRepository::class);
68
                }
69
                break;
70
71
            case $metadata->isView():
72 View Code Duplication
                if (! is_a($repositoryClassName, ViewRepository::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
                    throw MappingException::invalidRepositoryClass($documentName, $repositoryClassName, ViewRepository::class);
74
                }
75
                break;
76
77
            case $metadata->isEmbeddedDocument:
78
                throw MongoDBException::cannotCreateRepository($documentName);
79
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
80
81
            case $metadata->isMappedSuperclass:
82
            default:
83 View Code Duplication
                if (! is_a($repositoryClassName, DocumentRepository::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84
                    throw MappingException::invalidRepositoryClass($documentName, $repositoryClassName, DocumentRepository::class);
85
                }
86
                break;
87
        }
88
89
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
90
    }
91
92
    /**
93
     * Instantiates requested repository.
94
     */
95
    abstract protected function instantiateRepository(string $repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata) : ObjectRepository;
96
}
97