This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
ActionManager.php
178 lines (148 loc) · 4.76 KB
/
ActionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Spy\Timeline\Driver\Redis;
use Spy\Timeline\Driver\AbstractActionManager;
use Spy\Timeline\Driver\ActionManagerInterface;
use Spy\Timeline\Model\ActionInterface;
use Spy\Timeline\Model\ComponentInterface;
use Spy\Timeline\ResolveComponent\ValueObject\ResolvedComponentData;
use Spy\Timeline\ResultBuilder\ResultBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ActionManager extends AbstractActionManager implements ActionManagerInterface
{
/**
* @var object
*/
protected $client;
/**
* @var ResultBuilderInterface
*/
protected $resultBuilder;
/**
* @var string
*/
protected $prefix;
/**
* @param object $client client
* @param ResultBuilderInterface $resultBuilder resultBuilder
* @param string $prefix prefix
* @param string $actionClass actionClass
* @param string $componentClass componentClass
* @param string $actionComponentClass actionComponentClass
*/
public function __construct($client, ResultBuilderInterface $resultBuilder, $prefix, $actionClass, $componentClass, $actionComponentClass)
{
$this->client = $client;
$this->prefix = $prefix;
$this->resultBuilder = $resultBuilder;
parent::__construct($actionClass, $componentClass, $actionComponentClass);
}
/**
* {@inheritdoc}
*/
public function findActionsWithStatusWantedPublished($limit = 100)
{
throw new \Exception('Method '.__METHOD__.' is currently not supported by redis driver');
}
/**
* {@inheritdoc}
*/
public function countActions(ComponentInterface $subject, $status = ActionInterface::STATUS_PUBLISHED)
{
if ($status != ActionInterface::STATUS_PUBLISHED) {
throw new \Exception('Method '.__METHOD__.' can only retrieve action published');
}
$redisKey = $this->getSubjectRedisKey($subject);
return $this->client->zCard($redisKey);
}
/**
* {@inheritdoc}
*/
public function getSubjectActions(ComponentInterface $subject, array $options = array())
{
$resolver = new OptionsResolver();
$resolver->setDefaults(array(
'page' => 1,
'max_per_page' => 10,
'filter' => true,
'paginate' => false,
));
$options = $resolver->resolve($options);
$token = new Pager\PagerToken($this->getSubjectRedisKey($subject));
return $this->resultBuilder->fetchResults($token, $options['page'], $options['max_per_page'], $options['filter'], $options['paginate']);
}
/**
* {@inheritdoc}
*/
public function updateAction(ActionInterface $action)
{
$action->setId($this->getNextId());
$this->client->hset($this->getActionKey(), $action->getId(), serialize($action));
$this->deployActionDependOnDelivery($action);
}
/**
* {@inheritdoc}
*/
public function findOrCreateComponent($model, $identifier = null, $flush = true)
{
return $this->createComponent($model, $identifier, $flush);
}
/**
* {@inheritdoc}
*/
public function createComponent($model, $identifier = null, $flush = true)
{
$resolvedComponentData = $this->resolveModelAndIdentifier($model, $identifier);
// we do not persist component on redis driver.
return $this->getComponentFromResolvedComponentData($resolvedComponentData);
}
/**
* {@inheritdoc}
*/
public function flushComponents()
{
}
/**
* {@inheritdoc}
*/
public function findComponents(array $concatIdents)
{
$components = array();
foreach ($concatIdents as $concatIdent) {
$component = new $this->componentClass();
$components[] = $component->createFromHash($concatIdent);
}
return $components;
}
/**
* {@inheritdoc}
*/
public function findComponentWithHash($hash)
{
$component = new $this->componentClass();
$component = $component->createFromHash($hash);
return $component;
}
/**
* @return integer|double
*/
protected function getNextId()
{
return ($this->client->hlen($this->getActionKey()) + 1);
}
/**
* @return string
*/
protected function getActionKey()
{
return sprintf('%s:action', $this->prefix);
}
/**
* @param ComponentInterface $subject subject
*
* @return string
*/
protected function getSubjectRedisKey(ComponentInterface $subject)
{
return sprintf('%s:%s', $this->prefix, $subject->getHash());
}
}