Completed
Push — master ( 8cb42d...e9bfab )
by
unknown
19:56
created

CollectionCache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 160
Duplicated Lines 24.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 70.2%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 3
dl 39
loc 160
ccs 33
cts 47
cp 0.702
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A buildCacheKey() 0 4 1
A getCollectionCacheTime() 0 7 2
A getByRepository() 13 13 3
A setByRepository() 10 10 2
A setConfiguration() 0 4 1
A updateOperationCheck() 9 9 2
A addUpdateLock() 7 7 1
A releaseUpdateLock() 0 12 2

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
 * 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 $result;
92
        }
93
        return false;
94
    }
95
96
    /**
97
     * @param Repository $repository DB Repository
98
     * @param string     $serialized Serialised Object document
99
     * @param string     $id         Object document identifier
100
     * @return bool
101
     */
102 4 View Code Duplication
    public function setByRepository(Repository $repository, $serialized, $id)
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...
103
    {
104 4
        $collection = $repository->getClassMetadata()->collection;
105 4
        if (!$time = $this->getCollectionCacheTime($collection)) {
106 2
            return false;
107
        }
108 2
        $key = $this->buildCacheKey($collection, $id);
109
110 2
        return $this->cache->save($key, $serialized, $time);
111
    }
112
113
    /**
114
     * Will sleep until previous operation has finished.
115
     * Default sleep time 10 seconds.
116
     * Loops by 1/4 second while there is a cache update lock
117
     *
118
     * @param Repository $repository Model repository
119
     * @param string     $id         Object identifier
120
     *
121
     * @return void
122
     */
123 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...
124
    {
125 2
        $collection = $repository->getClassMetadata()->collection;
126 2
        $key = self::BASE_UPDATE_KEY.'-'.$this->buildCacheKey($collection, $id);
127
128 2
        while ($this->cache->fetch($key)) {
129 2
            usleep(250000);
130 1
        }
131 2
    }
132
133
    /**
134
     * Will add update lock
135
     *
136
     * @param Repository $repository Model repository
137
     * @param string     $id         Object identifier
138
     * @param integer    $maxTime    Set timeout for lock
139
     *
140
     * @return bool
141
     */
142 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...
143
    {
144 2
        $collection = $repository->getClassMetadata()->collection;
145 2
        $key = self::BASE_UPDATE_KEY.'-'.$this->buildCacheKey($collection, $id);
146
147 2
        return  $this->cache->save($key, true, $maxTime);
148
    }
149
150
    /**
151
     * Will remove lock if there was one.
152
     *
153
     * @param Repository $repository Model repository
154
     * @param string     $id         Object identifier
155
     *
156
     * @return void
157
     */
158
    public function releaseUpdateLock(Repository $repository, $id)
159
    {
160
        $collection = $repository->getClassMetadata()->collection;
161
        $baseKey = $this->buildCacheKey($collection, $id);
162
        $key = self::BASE_UPDATE_KEY.'-'.$baseKey;
163
164
        $this->cache->delete($key);
165
166
        if ($this->getCollectionCacheTime($collection)) {
167
            $this->cache->delete($baseKey);
168
        }
169
    }
170
171
    /**
172
     * Update cache if needed
173
     *
174
     * @param array $configuration configuration array
175
     * @return void
176
     */
177
    public function setConfiguration($configuration)
178
    {
179
        $this->config = $configuration;
180
    }
181
}
182