Completed
Push — feature/EVO-8323-configurable-... ( 36aaf7 )
by
unknown
15:55
created

CollectionCache::buildCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * ExtReferenceConverter class file
4
 */
5
6
namespace Graviton\DocumentBundle\Service;
7
8
use Doctrine\Common\Cache\CacheProvider;
9
use Doctrine\ODM\MongoDB\DocumentRepository as Repository;
10
11
/**
12
 * graviton.cache.collections
13
14
 * Collection Cache Service
15
 *
16
 *
17
 *
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class CollectionCache
23
{
24
    /** Prefix cache key */
25
    const BASE_KEY = 'CollectionCache';
26
27
    /** Prefix cache key */
28
    const BASE_UPDATE_KEY = 'CollectionUpdate';
29
30
    /** @var array  */
31
    private $config = [];
32
33
    /** @var CacheProvider */
34
    private $cache;
35
36
    /**
37
     * CollectionCache constructor.
38
     * @param CacheProvider $cache         Cache provider
39
     * @param array         $configuration Collections to be saved
40
     */
41 10
    public function __construct(
42
        CacheProvider $cache,
43
        $configuration
44
    ) {
45 10
        $this->cache = $cache;
46 10
        $this->config = $configuration;
47 10
    }
48
49
    /**
50
     * Makes an id
51
     *
52
     * @param string $collection DB collection name
53
     * @param string $id         Object Identifier
54
     * @return string
55
     */
56 6
    private function buildCacheKey($collection, $id)
57
    {
58 6
        return self::BASE_KEY.'-'.preg_replace("/[^a-zA-Z0-9_-]+/", "-", $collection.'-'.$id);
59
    }
60
61
    /**
62
     * Time it should be in cache and if so should happen
63
     *
64
     * @param string $collection DB collection name
65
     * @return int
66
     */
67 6
    private function getCollectionCacheTime($collection)
68
    {
69 6
        if (array_key_exists($collection, $this->config)) {
70 4
            return (int) $this->config[$collection];
71
        }
72 2
        return 0;
73
    }
74
75
    /**
76
     * Return un cached object.
77
     *
78
     * @param Repository $repository DB Repository
79
     * @param string     $id         Queried is
80
     * @return object|false if no cache found
81
     */
82 2 View Code Duplication
    public function getByRepository(Repository $repository, $id)
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...
83
    {
84 2
        $collection = $repository->getClassMetadata()->collection;
85 2
        if (!$this->getCollectionCacheTime($collection)) {
86
            return false;
87
        }
88 2
        $key = $this->buildCacheKey($collection, $id);
89
90 2
        if ($result = $this->cache->fetch($key)) {
91 2
            return unserialize($result);
92
        }
93
        return false;
94
    }
95
96
    /**
97
     * @param Repository $repository DB Repository
98
     * @param object     $document   Object document
99
     * @return bool
100
     */
101 4 View Code Duplication
    public function setByRepository(Repository $repository, $document)
0 ignored issues
show
Coding Style introduced by
function setByRepository() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
102
    {
103 4
        if (empty($document)) {
104
            return false;
105
        }
106 4
        $collection = $repository->getClassMetadata()->collection;
107 4
        if (!$time = $this->getCollectionCacheTime($collection)) {
108 2
            return false;
109
        }
110 2
        $key = $this->buildCacheKey($collection, $document->getId());
111
112 2
        return $this->cache->save($key, serialize($document), $time);
113
    }
114
115
    /**
116
     * Will sleep until previous operation has finished but for max 10s
117
     * Loops by 1/4 second
118
     *
119
     * @param Repository $repository Model repository
120
     * @param string     $id         Object identifier
121
     *
122
     * @return void
123
     */
124 2 View Code Duplication
    public function updateOperationCheck(Repository $repository, $id)
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...
125
    {
126 2
        $collection = $repository->getClassMetadata()->collection;
127 2
        $key = self::BASE_UPDATE_KEY.'-'.$this->buildCacheKey($collection, $id);
128
129 2
        while ($this->cache->fetch($key)) {
130 2
            sleep(0.25);
131 1
        }
132 2
    }
133
134
    /**
135
     * Will add update lock
136
     *
137
     * @param Repository $repository Model repository
138
     * @param string     $id         Object identifier
139
     * @param integer    $maxTime    Set timeout for lock
140
     *
141
     * @return bool
142
     */
143 2 View Code Duplication
    public function addUpdateLock(Repository $repository, $id, $maxTime = 10)
0 ignored issues
show
Coding Style introduced by
function addUpdateLock() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
144
    {
145 2
        $collection = $repository->getClassMetadata()->collection;
146 2
        $key = self::BASE_UPDATE_KEY.'-'.$this->buildCacheKey($collection, $id);
147
148 2
        return  $this->cache->save($key, true, $maxTime);
149
    }
150
151
    /**
152
     * Will remove lock if there was one.
153
     *
154
     * @param Repository $repository Model repository
155
     * @param string     $id         Object identifier
156
     *
157
     * @return void
158
     */
159
    public function releaseUpdateLock(Repository $repository, $id)
160
    {
161
        $collection = $repository->getClassMetadata()->collection;
162
        $baseKey = $this->buildCacheKey($collection, $id);
163
        $key = self::BASE_UPDATE_KEY.'-'.$baseKey;
164
        if ($this->cache->delete($key)) {
165
            $this->cache->delete($baseKey);
166
        }
167
    }
168
169
    /**
170
     * Update cache if needed
171
     *
172
     * @param array $configuration configuration array
173
     * @return void
174
     */
175
    public function setConfiguration($configuration)
176
    {
177
        $this->config = $configuration;
178
    }
179
}
180