|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\ViewReferenceBundle\Connector\Redis; |
|
4
|
|
|
|
|
5
|
|
|
use Predis\ClientInterface; |
|
6
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceConnectorManagerInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ViewReferenceRedisManager. |
|
10
|
|
|
*/ |
|
11
|
|
|
class ViewReferenceRedisManager implements ViewReferenceConnectorManagerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected $redis; |
|
14
|
|
|
protected $tools; |
|
15
|
|
|
protected $alias = 'reference'; |
|
16
|
|
|
protected $repository; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ViewReferenceRedisManager constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param ClientInterface $redis |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(ClientInterface $redis, ViewReferenceRedisRepository $repository) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->redis = $redis; |
|
26
|
|
|
$this->tools = new ViewReferenceRedisTool(); |
|
27
|
|
|
$this->repository = $repository; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function create(array $data) |
|
34
|
|
|
{ |
|
35
|
|
|
// To generate a new view reference we need an id |
|
36
|
|
|
if (!isset($data['id']) && empty($data['id'])) { |
|
37
|
|
|
throw new \Exception('You can\'t create a redis item without an id.'); |
|
38
|
|
|
} |
|
39
|
|
|
$id = $data['id']; |
|
40
|
|
|
// The hash/key for the view reference is generated |
|
41
|
|
|
$hash = $this->tools->generateKey($this->alias, $id); |
|
42
|
|
|
|
|
43
|
|
|
// If if we already have a same hash we throw an exception |
|
44
|
|
|
if ($this->redis->exists($hash)) { |
|
45
|
|
|
throw new \Exception('An redis hash "'.$this->alias.'" with id '.$hash.' already exist'); |
|
46
|
|
|
} |
|
47
|
|
|
// Data have to be "redislize" |
|
48
|
|
|
$values = $this->tools->redislizeArray($data); |
|
49
|
|
|
// Data are set for the specified hash |
|
50
|
|
|
$this->redis->hmset($hash, $values); |
|
51
|
|
|
// Index values to be able to query them |
|
52
|
|
|
$this->index($id, $values); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function update($id, array $data) |
|
59
|
|
|
{ |
|
60
|
|
|
$data['id'] = $id; |
|
61
|
|
|
$hash = $this->tools->generateKey($this->alias, $id); |
|
62
|
|
|
// If a reference with same hash exist |
|
63
|
|
|
if ($this->redis->exists($hash)) { |
|
64
|
|
|
$oldValues = $this->redis->hgetall($hash); |
|
65
|
|
|
$oldValues = $this->tools->unredislizeArray($oldValues); |
|
66
|
|
|
// The old reference is removed |
|
67
|
|
|
$this->remove($id); |
|
68
|
|
|
// Old and new data are merge |
|
69
|
|
|
$data = array_merge($oldValues, $data); |
|
70
|
|
|
} |
|
71
|
|
|
// Create the new reference |
|
72
|
|
|
$this->create($data); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function remove($id) |
|
79
|
|
|
{ |
|
80
|
|
|
$hash = $this->tools->generateKey($this->alias, $id); |
|
81
|
|
|
$values = $this->redis->hgetall($hash); |
|
82
|
|
View Code Duplication |
foreach ($values as $name => $value) { |
|
|
|
|
|
|
83
|
|
|
// Remove index for this reference |
|
84
|
|
|
$key = $this->tools->generateKey($name.'_'.$this->alias, $value); |
|
85
|
|
|
$this->redis->srem($key, $id); |
|
86
|
|
|
} |
|
87
|
|
|
// Remove the reference |
|
88
|
|
|
$this->redis->del($hash); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* This method index a reference. |
|
93
|
|
|
* |
|
94
|
|
|
* @param $id |
|
95
|
|
|
* @param $values |
|
96
|
|
|
*/ |
|
97
|
|
|
public function index($id, $values) |
|
98
|
|
|
{ |
|
99
|
|
View Code Duplication |
foreach ($values as $name => $value) { |
|
|
|
|
|
|
100
|
|
|
$key = $this->tools->generateKey($name.'_'.$this->alias, $value); |
|
101
|
|
|
// Store id for value |
|
102
|
|
|
$this->redis->sadd($key, $id); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function addChild($parentId, $childId) |
|
110
|
|
|
{ |
|
111
|
|
|
$parentHash = $this->tools->generateKey($this->alias, $parentId); |
|
112
|
|
|
$childHash = $this->tools->generateKey($this->alias, $childId); |
|
113
|
|
|
$hash = $this->tools->generateKey($parentHash, 'children'); |
|
114
|
|
|
// Index the child in list of children to parent |
|
115
|
|
|
$this->redis->sadd($hash, $childId); |
|
116
|
|
|
// Index and add the value of parent for the child |
|
117
|
|
|
$this->redis->hset($childHash, 'parent', $parentId); |
|
118
|
|
|
$this->redis->sadd('parent_'.$parentHash, $childId); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function buildUrl($id) |
|
125
|
|
|
{ |
|
126
|
|
|
$reference = $this->repository->findById($id); |
|
127
|
|
|
$locale = $this->repository->findValueForId('locale', $id); |
|
128
|
|
|
$url = ''; |
|
129
|
|
|
// while the reference has a slug |
|
130
|
|
|
while (isset($reference['slug']) && $reference['slug'] != '') { |
|
131
|
|
|
// Build url |
|
132
|
|
|
if ($url != '') { |
|
133
|
|
|
$url = $reference['slug'].'/'.$url; |
|
134
|
|
|
} else { |
|
135
|
|
|
$url = $reference['slug']; |
|
136
|
|
|
} |
|
137
|
|
|
// Set reference with the parent |
|
138
|
|
|
if ($parentId = $reference['parent']) { |
|
139
|
|
|
$reference = $this->repository->findById($parentId); |
|
140
|
|
|
} else { |
|
141
|
|
|
$reference = []; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
// set the new url |
|
145
|
|
|
$this->setUrl($id, $url, $locale); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setUrl($refId, $url, $locale = 'fr') |
|
152
|
|
|
{ |
|
153
|
|
|
//if an url exist for the current reference |
|
154
|
|
|
if ($this->redis->hexists('reference:'.$refId, 'url')) { |
|
155
|
|
|
// Remove the old url |
|
156
|
|
|
$refUrl = $this->tools->unredislize($this->redis->hget('reference:'.$refId, 'url')); |
|
157
|
|
|
if ($refUrl != '') { |
|
158
|
|
|
$this->removeUrl($refUrl, $locale); |
|
159
|
|
|
$this->redis->hdel('reference:'.$refId, 'url'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
// Set the new url |
|
163
|
|
|
$this->redis->set($locale.':/'.$url, $refId); |
|
164
|
|
|
$this->redis->hset('reference:'.$refId, 'url', $url); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* {@inheritdoc} |
|
169
|
|
|
*/ |
|
170
|
|
|
public function removeUrl($url, $locale = 'fr') |
|
171
|
|
|
{ |
|
172
|
|
|
if ($url == '' || $url[0] != '/') { |
|
173
|
|
|
$url = '/'.$url; |
|
174
|
|
|
} |
|
175
|
|
|
$this->redis->del($locale.':'.$url); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
*/ |
|
181
|
|
|
public function reset() |
|
182
|
|
|
{ |
|
183
|
|
|
$this->redis->flushall(); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
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.