ModuleTrait::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Redislabs\Module;
5
6
use Redislabs\Exceptions\InvalidCommandException;
7
use Redislabs\Interfaces\CommandInterface;
8
use Redislabs\Interfaces\RedisClientInterface;
9
use Predis\Client as PredisClient;
10
use Redis as PhpRedisClient;
11
use Redislabs\RedisClient\Predis as RedislabsPredisClient;
12
use Redislabs\RedisClient\Redis as RedislabsPhpRedisClient;
13
14
trait ModuleTrait
15
{
16
    protected $redisClient;
17
18
    public function __construct(RedisClientInterface $redisClient)
19
    {
20
        $this->redisClient = $redisClient;
21
    }
22
23
    final public static function createWithPredis(PredisClient $predisClient) : self
24
    {
25
        return new static(
26
            new RedislabsPredisClient($predisClient)
27
        );
28
    }
29
30
    final public static function createWithPhpRedis(PhpRedisClient $predisClient) : self
31
    {
32
        return new static(
33
            new RedislabsPhpRedisClient($predisClient)
34
        );
35
    }
36
37
    final public function runCommand(CommandInterface $command)
38
    {
39
        $response = $this->redisClient->rawCommand(
40
            $command->getCommand(),
41
            $command->getArguments()
42
        );
43
        $callback = $command->getResponseCallback();
44
        return $callback ? $callback($response) : $response;
45
    }
46
47
    final public function __call($name, $arguments)
48
    {
49
        throw new InvalidCommandException(
50
            sprintf('%s::%s is not a valid method', static::$moduleName, $name)
51
        );
52
    }
53
}
54