Completed
Push — master ( 74c3c7...591b88 )
by Eric
08:09
created

RepositoryFactory::createRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
ccs 12
cts 12
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Resource\Repository\Doctrine\MongoDB;
13
14
use Doctrine\Common\Persistence\ObjectRepository;
15
use Doctrine\ODM\MongoDB\DocumentManager;
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory;
18
use Lug\Component\Registry\Model\RegistryInterface;
19
use Lug\Component\Resource\Model\ResourceInterface;
20
use Lug\Component\Resource\Repository\RepositoryInterface as BaseRepositoryInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class RepositoryFactory extends DefaultRepositoryFactory
26
{
27
    /**
28
     * @var RegistryInterface
29
     */
30
    private $resourceRegistry;
31
32
    /**
33
     * @param RegistryInterface $resourceRegistry
34
     */
35 9
    public function __construct(RegistryInterface $resourceRegistry)
36
    {
37 9
        $this->resourceRegistry = $resourceRegistry;
38 9
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 5 View Code Duplication
    protected function createRepository(DocumentManager $documentManager, $documentName)
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...
44
    {
45 5
        $metadata = $documentManager->getClassMetadata($documentName);
46 5
        $repository = $metadata->customRepositoryClassName;
47
48 5
        if ($repository === null) {
49 2
            $repository = $documentManager->getConfiguration()->getDefaultRepositoryClassName();
50 2
        }
51
52 5
        return $this->createResourceRepository(
53 5
            $repository,
54 5
            $documentManager,
55 5
            $metadata,
56 5
            $this->resolveResource($metadata->getName())
57 5
        );
58
    }
59
60
    /**
61
     * @param string                 $class
62
     * @param DocumentManager        $documentManager
63
     * @param ClassMetadata          $metadata
64
     * @param ResourceInterface|null $resource
65
     *
66
     * @return ObjectRepository
67
     */
68 4
    protected function createResourceRepository(
69
        $class,
70
        DocumentManager $documentManager,
71
        ClassMetadata $metadata,
72
        ResourceInterface $resource = null
73
    ) {
74 4
        if ($resource !== null && is_a($class, BaseRepositoryInterface::class, true)) {
75 2
            return new $class($documentManager, $documentManager->getUnitOfWork(), $metadata, $resource);
76
        }
77
78 2
        return parent::createRepository($documentManager, $metadata->getName());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (createRepository() instead of createResourceRepository()). Are you sure this is correct? If so, you might want to change this to $this->createRepository().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
79
    }
80
81
    /**
82
     * @param string $class
83
     *
84
     * @return ResourceInterface|null
85
     */
86 5
    private function resolveResource($class)
87
    {
88 5
        foreach ($this->resourceRegistry as $resource) {
89 3
            if ($resource->getModel() === $class) {
90 3
                return $resource;
91
            }
92 2
        }
93 2
    }
94
}
95