Completed
Pull Request — master (#4)
by Pavel
10:02
created

MapperRegistry   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 32.26%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 99
ccs 10
cts 31
cp 0.3226
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultConnectionName() 0 4 1
A getConnection() 0 4 1
A getConnections() 0 4 1
A getConnectionNames() 0 4 1
A getDefaultManagerName() 0 4 1
A getManager() 0 8 3
A getManagers() 0 4 1
A resetManager() 0 4 1
A getAliasNamespace() 0 4 1
A getManagerNames() 0 4 1
A getRepository() 0 4 1
A getManagerForClass() 0 8 2
1
<?php
2
3
namespace Bankiru\Api\Adapters\Sensio;
4
5
use Bankiru\Api\Doctrine\ApiEntityManager;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
8
final class MapperRegistry implements ManagerRegistry
9
{
10
    /** @var ApiEntityManager */
11
    private $manager;
12
13
    /**
14
     * MapperRegistry constructor.
15
     *
16
     * @param ApiEntityManager $manager
17
     */
18 1
    public function __construct(ApiEntityManager $manager)
19
    {
20 1
        $this->manager = $manager;
21 1
    }
22
23
    /** {@inheritdoc} */
24
    public function getDefaultConnectionName()
25
    {
26
        return null;
27
    }
28
29
    /** {@inheritdoc} */
30
    public function getConnection($name = null)
31
    {
32
        return null;
33
    }
34
35
    /**
36
     * Gets an array of all registered connections.
37
     *
38
     * @return array An array of Connection instances.
39
     */
40
    public function getConnections()
41
    {
42
        return [];
43
    }
44
45
    /** {@inheritdoc} */
46
    public function getConnectionNames()
47
    {
48
        return [];
49
    }
50
51
    /** {@inheritdoc} */
52 1
    public function getDefaultManagerName()
53
    {
54 1
        return 'default';
55
    }
56
57
    /** {@inheritdoc} */
58
    public function getManager($name = null)
59
    {
60
        if (null === $name || $this->getDefaultManagerName() === $name) {
61
            return $this->manager;
62
        }
63
64
        throw new \OutOfBoundsException('Invalid API entity manager: ' . $name);
65
    }
66
67
    /** {@inheritdoc} */
68 1
    public function getManagers()
69
    {
70 1
        return [$this->getDefaultManagerName() => $this->manager];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($this->getD...e() => $this->manager); (array<string,Bankiru\Api...trine\ApiEntityManager>) is incompatible with the return type declared by the interface Doctrine\Common\Persiste...erRegistry::getManagers of type Doctrine\Common\Persistence\ObjectManager[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
71
    }
72
73
    /** {@inheritdoc} */
74
    public function resetManager($name = null)
75
    {
76
        return $this->getManager($name);
77
    }
78
79
    /** {@inheritdoc} */
80
    public function getAliasNamespace($alias)
81
    {
82
        throw new \OutOfBoundsException('Unable to resolve alias ' . $alias);
83
    }
84
85
    /** {@inheritdoc} */
86
    public function getManagerNames()
87
    {
88
        return [$this->getDefaultManagerName()];
89
    }
90
91
    /** {@inheritdoc} */
92
    public function getRepository($persistentObject, $persistentManagerName = null)
93
    {
94
        return $this->getManager($persistentManagerName)->getRepository($persistentObject);
95
    }
96
97
    /** {@inheritdoc} */
98 1
    public function getManagerForClass($class)
99
    {
100 1
        if (!$this->manager->getMetadataFactory()->isTransient($class)) {
101 1
            return $this->manager;
102
        }
103
104
        return null;
105
    }
106
}
107