|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\ViewReferenceBundle\Connector\Redis; |
|
4
|
|
|
|
|
5
|
|
|
use Predis\ClientInterface; |
|
6
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceConnectorRepositoryInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ViewReferenceRedisRepository. |
|
10
|
|
|
*/ |
|
11
|
|
|
class ViewReferenceRedisRepository implements ViewReferenceConnectorRepositoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected $redis; |
|
14
|
|
|
private $alias = 'reference'; |
|
15
|
|
|
private $tools; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ViewReferenceRedisRepository constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param ClientInterface $redis |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(ClientInterface $redis) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->redis = $redis; |
|
25
|
|
|
$this->tools = new ViewReferenceRedisTool(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getAll() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->redis->keys($this->alias.'*'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getAllBy(array $criteria, $type = 'AND') |
|
40
|
|
|
{ |
|
41
|
|
|
$filters = []; |
|
42
|
|
|
$criteria = $this->tools->redislizeArray($criteria); |
|
43
|
|
|
// Create hashs for every index needed |
|
44
|
|
|
foreach ($criteria as $key => $value) { |
|
45
|
|
|
$filters[] = $this->tools->generateKey($key.'_'.$this->alias, $value); |
|
46
|
|
|
} |
|
47
|
|
|
// Call the right method for "AND" or "OR" |
|
48
|
|
|
if ($type != 'OR') { |
|
49
|
|
|
return $this->redis->sinter($filters); |
|
50
|
|
|
} else { |
|
51
|
|
|
return $this->redis->sunion($filters); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getResults(array $data) |
|
59
|
|
|
{ |
|
60
|
|
|
$results = []; |
|
61
|
|
|
foreach ($data as $item) { |
|
62
|
|
|
$results[] = $this->findById($item); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $results; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function findById($id) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->tools->unredislizeArray($this->redis->hgetall($this->alias.':'.$id)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function findValueForId($value, $id) |
|
77
|
|
|
{ |
|
78
|
|
|
$reference = $this->findById($id); |
|
79
|
|
|
if (!isset($reference[$value])) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->tools->unredislize($reference[$value]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getChildren($id) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->redis->smembers($this->alias.':'.$id.':children'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $alias |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setAlias($alias) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->alias = $alias; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function findRefIdByUrl($url = '', $locale = 'fr') |
|
110
|
|
|
{ |
|
111
|
|
|
if ($url == '' || $url[0] != '/') { |
|
112
|
|
|
$url = '/'.$url; |
|
113
|
|
|
} |
|
114
|
|
|
$refId = $this->tools->unredislize($this->redis->get($locale.':'.$url)); |
|
115
|
|
|
|
|
116
|
|
|
return $refId; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|