Completed
Pull Request — master (#116)
by Roberto
13:38
created

Scripting   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 131
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A eval() 0 8 3
A evalSha() 0 8 3
B script() 0 24 7
A getLastError() 0 4 1
A clearLastError() 0 4 1
A prefix() 0 4 1
A unserialize() 0 4 1
A serialize() 0 4 1
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
use Webdcg\Redis\Exceptions\ScriptCommandException;
6
7
trait Scripting
8
{
9
    /*
10
     * Available Script Commands
11
     */
12
    protected $SCRIPT_COMMANDS = ['LOAD', 'FLUSH', 'KILL', 'EXISTS'];
13
14
    /**
15
     * Evaluate a LUA script serverside.
16
     *
17
     * See: https://redis.io/commands/eval.
18
     *
19
     * @param  string      $script
20
     * @param  array       $arguments
21
     * @param  int|integer $numKeys
22
     *
23
     * @return mixed       What is returned depends on what the LUA script
24
     *                     itself returns, which could be a scalar value
25
     *                     (int/string), or an array. Arrays that are returned
26
     *                     can also contain other arrays, if that's how it was
27
     *                     set up in your LUA script. If there is an error
28
     *                     executing the LUA script, the getLastError()
29
     *                     function can tell you the message that came back
30
     *                     from Redis (e.g. compile error).
31
     */
32
    public function eval(string $script, ?array $arguments = null, ?int $numKeys = null)
33
    {
34
        if (!is_null($arguments) && !is_null($numKeys)) {
35
            return $this->redis->eval($script, $arguments, $numKeys);
0 ignored issues
show
Bug introduced by
The property redis does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        }
37
38
        return $this->redis->eval($script);
39
    }
40
41
42
    /**
43
     * Evaluate a LUA script serverside, from the SHA1 hash of the script instead
44
     * of the script itself.
45
     *
46
     * In order to run this command Redis will have to have already loaded the
47
     * script, either by running it or via the SCRIPT LOAD command.
48
     *
49
     * See: https://redis.io/commands/evalsha.
50
     *
51
     * @param  string     $sha1         The sha1 encoded hash of the script you want to run.
52
     * @param  array|null $arguments    Arguments to pass to the LUA script.
53
     * @param  int|null   $numKeys      The number of arguments that should go into the
54
     *                                  KEYS array, vs. the ARGV array when Redis spins
55
     *                                  the script (optional).
56
     *
57
     * @return mixed
58
     */
59
    public function evalSha(string $sha1, ?array $arguments = null, ?int $numKeys = null)
60
    {
61
        if (!is_null($arguments) && !is_null($numKeys)) {
62
            return $this->redis->evalSha($sha1, $arguments, $numKeys);
63
        }
64
65
        return $this->redis->evalSha($sha1);
66
    }
67
68
69
    /**
70
     * Execute the Redis SCRIPT command to perform various operations on the
71
     * scripting subsystem.
72
     *
73
     * See: https://redis.io/commands/script-load.
74
     * See: https://redis.io/commands/script-flush.
75
     *
76
     * @param  string $command
77
     * @param  splat $scripts
78
     *
79
     * @return mixed            SCRIPT LOAD will return the SHA1 hash of the
80
     *                                 passed script on success, and FALSE on
81
     *                                 failure.
82
     *                          SCRIPT FLUSH should always return TRUE
83
     *                          SCRIPT KILL will return true if a script was
84
     *                                  able to be killed and false if not.
85
     *                          SCRIPT EXISTS will return an array with TRUE
86
     *                                  or FALSE for each passed script.
87
     */
88
    public function script(string $command, ...$scripts)
89
    {
90
        $command = strtoupper($command);
91
92
        if (!in_array($command, $this->SCRIPT_COMMANDS)) {
93
            throw new ScriptCommandException('Script Command not supported', 1);
94
        }
95
96
        switch ($command) {
97
            case 'FLUSH':
98
            case 'KILL':
99
                return $this->redis->script($command);
100
101
            case 'LOAD':
102
                if (count($scripts) != 1) {
103
                    throw new ScriptCommandException('Invalid Number of Scripts to Load', 1);
104
                }
105
106
                return $this->redis->script($command, $scripts[0]);
107
            
108
            case 'EXISTS':
109
                return $this->redis->script($command, ...$scripts);
110
        }
111
    }
112
113
    public function getLastError(): bool
114
    {
115
        return false;
116
    }
117
118
    public function clearLastError(): bool
119
    {
120
        return false;
121
    }
122
123
    public function prefix(): bool
124
    {
125
        return false;
126
    }
127
128
    public function unserialize(): bool
129
    {
130
        return false;
131
    }
132
133
    public function serialize(): bool
134
    {
135
        return false;
136
    }
137
}
138