Completed
Push — dev ( e5ce98...4b06a2 )
by Андрей
02:24
created

EntityMapCache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 200
Duplicated Lines 11 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 3
dl 22
loc 200
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A saveEntityMap() 0 8 1
A loadEntityMap() 12 12 2
A deleteEntityMap() 10 10 2
A hasEntityMap() 0 6 1
A getCacheKeyForObjectManagerName() 0 13 2
A getEntityMapBuilder() 0 4 1
A setEntityMapBuilder() 0 6 1
A getCache() 0 4 1
A setCache() 0 6 1
A getModuleOptions() 0 4 1
A setModuleOptions() 0 6 1

How to fix   Duplicated Code   

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:

1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine\Utils;
7
8
use Doctrine\Common\Cache\Cache;
9
use Nnx\Doctrine\Options\ModuleOptionsInterface;
10
11
/**
12
 * Class EntityMapCache
13
 *
14
 * @package Nnx\Doctrine\Utils
15
 */
16
class EntityMapCache implements EntityMapCacheInterface
17
{
18
    /**
19
     * Сервис для построения карты сущностей Doctrine2
20
     *
21
     * @var EntityMapBuilderInterface
22
     */
23
    protected $entityMapBuilder;
24
25
    /**
26
     * Кеш в который сохрянется карта сущностей Doctrine2
27
     *
28
     * @var Cache
29
     */
30
    protected $cache;
31
32
    /**
33
     * Объект с настройками модуля
34
     *
35
     * @var ModuleOptionsInterface
36
     */
37
    protected $moduleOptions;
38
39
    /**
40
     * Ключ кеширования
41
     *
42
     * @var array
43
     */
44
    protected $cacheKeys = [];
45
46
    /**
47
     * EntityMapCache constructor.
48
     *
49
     * @param EntityMapBuilderInterface $entityMapBuilder
50
     * @param Cache                     $cache
51
     * @param ModuleOptionsInterface    $moduleOptions
52
     */
53
    public function __construct(
54
        EntityMapBuilderInterface $entityMapBuilder,
55
        Cache $cache,
56
        ModuleOptionsInterface $moduleOptions
57
    ) {
58
        $this->setEntityMapBuilder($entityMapBuilder);
59
        $this->setCache($cache);
60
        $this->setModuleOptions($moduleOptions);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     *
66
     * @param $objectManagerName
67
     *
68
     * @return boolean
69
     */
70
    public function saveEntityMap($objectManagerName)
71
    {
72
        $map = $this->getEntityMapBuilder()->buildEntityMapByObjectManagerName($objectManagerName);
73
74
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
75
76
        return $this->getCache()->save($key, $map);
77
    }
78
79
    /**
80
     * Загружает карту сущностей
81
     *
82
     * @param $objectManagerName
83
     *
84
     * @return array|null
85
     */
86 View Code Duplication
    public function loadEntityMap($objectManagerName)
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...
87
    {
88
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
89
90
        $cache = $this->getCache();
91
92
        if (false === $cache->contains($key)) {
93
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Nnx\Doctrine\Utils\Entit...nterface::loadEntityMap of type array|null.

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...
94
        }
95
96
        return $cache->fetch($key);
97
    }
98
99
    /**
100
     * Удаляет карту сущностей
101
     *
102
     * @param $objectManagerName
103
     *
104
     * @return void
105
     */
106 View Code Duplication
    public function deleteEntityMap($objectManagerName)
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...
107
    {
108
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
109
110
        $cache = $this->getCache();
111
112
        if (false === $cache->contains($key)) {
113
            $cache->delete($key);
114
        }
115
    }
116
117
    /**
118
     * Проверяет есть ли в кеше карта сущностей для заданного ObjectManager
119
     *
120
     * @param $objectManagerName
121
     *
122
     * @return array|null
123
     */
124
    public function hasEntityMap($objectManagerName)
125
    {
126
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
127
128
        return $this->getCache()->contains($key);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getCache()->contains($key); (boolean) is incompatible with the return type declared by the interface Nnx\Doctrine\Utils\Entit...Interface::hasEntityMap of type array|null.

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...
129
    }
130
131
    /**
132
     * Получает ключ для кеширования карты сущностей
133
     *
134
     * @param $objectManagerName
135
     *
136
     * @return string
137
     */
138
    public function getCacheKeyForObjectManagerName($objectManagerName)
139
    {
140
        if (array_key_exists($objectManagerName, $this->cacheKeys)) {
141
            return $this->cacheKeys[$objectManagerName];
142
        }
143
144
        $preffix = $this->getModuleOptions()->getEntityMapDoctrineCachePrefix();
145
        $key = $preffix . '_' . $objectManagerName;
146
147
        $this->cacheKeys[$objectManagerName] = $key;
148
149
        return $this->cacheKeys[$objectManagerName];
150
    }
151
152
    /**
153
     * @return EntityMapBuilderInterface
154
     */
155
    public function getEntityMapBuilder()
156
    {
157
        return $this->entityMapBuilder;
158
    }
159
160
    /**
161
     * @param EntityMapBuilderInterface $entityMapBuilder
162
     *
163
     * @return $this
164
     */
165
    public function setEntityMapBuilder(EntityMapBuilderInterface $entityMapBuilder)
166
    {
167
        $this->entityMapBuilder = $entityMapBuilder;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return Cache
174
     */
175
    public function getCache()
176
    {
177
        return $this->cache;
178
    }
179
180
    /**
181
     * @param Cache $cache
182
     *
183
     * @return $this
184
     */
185
    public function setCache(Cache $cache)
186
    {
187
        $this->cache = $cache;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Возвращает объект с настройками модуля
194
     *
195
     * @return ModuleOptionsInterface
196
     */
197
    public function getModuleOptions()
198
    {
199
        return $this->moduleOptions;
200
    }
201
202
    /**
203
     * Устанавливает  объект с настройками модуля
204
     *
205
     * @param ModuleOptionsInterface $moduleOptions
206
     *
207
     * @return $this
208
     */
209
    public function setModuleOptions(ModuleOptionsInterface $moduleOptions)
210
    {
211
        $this->moduleOptions = $moduleOptions;
212
213
        return $this;
214
    }
215
}
216