Completed
Push — dev ( 9779e8...896128 )
by Андрей
02:24
created

ObjectManagerAutoDetector::hasObjectManagerNameByClassName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine\ObjectManager;
7
8
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
9
10
/**
11
 * Class ObjectManagerAutoDetector
12
 *
13
 * @package Nnx\Doctrine\ObjectManager
14
 */
15
class ObjectManagerAutoDetector implements ObjectManagerAutoDetectorInterface
16
{
17
    /**
18
     * Менеджер для работы с настройками модуля
19
     *
20
     * @var ModuleOptionsPluginManagerInterface
21
     */
22
    protected $moduleOptionsManager;
23
24
    /**
25
     * ObjectManagerAutoDetector constructor.
26
     *
27
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsManager
28
     */
29
    public function __construct(ModuleOptionsPluginManagerInterface $moduleOptionsManager)
30
    {
31
        $this->setModuleOptionsManager($moduleOptionsManager);
32
    }
33
34
    /**
35
     * Получает имя используемого в модуле ObjectManager'a Doctrine, по имени любого класса модуля
36
     *
37
     * @param $className
38
     *
39
     * @return string
40
     *
41
     * @throws Exception\RuntimeException
42
     */
43 View Code Duplication
    public function getObjectManagerNameByClassName($className)
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
        $moduleOptions = $this->getModuleOptionsManager()->getOptionsByClassName($className);
46
47
        if (!$moduleOptions instanceof ObjectManagerNameProviderInterface) {
48
            $errMsg = sprintf('Module options not implement %s', ObjectManagerNameProviderInterface::class);
49
            throw new Exception\RuntimeException($errMsg);
50
        }
51
52
        $objectManagerName = $moduleOptions->getObjectManagerName();
53
54
        if (!is_string($objectManagerName)) {
55
            $errMsg = 'Invalid object manager name. Manager name not string';
56
            throw new Exception\RuntimeException($errMsg);
57
        }
58
59
        return $objectManagerName;
60
    }
61
62
63
    /**
64
     * Проверяет есть ли возможность по имени класса модуля, получить имя objectManager'a который используется в данном модуле
65
     *
66
     * @param $className
67
     *
68
     * @return boolean
69
     */
70 View Code Duplication
    public function hasObjectManagerNameByClassName($className)
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...
71
    {
72
        $moduleOptions = $this->getModuleOptionsManager()->getOptionsByClassName($className);
73
74
        if (!$moduleOptions instanceof ObjectManagerNameProviderInterface) {
75
            $errMsg = sprintf('Module options not implement %s', ObjectManagerNameProviderInterface::class);
76
            throw new Exception\RuntimeException($errMsg);
77
        }
78
79
        $objectManagerName = $moduleOptions->getObjectManagerName();
80
81
        if (!is_string($objectManagerName)) {
82
            $errMsg = 'Invalid object manager name. Manager name not string';
83
            throw new Exception\RuntimeException($errMsg);
84
        }
85
86
        return $objectManagerName;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $objectManagerName; (string) is incompatible with the return type declared by the interface Nnx\Doctrine\ObjectManag...tManagerNameByClassName of type boolean.

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...
87
    }
88
89
90
91
92
93
    /**
94
     * Возвращает менеджер для работы с настройками модуля
95
     *
96
     * @return ModuleOptionsPluginManagerInterface
97
     */
98
    public function getModuleOptionsManager()
99
    {
100
        return $this->moduleOptionsManager;
101
    }
102
103
    /**
104
     * Устанавливает менеджер для работы с настройками модуля
105
     *
106
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsManager
107
     *
108
     * @return $this
109
     */
110
    public function setModuleOptionsManager(ModuleOptionsPluginManagerInterface $moduleOptionsManager)
111
    {
112
        $this->moduleOptionsManager = $moduleOptionsManager;
113
114
        return $this;
115
    }
116
}
117