1 | <?php |
||
15 | class Gateway |
||
16 | { |
||
17 | |||
18 | const REDIS_KEY = 'expressions'; |
||
19 | |||
20 | /** |
||
21 | * @var Language |
||
22 | */ |
||
23 | private $language; |
||
24 | |||
25 | use RedisTrait; |
||
26 | use IdGeneratorTrait; |
||
27 | use EventDispatcherTrait; |
||
28 | |||
29 | /** |
||
30 | * @Inject("@Expression.Language") |
||
31 | * @param Language $language |
||
32 | */ |
||
33 | 2 | public function __construct(Language $language) |
|
37 | |||
38 | /** |
||
39 | * @param Entity $entity |
||
40 | */ |
||
41 | 1 | public function save(Entity $entity) |
|
42 | { |
||
43 | 1 | $entity->expressionId = $entity->expressionId ?: $this->generateUniqueId(); |
|
44 | 1 | $entity->payload = $entity->payload ?: []; |
|
45 | |||
46 | 1 | $entity->compiledCondition = $this->language->compile( |
|
47 | 1 | implode(' && ', $entity->conditions), |
|
48 | 1 | $this->language->getParameterNames() |
|
49 | 1 | ); |
|
50 | |||
51 | 1 | $this->getRedis()->hset( |
|
52 | 1 | self::REDIS_KEY, |
|
53 | 1 | $entity->expressionId, |
|
54 | 1 | serialize($entity) |
|
55 | 1 | ); |
|
56 | |||
57 | 1 | $this->updateCache(); |
|
58 | 1 | } |
|
59 | |||
60 | /** |
||
61 | * @return Entity[] |
||
62 | */ |
||
63 | public function getAll() |
||
64 | { |
||
65 | return array_map( |
||
66 | function ($string) { |
||
67 | return unserialize($string); |
||
68 | }, |
||
69 | $this->getRedis()->hgetall(self::REDIS_KEY) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param int[] $entityIds |
||
75 | * @return Entity[] |
||
76 | */ |
||
77 | public function getEntities(array $entityIds) |
||
78 | { |
||
79 | return array_map( |
||
80 | function ($string) { |
||
81 | return unserialize($string); |
||
82 | }, |
||
83 | $this->getRedis()->hmget(self::REDIS_KEY, $entityIds) |
||
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param int $expressionId |
||
89 | * @return bool |
||
90 | */ |
||
91 | 1 | public function delete($expressionId) |
|
92 | { |
||
93 | 1 | $result = $this->getRedis()->hdel(self::REDIS_KEY, [$expressionId]); |
|
94 | 1 | if ($result) { |
|
95 | $this->updateCache(); |
||
96 | } |
||
97 | |||
98 | 1 | return (bool)$result; |
|
99 | } |
||
100 | |||
101 | 1 | private function updateCache() |
|
106 | } |
||
107 |