Completed
Push — dev ( 526cfa...e5ce98 )
by Андрей
03:57
created

EntityMapCache::loadEntityMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

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