EntityMapCache::loadEntityMap()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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
        $moduleOptions =  $this->getModuleOptions();
73
        $originalFlag = $this->getModuleOptions()->getFlagDisableUseEntityMapDoctrineCache();
74
        $moduleOptions->setFlagDisableUseEntityMapDoctrineCache(true);
75
76
        $map = $this->getEntityMapBuilder()->buildEntityMapByObjectManagerName($objectManagerName);
77
78
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
79
        $resultSaveCache = $this->getCache()->save($key, $map);
80
        $moduleOptions->setFlagDisableUseEntityMapDoctrineCache($originalFlag);
81
82
        return $resultSaveCache;
83
    }
84
85
    /**
86
     * Загружает карту сущностей
87
     *
88
     * @param $objectManagerName
89
     *
90
     * @return array|null
91
     */
92 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...
93
    {
94
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
95
96
        $cache = $this->getCache();
97
98
        if (false === $cache->contains($key)) {
99
            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...
100
        }
101
102
        return $cache->fetch($key);
103
    }
104
105
    /**
106
     * Удаляет карту сущностей
107
     *
108
     * @param $objectManagerName
109
     *
110
     * @return void
111
     */
112 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...
113
    {
114
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
115
116
        $cache = $this->getCache();
117
118
        if (true === $cache->contains($key)) {
119
            $cache->delete($key);
120
        }
121
    }
122
123
    /**
124
     * Проверяет есть ли в кеше карта сущностей для заданного ObjectManager
125
     *
126
     * @param $objectManagerName
127
     *
128
     * @return bool
129
     */
130
    public function hasEntityMap($objectManagerName)
131
    {
132
        $key = $this->getCacheKeyForObjectManagerName($objectManagerName);
133
134
        return $this->getCache()->contains($key);
135
    }
136
137
    /**
138
     * Получает ключ для кеширования карты сущностей
139
     *
140
     * @param $objectManagerName
141
     *
142
     * @return string
143
     */
144
    public function getCacheKeyForObjectManagerName($objectManagerName)
145
    {
146
        if (array_key_exists($objectManagerName, $this->cacheKeys)) {
147
            return $this->cacheKeys[$objectManagerName];
148
        }
149
150
        $preffix = $this->getModuleOptions()->getEntityMapDoctrineCachePrefix();
151
        $key = $preffix . '_' . $objectManagerName;
152
153
        $this->cacheKeys[$objectManagerName] = $key;
154
155
        return $this->cacheKeys[$objectManagerName];
156
    }
157
158
    /**
159
     * @return EntityMapBuilderInterface
160
     */
161
    public function getEntityMapBuilder()
162
    {
163
        return $this->entityMapBuilder;
164
    }
165
166
    /**
167
     * @param EntityMapBuilderInterface $entityMapBuilder
168
     *
169
     * @return $this
170
     */
171
    public function setEntityMapBuilder(EntityMapBuilderInterface $entityMapBuilder)
172
    {
173
        $this->entityMapBuilder = $entityMapBuilder;
174
175
        return $this;
176
    }
177
178
    /**
179
     * @return Cache
180
     */
181
    public function getCache()
182
    {
183
        return $this->cache;
184
    }
185
186
    /**
187
     * @param Cache $cache
188
     *
189
     * @return $this
190
     */
191
    public function setCache(Cache $cache)
192
    {
193
        $this->cache = $cache;
194
195
        return $this;
196
    }
197
198
    /**
199
     * Возвращает объект с настройками модуля
200
     *
201
     * @return ModuleOptionsInterface
202
     */
203
    public function getModuleOptions()
204
    {
205
        return $this->moduleOptions;
206
    }
207
208
    /**
209
     * Устанавливает  объект с настройками модуля
210
     *
211
     * @param ModuleOptionsInterface $moduleOptions
212
     *
213
     * @return $this
214
     */
215
    public function setModuleOptions(ModuleOptionsInterface $moduleOptions)
216
    {
217
        $this->moduleOptions = $moduleOptions;
218
219
        return $this;
220
    }
221
}
222