Completed
Push — master ( c95bdf...5e09a3 )
by Matze
02:56
created

Gateway::getEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6667
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BrainExe\Expression;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Annotations\Annotations\Service;
7
use BrainExe\Core\Traits\EventDispatcherTrait;
8
use BrainExe\Core\Traits\IdGeneratorTrait;
9
use BrainExe\Core\Traits\RedisTrait;
10
use BrainExe\Expression\Listener\RebuildExpressionCache;
11
12
/**
13
 * @Service("Expression.Gateway", public=false)
14
 */
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)
34
    {
35 2
        $this->language = $language;
36 2
    }
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()
102
    {
103 1
        $event = new RebuildExpressionCache();
104 1
        $this->dispatchEvent($event);
105 1
    }
106
}
107